Zbarcea Christian
Zbarcea Christian

Reputation: 9548

Android - ArrayAdapter getCount always is returning 0

I want to add data for my ListView, but getCount() method always is returning me 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;
    }
}

I have a Fragment class, where I set up the adapter:

@Override
protected void onCreate(Bundle bundle)
{
   if (mySellingListAdapter == null)
      mySellingListAdapter = new MySellingListAdapter(instance, R.layout.row_selling_item, listSellingData);
}

And I have an asynchronous call where I get data:

private void loadData()
{
   // ...
   @Override void onPostExecute(Data... values)
   {
      mySellingListAdapter.notifyDataSetChanged();
   }
}

DD 0

DD 0

DD 0

DD 0

DD 0

DD 0

Upvotes: 1

Views: 5947

Answers (1)

Langusten Gustel
Langusten Gustel

Reputation: 11002

The getCount() method will return null if the List ist either null or empty (size=0). In your posted code you never add/put anything to listSellingData.

In addition to that you call mySellingListAdapter.notifyDataSetChanged(); without adding anything before.

I guess you want to do something like:

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();
   }
}

Upvotes: 3

Related Questions