错误:无法解析notifyDataSetChanged(); Android的

前端之家收集整理的这篇文章主要介绍了错误:无法解析notifyDataSetChanged(); Android的前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在更新ListView时遇到了一些麻烦.

所以我使用了notifyDataSetChanged();但它说它无法解决.

下面是代码中不起作用的部分:

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();

                }
            });



        }
    });


    }

解决方法

notifyDataSetChanged不是ListAdapter的方法,而是BaseAdapter的方法,(ArrayAdapter是BaseAdapter的子类).要解决此问题,您只需转换ListAdapter即可
theListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent,long id) {
            String s1 = String.valueOf(parent.getItemAtPosition(position));
            pe.remove(s1);
             ((BaseAdapter)theAdapter).notifyDataSetChanged();

猜你在找的Android相关文章