Android 适配器 NotifyDataSetChanged 不工作
Android adaper NotifyDataSetChange Not Working
我想在删除列表视图后刷新它。即使我正在使用 notifyDataSetChanged,它仍然不会立即更新。我需要回去再回到这个意图。需要帮忙。提前致谢。
viewData();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, final long id) {
alart=new AlertDialog.Builder(ViewData.this);
alart.setTitle("Are you sure?");
alart.setMessage("Are you sure want to clear this data?");
alart.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
boolean ans=mydb.deleteTitle(position);
Toast.makeText(getApplicationContext(),"->"+ans,Toast.LENGTH_SHORT).show();
adapter.notifyDataSetChanged();
}
});
alart.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog a = alart.create();
a.show();
}
});
}
public void viewData() {
Cursor res = mydb.getData();
if (res.getCount() == 0) {
Toast.makeText(ViewData.this, "No Data Found", Toast.LENGTH_SHORT);
} else {
while (res.moveToNext()) {
arrayemail.add(res.getString(1) + "\n");
arraypass.add(res.getString(2) + "\n");
}
adapter = new CustomAdapter(this, arrayemail, arraypass);
listView.setAdapter(adapter);
}
}
您正在从数据库中删除,但并未从适配器中删除项目:
boolean ans=mydb.deleteTitle(position); //this doesn't actually change the adapter, just the database.
所以要么
- 从数据库中删除时从适配器中删除,然后在
onClick
方法中调用通知已更改
- 从列表中删除项目后调用
viewData()
,也在 onClick
内,这将创建一个全新的适配器,再次从您的数据库中读取(这可能不是您应该使用的方法拿)
我想在删除列表视图后刷新它。即使我正在使用 notifyDataSetChanged,它仍然不会立即更新。我需要回去再回到这个意图。需要帮忙。提前致谢。
viewData();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, final long id) {
alart=new AlertDialog.Builder(ViewData.this);
alart.setTitle("Are you sure?");
alart.setMessage("Are you sure want to clear this data?");
alart.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
boolean ans=mydb.deleteTitle(position);
Toast.makeText(getApplicationContext(),"->"+ans,Toast.LENGTH_SHORT).show();
adapter.notifyDataSetChanged();
}
});
alart.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog a = alart.create();
a.show();
}
});
}
public void viewData() {
Cursor res = mydb.getData();
if (res.getCount() == 0) {
Toast.makeText(ViewData.this, "No Data Found", Toast.LENGTH_SHORT);
} else {
while (res.moveToNext()) {
arrayemail.add(res.getString(1) + "\n");
arraypass.add(res.getString(2) + "\n");
}
adapter = new CustomAdapter(this, arrayemail, arraypass);
listView.setAdapter(adapter);
}
}
您正在从数据库中删除,但并未从适配器中删除项目:
boolean ans=mydb.deleteTitle(position); //this doesn't actually change the adapter, just the database.
所以要么
- 从数据库中删除时从适配器中删除,然后在
onClick
方法中调用通知已更改 - 从列表中删除项目后调用
viewData()
,也在onClick
内,这将创建一个全新的适配器,再次从您的数据库中读取(这可能不是您应该使用的方法拿)