Android change data in adapter

12,511

Solution 1

This information is still not enough to see the problem and help you. that's why I will explain with a simple example how to use notifyDataSetChanged(); on MyCustomAdapter. Here is a example how to populate the adapter:

private ArrayList<String> _names;
private MyCustomAdapter _adapter;
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    _names = new ArrayList<String>();

    for(int i = 0; i < _names.size();i++){
        _names.add("Element : "+i);
    }

    _adapter = new MyCustomAdapter(this, _names);
    _myListView.setAdapter(_adapter);


}

private void refreshListView(){
    _names.clear();
    for(int i = 0; i < _names.size();i++){
        _names.add("New Element : "+i);
    }
    _adapter.notifyDataSetChanged();
}

The idea here is to change the List which you are using to populate your listview. Clear it, populate it with the new data and after that call notifyDataSetChanged();.

That's all.

Solution 2

Short and fast, call these two lines after you feel changes have been done in dataset.

AppAdapter adapter=new AppAdapter(getPackageManager(), getResolveInfoList(0));
setListAdapter(adapter);
Share:
12,511
ptia
Author by

ptia

Rome

Updated on June 04, 2022

Comments

  • ptia
    ptia almost 2 years

    I use this adapter for my ListView:

    Appadapter extends ArrayAdapter<ResolveInfo> 
         private PackageManager pm=null;
         List<ResolveInfo> apps;
         AppAdapter(PackageManager pm, List<ResolveInfo> apps) {
        super(Launchalot.this, R.layout.row, apps);
      this.apps=apps;
      this.pm=pm;
    }
    
    @Override
    public View getView(int position, View convertView,
                          ViewGroup parent) {
          Log.w(Launchalot.this.getPackageName(),"getView");
      if (convertView==null) {
        convertView=newView(parent);
      }
    
      bindView(position, convertView);
    
      return(convertView);
    }
    
    private View newView(ViewGroup parent) {
        Log.w(Launchalot.this.getPackageName(),"newView");
      return(getLayoutInflater().inflate(R.layout.row, parent, false));
    }
    
    
    private void bindView(int position, View row) {
      TextView label=(TextView)row.findViewById(R.id.label);
      Log.w(Launchalot.this.getPackageName(),"bindView");
      label.setText(getItem(position).loadLabel(pm));
    
      ImageView icon=(ImageView)row.findViewById(R.id.icon);
    
      icon.setImageDrawable(getItem(position).loadIcon(pm));
    }
        public void notifyDataSetChanged() {
            super.notifyDataSetChanged();
            setNotifyOnChange(true);}}
    

    I populate my view using this code in a ListActivity class:

    AppAdapter adapter=new AppAdapter(getPackageManager(), getResolveInfoList(0));
    setListAdapter(adapter);
    

    Now i have changed List apps I've called notifyDataSetChanged() but, as I think, nothing changed. Please tell me a solution. Thanks