【问题标题】:How to refresh a listview in Android?如何在 Android 中刷新列表视图?
【发布时间】:2011-10-23 11:52:37
【问题描述】:

我在活动中使用了这段代码:

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

我想在删除一个需求后刷新列表视图,但是如果我再次在活动中调用该方法会导致强制关闭。

【问题讨论】:

  • 总是尝试发布错误的 logcat 跟踪
  • 请显示 logcat,以便我们了解您在哪里出错以及错误类型。

标签: java android listview


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-11
    • 2011-08-31
    • 2011-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多