Listview delete and Refresh element - android

this question is similar to this - android - delete list item and update .

I cannot update my adapter using

adapter.notifyDataSetChanged(); 

I tried:

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

but without success, just once (weird ...).

there is another answer:

 Call that Activity once again Using Intent 

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

I have been trying this for several hours without success.

my full code is:

 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) end timestamp of the string = 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(); 

}

+8
java android
source share
9 answers

Try

 adapter.remove(adapter.getItem(pos)); notifyDataSetChanged(); 
+6
source share

You probably decided to solve it, but just in case someone has a different problem, here is my solution:

 ArrayAdapter<String> myAdapter = (ArrayAdapter<String>)getListView().getAdapter(); myAdapter.remove(myAdapter.getItem(info.position)); myAdapter.notifyDataSetChanged(); 

The problem was that you did not have an adapter for your list.

+6
source share

Replace the list view after changing the data; inside your ListActivity, use the following lines when changing the data.

  getListView().invalidate(); 
+3
source share

create a function to bind your adapter to the Listview list, and simply call this function again when the deletion is complete, so that the Listview populates again and you get an updated list.
Do you use a database?

+2
source share

try using listview.invalidateViews(); after adapter.remove(adapter.getItem(pos));

+2
source share

you need to update the changes in your database, then you update your arraylist (with a requirement or something like, since it is deprecated) then you need to call adapter.notifyDataSetChanged ();

+2
source share
  public class Listview_cls extends Activity implements OnItemClickListener{ ListView lv; String items[]= {"Kolkata","Delhi","Mumbai","Pune"}; ArrayAdapter adp; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); arr = new ArrayList(); lv = (ListView) findViewById (R.id.ListView01); for(int i=0; i<items.length; i++) { arr.add(items[i]); } adp = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr); lv.setAdapter(adp); lv.setOnItemClickListener(this); } public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) { adp.remove(adp.getItem(arg2)); adp.notifyDataSetChanged(); } } 
0
source share
 @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); int position = info.position; favouriteReportList d; d= array_list.get(position); switch (item.getItemId()) { case R.id.connect: return true; case R.id.mark: return true; case R.id.delete: myFavReport = new DBhandlerReport(this); myFavReport.deleteMyFavReport(d.getReportName(), d.getPathName(), myUrl); myFavReport.close(); // arrarAdapter is an object of your class. arrayAdapter.remove(arrayAdapter.getItem(info.position)); arrayAdapter.notifyDataSetChanged(); return true; default: return super.onContextItemSelected(item); } } 
0
source share

Try

 Intent intent= getIntent(); finish(); startActivity(intent); 
-one
source share

All Articles