Error: Cannot Resolve notifyDataSetChanged(); Android

流过昼夜 提交于 2020-01-23 06:14:13

问题


I'm having some trouble with updating my ListView.

So I used notifyDataSetChanged(); but it says that it can't be resolved.

Heres the part of the code that is not working:

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main_activity2);
    background2= (RelativeLayout) findViewById(R.id.background);
    final ListView theListView = (ListView) findViewById(R.id.listView);
    Intent calledActivity=getIntent();
    final List pe=calledActivity.getExtras().getStringArrayList("Caller1");

    String []s =new String[pe.size()];
    for(int i=0;i<pe.size();i++)
    {
        s[i]=(String)pe.get(i);
    }

            final ListAdapter theAdapter= new ArrayAdapter<String>(this,    android.R.layout.simple_list_item_1, pe);
             theListView.setAdapter(theAdapter);
    final int cnt=1;
    theListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String s1 = String.valueOf(parent.getItemAtPosition(position));
            pe.remove(s1);
            runOnUiThread(new Runnable() {
                public void run() {
                    theAdapter.notifyDataSetChanged();

                }
            });



        }
    });


    }

回答1:


notifyDataSetChanged is not a method of ListAdapter, but of BaseAdapter, (ArrayAdapter is a subclass of BaseAdapter). To fix you can simply cast the ListAdapter

theListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String s1 = String.valueOf(parent.getItemAtPosition(position));
            pe.remove(s1);
             ((BaseAdapter)theAdapter).notifyDataSetChanged();


来源:https://stackoverflow.com/questions/28128734/error-cannot-resolve-notifydatasetchanged-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!