Deleting items from a ListView using a custom BaseAdapter

后端 未结 3 1560
日久生厌
日久生厌 2021-02-06 05:17

I am using a customised BaseAdapter to display items on a ListView. The items are just strings held in an ArrayList.

The list items have a delete button on them (big red

3条回答
  •  深忆病人
    2021-02-06 05:43

    My final solution was to use the accepted answer by Greg and the following:

    • Store the holders in a HashMap, with the item positions as the keys (this is initialised as empty in the constructor)

      private HashMap mHolders;

    • Use this as the onClickListener method:

    public void onClick(View v) {
        ViewHolder deleteHolder = (ViewHolder) v.getTag();
        int pos = deleteHolder.position;
        mHolders.remove(pos);

        ViewHolder currentHolder;
    
        // Shift 'position' of remaining languages 
        // down since 'pos' was deleted
        for(int i=pos+1; i

    [Please excuse the weird formatting. The code embedding isn't working properly]

提交回复
热议问题