简体   繁体   中英

Android: Using notifyDataSetChanged to update listview

I have a listview which I want to edit from a dialog box. I've read from googling that I need to notifyDataSetChanged() on the listview, however when I try to do this I get an error:

The method notifyDataSetChanged(View) is undefined for the type ListView

在此处输入图片说明

My listview is set originally at the top of my code with just a:

ListView listView;

It's then set in a routine on the onload

public void loadItems(){
    //Removed - just getting the data

     int rowCount;       
     rowCount = mCategory.size();

     listView = (ListView) findViewById(R.id.lvItems);


     int[] colors = {0, 0xFFFF0000, 0}; 
     listView.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
     listView.setDividerHeight(1);
     listView.setAdapter(new CustomAdapter());

     listView.setClickable(true);
     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

       @Override
       public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

          loadPopup(mDescription.get(position).toString(), mCountList.get(position).toString(), mTemplatecode.get(position).toString());
       }
     });
     //Removed - Tidying up
}

The dialog box that is loaded on the onlick has a simple interface, a minus button, a plus button, a text box and a go button. The number in the textbox is changed which does a db call. When the dialog box closes I want it to refresh the listview behind to reflect the new change. Ie basically re-run the loadItems() routine again.

This is what happens when you click the go button on the dialog. I'm obviously putting the notifyDataSetChanged in the wrong place as it wont even run.

 btnGo.setOnClickListener( new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            addAsset(v, AssetDesc, txt.getText().toString(), templateCode);
            listView.notifyDataSetChanged();
            loadItems();
            dialog.dismiss();
        }
    });

Custom adapter:

class CustomAdapter extends BaseAdapter
    {

    @Override
    public int getCount() {

        return mDescription.size();
    }
@Override
public Object getItem(int arg0) {

    return null;
}

@Override
public long getItemId(int arg0) {

    return 0;
}

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {

    LayoutInflater inf=getLayoutInflater();
    View v=inf.inflate(R.layout.noncriticalasset, arg2,false);

    TextView tv=(TextView)v.findViewById(R.id.txtOption);
    TextView tvCount=(TextView)v.findViewById(R.id.txtCount);

    tv.setText(mDescription.get(arg0).toString()); 
    tvCount.setText(mCountList.get(arg0).toString()); 

    return v;
}

}

您需要在适配器上而不是ListView本身上调用notifyDataSetChanged

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM