错误:无法解决notifyDataSetChanged();安卓 [英] Error: Cannot Resolve notifyDataSetChanged(); Android

查看:251
本文介绍了错误:无法解决notifyDataSetChanged();安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在更新ListView时遇到麻烦.

I'm having some trouble with updating my ListView.

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

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

                }
            });



        }
    });


    }

推荐答案

notifyDataSetChanged不是ListAdapter的方法,而是BaseAdapter的方法(ArrayAdapter是BaseAdapter的子类).要解决此问题,您只需投射ListAdapter

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

这篇关于错误:无法解决notifyDataSetChanged();安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆