How do I delete an item from my custom base adapter?

I am extending BaseAdapter to make a custom listview row. I have context menu that opens everytime a user holds on the row and prompts if he wants to delete it. However how do I remove the row? The hashmap is only 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;
    }
}

You do not delete from the adapter ! You delete from the items ! and the adapter is between your items and the view. From the view you can get the position and according the position you can delete items. Then the adapter will refresh you views.

That means you need to do something like this

 items.remove(position);
adapter.notifyDataSetChanged()

Remove row from listview using custom baseAdapter, This video is a sequel to the previous video I made on how to create a Custom ListView Adapter.Duration: 4:26 Posted: Mar 12, 2013 To delete an item from your computer, you must delete it from My Collection. NOTE: Some Garmin devices do not support the deletion of data using the BaseCamp application. Select one or more items (Selecting Multiple Items). Select Edit > Delete.

To delete, you'll need to do 2 things:

  1. Call .remove() on your ArrayList (items).
  2. Call .notifyDataSetChanged() on the instance of your MyListAdapter class (mListAdapter).

Android - How to remove an item from a Custom ListView, This example show how to add/remove items to/from custom ListView, using Long click item in ListView1 (left), the item will be removed from ListView1 extends BaseAdapter { private Context context; private List<Item> list;  ListView is a ViewGroup that displays a list of vertically scrollable items. The list items are automatically inserted into the list using an adapter that is connected to a source, such as an array or a database query, and each item is converted into a row in the ListView.

  1. remove item from items
  2. call BaseAdapter.notifyDataSetChanged(). Then listview will be redrawn and target row will be removed from screen.

Add/Remove items to custom ListView programmatically, The list items are automatically inserted into the list using an adapter that is our custom BaseAdapter implementation, we need to create the layout for the  ListView Tutorial With Example In Android Studio. List of scrollable items can be displayed in Android using ListView. It helps you to displaying the data in the form of a scrollable list. Users can then select any list item by clicking on it. ListView is default scrollable so we do not need to use scroll View or anything else with ListView.

You should add a Listener on your adapter to handle the delete event.

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

And on your getView() method set the listener

yourBtn.setOnClickListener(this.deleteListener);

You can add a value on the btn tag to identifiy the current row :

yourBtn.setTag(position);

Finally, on your Activity, your listener will fire with the current position in tag. You can then use the previous answer to update your adapter and refresh your listview.

Using a BaseAdapter with ListView, In your BaseAdapter sub class within getView() method perform the delete operation. public View getView(final int position, View view,  Here is code snippet of the MovieAdapter.class file : Add a ListView in the appropriate layout where you want to show your list. After creating the Custom Adapter you need a ListView which will show the adapter contents. So in the layout file of you Activity (in this case MainActivity) add a <ListView> tag.

Android ListView delete item and refresh, recycler.layoutManager = GridLayoutManager(this, 2) recycler.adapter = MyAdapter(items) { toast("${it.title} Clicked") } The final function is listener, which receives an item. The code will simply print the title of the item you click on. Conclusion. As simple as that is to implement a RecyclerView Adapter in Kotlin.

After that no new row items are created in the memory. As the user scrolls through the list, items that leave the screen are kept in memory for later use and then every new row that enters the screen reuses an older row kept in the memory. Creating a View template. Let’s create a xml layout that presents the items in a row in a customised way.

Custom ArrayAdapter Tutorial With Example In Android Studio ArrayAdapter is a type of Adapter which acts a bridge between UI component and data source that helps us to fill data in UI component. It expects a Layout with a single TextView and for more customization in grid items or list items, we use custom adapters.

Comments
  • calling adapter.notifyDataSetChanged() to the adapter bound to items will update the listView.