我有关于 notifyDataSetChanged 的问题,我读了一些其他的帖子但是帮不了我,我还有问题,我在我的 listview setadapter 之后调用它但是我的列表没有任何改变!这是我的代码,请帮助我,谢谢
public class PostDataAsyncTask extends AsyncTask<String, String, String> {
// this will post our text data
protected void onPreExecute() {
super.onPreExecute();
// do stuff before posting data
}
@Override
protected String doInBackground(String... strings) {
try {
postTextandGetRespons("http://demo.codeofaninja.com/tutorials/json-example-with-php/index.php");
JSONObject JsonOb = new JSONObject(responseString);
JSONArray messages = JsonOb.getJSONArray("Users");
for ( int i=0; i<= f;i++){
JSONObject c = messages.getJSONObject(i);
firstname = c.getString("firstname");
lastname = c.getString("lastname");
username = c.getString("username");
items.add(new item(firstname,lastname,username));
}
adapter.notifyDataSetChanged();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String responseStr) {
if ( setAdapter == true) {
lv = (ListView) findViewById(R.id.listView_asabani);
adapter = new adapter_common(getBaseContext(), items);
lv.setAdapter(adapter);
setAdapter = false;
}
}
}
最佳答案
将 adapter.notifyDataSetChanged();
移动到 onPostExecute()
,这是 UI 代码应该运行的地方。
关于android - notifyDataSetChanged 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33456585/