我想为ListView添加数据,但getCount()方法总是返回0。
public class MySellingListAdapter extends ArrayAdapter<MySellingData>
{
private Context context;
private List<MySellingData> listSellingData;
public MySellingListAdapter (Context context, int resourceId, List<MySellingData> listSellingData)
{
super(context, resourceId, listSellingData);
this.context = context;
this.listSellingData = listSellingData;
}
@Override
public int getCount()
{
int size = listSellingData == null ? 0 : listSellingData.size();
Log.e("DD", "" + size);
return size;
}
}
我有一个Fragment类,我设置了适配器:
@Override
protected void onCreate(Bundle bundle)
{
if (mySellingListAdapter == null)
mySellingListAdapter = new MySellingListAdapter(instance, R.layout.row_selling_item, listSellingData);
}
我有异步调用,我获取数据:
private void loadData()
{
// ...
@Override void onPostExecute(Data... values)
{
mySellingListAdapter.notifyDataSetChanged();
}
}
DD 0
DD 0
DD 0
DD 0
DD 0
DD 0
答案 0 :(得分:3)
getCount()
方法如果null
为List
或为空(size = 0),则会返回null
。在您发布的代码中,您永远不会向listSellingData
添加/添加任何内容。
除此之外,您在不添加任何内容的情况下调用mySellingListAdapter.notifyDataSetChanged();
。
我想你想做点什么:
private void loadData()
{
// ...
@Override
void onPostExecute(Data... values)
{
// note that this is Data and your type is MySellingData
// or even iterate over the values array and add all
mySellingListAdapter.add(values[0])
mySellingListAdapter.notifyDataSetChanged();
}
}