gpt4 book ai didi

android - 错误 : Cannot Resolve notifyDataSetChanged(); Android

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:30:26 25 4
gpt4 key购买 nike

我在更新 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, View view, int position, long id) {
String s1 = String.valueOf(parent.getItemAtPosition(position));
pe.remove(s1);
((BaseAdapter)theAdapter).notifyDataSetChanged();

关于android - 错误 : Cannot Resolve notifyDataSetChanged(); Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28128734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com