问题描述
我有AA客户适配器( SongsAdapter
)扩展 ArrayAdapter
,它包含<$ C $的数组C>歌曲的对象。在我片段 onCreateView
方法,我想初始化该适配器。
适配器=新SongsAdapter(的getContext(),arrayOfSongs);
问题是, arrayOfSongs
最初为null。用户应搜索数据库的iTunes上的歌曲,当我得到回应,我解析JSON并创建歌曲对象,然后将它们添加到我的适配器
adapter.addAll(听歌);
然后
adapter.notifyDataSetChanged();
但我发现了一个异常
显示java.lang.NullPointerException在android.widget.ArrayAdapter.getCount
如何隐藏在ListView直到用户第一次搜索,然后取消隐藏它来显示结果。我将如何正确初始化适配器?
下面是我的适配器
公共类SongsAdapter扩展ArrayAdapter&LT;宋&GT; {
公共SongsAdapter(上下文的背景下,ArrayList的&LT;宋&GT;歌曲){ 超(背景下,0,歌);
} @覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
宋歌=的getItem(位置); 如果(convertView == NULL)
convertView = LayoutInflater.from(的getContext())膨胀(R.layout.item_song,父母,假的)。 TextView的ARTISTNAME =(TextView中)convertView.findViewById(R.id.artistName);
TextView的TRACKNAME =(TextView中)convertView.findViewById(R.id.trackName); artistName.setText(song.getArtist());
trackName.setText(song.getTitle()); 返回convertView;
}
}
您可以检查ArrayList是在getCount将方法无效,并返回相应的数据。
公共类SongsAdapter延伸BaseAdapter {ArrayList的&LT;宋&GT; mList;
上下文mContext;公共SongsAdapter(上下文的背景下,ArrayList的&LT;宋&GT;歌曲){ mList =歌曲;
mContext =背景;}@覆盖
公众诠释的getCount(){
如果(mList == NULL)
{ 返回0;
}
其他{ 返回mList.size();
}
}@覆盖
公共对象的getItem(INT位置){
返回mList.get(位置);
}@覆盖
众长getItemId(INT位置){
返回0;
}@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
宋歌=的getItem(位置); 如果(convertView == NULL)
convertView = LayoutInflater.from(的getContext())膨胀(R.layout.item_song,父母,假的)。 TextView的ARTISTNAME =(TextView中)convertView.findViewById(R.id.artistName);
TextView的TRACKNAME =(TextView中)convertView.findViewById(R.id.trackName); artistName.setText(song.getArtist());
trackName.setText(song.getTitle()); 返回convertView;
}
}
标记此,如果它可以帮助别人让我知道如果你需要任何说明。快乐编码。
I have a a customer adapter (SongsAdapter
) that extends ArrayAdapter
and it contains an array of Song
objects. In my fragments onCreateView
method I'm trying to initialize this adapter.
adapter = new SongsAdapter(getContext(), arrayOfSongs);
the problem is that arrayOfSongs
is initially null. The user should search for a song on the iTunes database and when I get a response, I parse the JSON and create song objects and then add them to my adapter
adapter.addAll(songs);
and then
adapter.notifyDataSetChanged();
but I'm getting an exception
java.lang.NullPointerException at android.widget.ArrayAdapter.getCount
How can I hide the listview until the first search by the user, and then unhide it to display the results. How would I properly initialize the adapter?
Here is my adapter
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;
}
}
You can check if the arraylist is null in getCount method and return the data accordingly.
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;
}
}
Mark this up if it helps else let me know if you need any description. Happy coding.
这篇关于创建列表视图适配器没有开始传递数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!