0

I have an adapter and I want to add data to adapter.This is my source code:

private static class Random{
    public  JsonObject randomData=null;
    public  Integer badgeNumber=null;

    private Random(JsonObject randomData,Integer badgeNumber) {
        this.randomData=randomData;
        this.badgeNumber=badgeNumber;
    }
}

private static class Randoms{
    private List<Random> randoms=null;

    public Randoms(List<Random> randoms) {
        this.randoms=randoms;
    }
    public Random get(int position) {
        return randoms.get(position);
    }
    public int size() {
        return randoms.size();
    }
}
private static class RandomsAdapter extends BaseAdapter{
    private Randoms randoms;
    private LayoutInflater inflater;
    private RandomsAdapter(Context context,Randoms randoms) {
        this.randoms=randoms;
        this.inflater = LayoutInflater.from(context);
    }
    public void updateRandoms(Randoms randoms) {
        this.randoms=randoms;
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return randoms.size();
    }
    @Override
    public Random getItem(int position) {
        return randoms.get(position);
    }
    @Override
    public long getItemId(int position) {
        return 0;
    }
    public View getView(int position,View convertView,ViewGroup parent) {
        View view=convertView;

        if (convertView == null) {
            convertView=inflater.inflate(R.layout.random_bars,parent,false);
        }
        Random item=getItem(position);
        Log.w("testt",""+item);
        return convertView;
    }
}

And I am defining the adapter with this code:

Randoms randoms=new Randoms(randomsList);
randomsAdapter=new RandomsAdapter(this,randoms);
listView = (ListView)findViewById(R.id.list);
listView.setAdapter(randomsAdapter);

When I try to add a data I am using this line

randomsList.add(new Random(result.get(i).getAsJsonObject(),1));

But this not working,I can't see any log with "testt" tag.How can I resolve this ?

I mean getView method is not working.

Sagar Pilkhwal
  • 3,998
  • 2
  • 25
  • 77
Serkay Sarıman
  • 465
  • 1
  • 10
  • 17

2 Answers2

0

after adding

randomsList.add(new Random(result.get(i).getAsJsonObject(),1));

call notifyDataSetChanged() on your adapter

randomsAdapter.notifyDataSetChanged();

Edit

((RandomsAdapter) randomsAdapter)notifyDataSetChanged();
Nadir Belhaj
  • 11,953
  • 2
  • 22
  • 21
0

As suggested by others, use

randomsAdapter.notifyDataSetChanged();

If you get an error like "notifyDataSetChanged() is undefined for the type ListAdapter", I guess it's because you're getting your adapter through getAdapter() or such method which returns a ListAdapter. In this case, you should cast it to either BaseAdapter or RandomsAdapter. For instance,

((RandomsAdapter) randomsAdapter).notifyDataSetChanged();
Sagar Pilkhwal
  • 3,998
  • 2
  • 25
  • 77
Herve
  • 36
  • 7