简体   繁体   中英

How to refresh a listview in Android?

I'm using this pieces of code, in the 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;
}

and in the adapter:

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

I want to refresh the listview after deleting a demand, but it results in a forceclose if I call the method in activity again.

call adapter.notifyDataSetChanged(), it Notifies the attached View that the underlying data has been changed and it should refresh itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM