0

this question is is similar to this * Android - Listview delete item and Refresh and this (the same , but I added the full code here to check if I have any problems in my code):

please give me code example. . .

can i call an intent to refresh my list ?

I cant refresh my adapter with :

adapter.notifyDataSetChanged();

I tried:

adapter.remove(adapter.getItem(pos));

but without success, just one time (weird...).

there is another answer there:

Call that Activity once again Using Intent

sombody can give me the exact code for this (or for the adapter/cursor) ?

I am trying this for a couple of hours without success.

my full code:

protected void onCreate (Bundle SavedInstanceState) {


    super.onCreate(SavedInstanceState);
    setContentView(R.layout.personalmessageview);

    headtitle= getIntent().getExtras().getString("head");

    setTitle(headtitle);


     personalresults = getIntent().getExtras().getStringArrayList("personalres");
     personalresultswithtime = getIntent().getExtras().getStringArrayList("personalrestime"); 



    // setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,personalresults));

     ListView list = (ListView)findViewById(R.id.listview_personal);    
    // ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, personalresults);
     list.setAdapter(adapter);
     registerForContextMenu(list);


     list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
            public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {


                 String time = personalresultswithtime.get(pos).toString();


                    Show_Alert_box(v.getContext(),"Please select action.",time,pos);


                return true;
            }
        });

public void Show_Alert_box(Context context, String message,String time,int position) 
              final String timestamp = time;

              final int pos = position;

             final AlertDialog alertDialog = new  AlertDialog.Builder(context).create();
                alertDialog.setTitle(getString(R.string.app_name));
                alertDialog.setButton("Delete", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {

                        try
                        {
                            db = databaseHelper.getWritableDatabase();


                            db.delete("messages","timestamp" + "=?", new String[] { timestamp }); 

                            Log.d("DB"," delete! ");

                           ArrayAdapter<String> adapter = new ArrayAdapter<String>(PersonalMessageView.this, android.R.layout.simple_list_item_1, personalresults);


                                    adapter.remove(adapter.getItem(pos)); //not working t all! why ?

                            list.notify();
                            list.invalidate();


                            personalresults.remove(pos);
                            personalresultswithtime.remove(pos);

                            adapter.notifyDataSetChanged();

                            db.close();      

                        }
                        catch(Exception e)
                        {

                        }
                } }); 
                alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        alertDialog.dismiss();
                } }); 

                alertDialog.setMessage(message);
                alertDialog.show();
}
Community
  • 1
  • 1
Smoker09
  • 127
  • 1
  • 2
  • 8

2 Answers2

0

instead of using

adapter.remove(adapter.getItem(pos));

use

string str=list.getItemAtPosition(index).toString();
personalresults.remove(str);
adapter.notifyDataSetChanged();
Mark
  • 8,046
  • 15
  • 48
  • 78
waqar
  • 1
0

Inside your onClick of Dialog, you are dealing with an entirely new Adapter.There is no accociation of adapter(inside onClick()) to the listView Either you should say list.setAdapter(adapter); inside the onClick() method or make the adapter global.

Alok Kulkarni
  • 2,153
  • 19
  • 31