1

我想为我的 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();
   }
}

0

0

0

0

0

0

4

1 回答 1

3

如果ist 或者为空(size=0),该getCount()方法将返回。在您发布的代码中,您永远不会向.nullListnulllistSellingData

除此之外,您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();
   }
}
于 2013-11-03T22:18:57.520 回答