How to remove an item from my custom base adapter?

I am expanding the BaseAdapter to create a custom list string. I have a context menu that opens every time a user holds on a line and asks if he wants to delete it. However, how to delete a row? A hashmap is just test data.

private MyListAdapter myListAdapter; private ArrayList<HashMap<String, String>> items; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); items = new ArrayList<HashMap<String,String>>(); HashMap<String, String> map1 = new HashMap<String, String>(); map1.put("date", "10/09/2011"); map1.put("distance", "309 km"); map1.put("duration", "1t 45min"); items.add(map1); myListAdapter = new MyListAdapter(this, items); setListAdapter(myListAdapter); getListView().setOnCreateContextMenuListener(this); } private class MyListAdapter extends BaseAdapter { private Context context; private ArrayList<HashMap<String, String>> items; public MyListAdapter(Context context, ArrayList<HashMap<String, String>> items) { this.context = context; this.items = items; } @Override public int getCount() { return items.size(); } @Override public Object getItem(int position) { return items.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = convertView; if (view == null) { LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = layoutInflater.inflate(R.layout.row_log, null); } TextView rowLogOverview = (TextView) view.findViewById(R.id.rowLogOverview); HashMap<String, String> item = items.get(position); rowLogOverview.setText(item.get("date")); return view; } } 
+8
source share
4 answers

You do not remove the adapter! You remove from items! and the adapter is between your goods and the view. In terms of position, you can get a position in accordance with a position that you can delete. The adapter will then update your views.

That means you need to do something like this.

  items.remove(position); adapter.notifyDataSetChanged() 
+15
source

To remove, you need to do 2 things:

  • Call .remove() on the ArrayList (elements).
  • Call .notifyDataSetChanged() on an instance of your MyListAdapter ( mListAdapter ) class.
+10
source
  • remove item from items
  • calling BaseAdapter.notifyDataSetChanged() . Then the listview will be redrawn, and the target line will be removed from the screen.
+1
source

You must add a listener to your adapter to handle the delete event.

 public YourAdapter(Context context, List<T> rows, View.OnClickListener deleteListener) { ... } 

And in your getView () method, set a listener

 yourBtn.setOnClickListener(this.deleteListener); 

You can add a value to the btn tag to identify the current line:

 yourBtn.setTag(position); 

Finally, in your activity, your listener will shoot with the current position in the tag. You can then use the previous answer to update your adapter and update the list.

0
source

All Articles