cesar
cesar

Reputation: 1

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.

Upvotes: 0

Views: 2861

Answers (1)

varun bhardwaj
varun bhardwaj

Reputation: 1522

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

Upvotes: 2

Related Questions