gpt4 book ai didi

java - Android 中如何刷新 ListView ?

转载 作者:行者123 更新时间:2023-12-02 08:13:05 24 4
gpt4 key购买 nike

我在 Activity 中使用这段代码:

     public void carregaListaDemanda(){
setContentView(R.layout.listaviewdemanda);
lstDem = (ListView) findViewById(R.id.listViewDemanda);
DemandaAdapter adapter = new DemandaAdapter(ctx,
bancodedados.getAllDem(), this);
lstDem.setAdapter(adapter);
lstDem.setItemsCanFocus(true);
teste=0;
}

在适配器中:

    public void onClick(View v) {
AlertDialog alertDialog = new AlertDialog.Builder(ctx).create();
alertDialog.setTitle("Do you wanna delete?");
alertDialog.setIcon(R.drawable.icon);
alertDialog.setMessage("if 'yes' the demand '"
+ dem.getNr_demanda() + "' will be deleted!");
alertDialog.setButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// if yes delete, and REFRESH the screen
DemandaDAO dbHelper;
try {
dbHelper = new DemandaDAO(ctx);
dbHelper.DeleteDem(dem);
} catch (FileNotFoundException e) {
e.printStackTrace();
}}
});
alertDialog.setButton2("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
return;
}
});
alertDialog.show();
}
});

我想在删除需求后刷新 ListView ,但如果我再次在 Activity 中调用该方法,则会导致强制关闭。

最佳答案

调用adapter.notifyDataSetChanged(),它通知附加的View底层数据已更改并且应该刷新自己。

关于java - Android 中如何刷新 ListView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7001245/

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