在最初不传递数组的情况下为列表视图创建适配器
Creating an adapter for listview without passing an array initially
我有一个扩展 ArrayAdapter
的客户适配器 (SongsAdapter
),它包含一个 Song
对象数组。在我的片段 onCreateView
方法中,我试图初始化这个适配器。
adapter = new SongsAdapter(getContext(), arrayOfSongs);
问题是 arrayOfSongs
最初是空的。用户应该在 iTunes 数据库中搜索一首歌曲,当我得到响应时,我解析 JSON 并创建歌曲对象,然后将它们添加到我的适配器
adapter.addAll(songs);
然后
adapter.notifyDataSetChanged();
但我遇到了异常
java.lang.NullPointerException at android.widget.ArrayAdapter.getCount
如何在用户第一次搜索之前隐藏列表视图,然后取消隐藏以显示结果。我将如何正确初始化适配器?
这是我的适配器
public class SongsAdapter extends ArrayAdapter<Song> {
public SongsAdapter(Context context, ArrayList<Song> songs) {
super(context, 0, songs);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Song song = getItem(position);
if (convertView == null)
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_song, parent, false);
TextView artistName = (TextView) convertView.findViewById(R.id.artistName);
TextView trackName = (TextView) convertView.findViewById(R.id.trackName);
artistName.setText(song.getArtist());
trackName.setText(song.getTitle());
return convertView;
}
}
您可以在 getCount 方法中检查 arraylist 是否为空,并return相应地检查数据。
public class SongsAdapter extends BaseAdapter {
ArrayList<Song> mList;
Context mContext;
public SongsAdapter(Context context, ArrayList<Song> songs) {
mList = songs;
mContext = context;
}
@Override
public int getCount() {
if(mList==null)
{
return 0;
}
else {
return mList.size();
}
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Song song = getItem(position);
if (convertView == null)
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_song, parent, false);
TextView artistName = (TextView) convertView.findViewById(R.id.artistName);
TextView trackName = (TextView) convertView.findViewById(R.id.trackName);
artistName.setText(song.getArtist());
trackName.setText(song.getTitle());
return convertView;
}
}
如果它有帮助,请标记它,如果您需要任何说明,请告诉我。编码愉快。
我有一个扩展 ArrayAdapter
的客户适配器 (SongsAdapter
),它包含一个 Song
对象数组。在我的片段 onCreateView
方法中,我试图初始化这个适配器。
adapter = new SongsAdapter(getContext(), arrayOfSongs);
问题是 arrayOfSongs
最初是空的。用户应该在 iTunes 数据库中搜索一首歌曲,当我得到响应时,我解析 JSON 并创建歌曲对象,然后将它们添加到我的适配器
adapter.addAll(songs);
然后
adapter.notifyDataSetChanged();
但我遇到了异常
java.lang.NullPointerException at android.widget.ArrayAdapter.getCount
如何在用户第一次搜索之前隐藏列表视图,然后取消隐藏以显示结果。我将如何正确初始化适配器?
这是我的适配器
public class SongsAdapter extends ArrayAdapter<Song> {
public SongsAdapter(Context context, ArrayList<Song> songs) {
super(context, 0, songs);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Song song = getItem(position);
if (convertView == null)
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_song, parent, false);
TextView artistName = (TextView) convertView.findViewById(R.id.artistName);
TextView trackName = (TextView) convertView.findViewById(R.id.trackName);
artistName.setText(song.getArtist());
trackName.setText(song.getTitle());
return convertView;
}
}
您可以在 getCount 方法中检查 arraylist 是否为空,并return相应地检查数据。
public class SongsAdapter extends BaseAdapter {
ArrayList<Song> mList;
Context mContext;
public SongsAdapter(Context context, ArrayList<Song> songs) {
mList = songs;
mContext = context;
}
@Override
public int getCount() {
if(mList==null)
{
return 0;
}
else {
return mList.size();
}
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Song song = getItem(position);
if (convertView == null)
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_song, parent, false);
TextView artistName = (TextView) convertView.findViewById(R.id.artistName);
TextView trackName = (TextView) convertView.findViewById(R.id.trackName);
artistName.setText(song.getArtist());
trackName.setText(song.getTitle());
return convertView;
}
}
如果它有帮助,请标记它,如果您需要任何说明,请告诉我。编码愉快。