Search code examples
androidandroid-listview

android-How to refresh data in listview with Baseapdater


this is my code. I get new data and I need to refresh the list . I'm using base adapter for making my listview .

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lv=(ListView) findViewById(R.id.listView1);
    lv.setAdapter(new myAdapter(this));

}

class SingleRow{
    String title; 
    String description;
    int image ; 
    SingleRow(String title,String description, int image) {
        this.title=title; 
        this.description=description;
        this.image=image;
    }
}

class myAdapter extends BaseAdapter{
    ArrayList<SingleRow> list; 
    Context context;
    myAdapter(Context c){
        context=c; 
        list=new ArrayList<SingleRow>();
        Resources res=c.getResources();
        String[] titles=res.getStringArray(R.array.titiles);
        String[] descriptions=res.getStringArray(R.array.descriptions);
        int[] images={R.drawable.a1 ,R.drawable.a2,R.drawable.a3,R.drawable.a49};
        for(int i =0; i<4;i++){  
            list.add(new SingleRow(titles[i], descriptions[i], images[i]));
        }
    }

    @Override
    public int getCount() {
        return list.size(); 
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {

        LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row= inflater.inflate(R.layout.custom_list_items, parent,false);

        TextView title= (TextView) row.findViewById(R.id.title);
        TextView description= (TextView) row.findViewById(R.id.desc);
        ImageView image=(ImageView) row.findViewById(R.id.imageView1);

        SingleRow temp= list.get(position);

        title.setText(temp.title);
        description.setText(temp.description);
        image.setImageResource(temp.image);
        return row;
    }

Where should I put notifyDataSetChanged() or other methods to refresh my list and show new data ?

thanks


Solution

  • NotifyDataSetChanged works to notify your data set associated with the Adapter.Now Ideally when ever you are setting an adapter after that you should call notifydatasetChanged to make the previous data set invalidate and load the new dataset from your adapter.

    It is also not suggested to create a fresh adapter object everytime.

    You can do something like this:->

    Myadapter adapter = new Myadapter(context,dataset of your custom arraylist);//Constructor

    after that whenever you will get new data set call this

    adapter.ArrayList<T> = new ArrayList<T>;
    

    then

    adapter.notifyDataSetChanged();

    If you don't want to change anything then in your code after your new adapter call...call adapter.notifyDataSetChanged();

    EDIT:

    in Activity:

    ArrayList<SingleRow> rows = new ArrayList<SingleRow>();
    myAdapter theAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv=(ListView) findViewById(R.id.listView1);
        Resources res = getResources();
        String[] titles=res.getStringArray(R.array.titiles);
        String[] descriptions=res.getStringArray(R.array.descriptions);
        int[] images={R.drawable.a1 ,R.drawable.a2,R.drawable.a3,R.drawable.a49};
        for(int i =0; i<4;i++){  
            rows.add(new SingleRow(titles[i], descriptions[i], images[i]));
        }
        theAdapter = new myAdapter(this, rows);
        lv.setAdapter(theAdapter);
    }
    
    void doAdd()
    {
        rows.add(new new SingleRow(...some values...);
        theAdapter.notifyDataSetChanged()
    }
    
    void doClear()
    {
        rows.clear();
        theAdapter.notifyDataSetChanged()
    }
    

    in adapter:

    myAdapter(Context c, ArrayList<SingleRow> rows)
    {
        context=c;
        list = rows;
    }   
    

    you can aslo take a look at ArrayAdapter implementation ...