Android - How to remove list item?

19,731

Solution 1

remove list item, and invoke notifyDataSetChanged();

public View getView(int position, View convertView, ViewGroup parent)
    {
        View row = convertView;     

        if(row == null)
        {
            LayoutInflater inflater = getLayoutInflater();
            row = inflater.inflate(R.layout.custom_list_item, null);
        }

        StringTokenizer tokens = new StringTokenizer(noteList.get(position), ":");
        String first = tokens.nextToken();
        String second = tokens.nextToken();
        row.getTag();
        ((TextView)row.findViewById(R.id.nametv)).setText(first);
        ((EditText)row.findViewById(R.id.result)).setText(second);

        Button deleteButton = (Button) row.findViewById(R.id.button);

        deleteButton.setTag(position);

        deleteButton.setOnClickListener(
                new Button.OnClickListener() {
                    public void onClick(View v) {
                        noteList.remove(position);
                        notifyDataSetChanged();
                    }
                }
            );
        return (row);
    }

Solution 2

Remove the item from the arraylist using

noteList.remove(itemIndex);

and then call

aa.notifyDataSetChanged();
Share:
19,731
Hend
Author by

Hend

Updated on June 04, 2022

Comments

  • Hend
    Hend almost 2 years

    I have got a main extending adapter which adds rows when user clicks on a button. In the adapter, I got a remove item button and a checkbox. How do I remove the item from the list?

    public class main extends Activity{
    
    ArrayList<String> noteList = new ArrayList<String>();
    FancyAdapter aa = null;
    
    Button calculate;
    EditText result;
    String total;
    String name;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        final Spinner spinner = (Spinner)findViewById(R.id.spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.spinner_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
    
        ListView myListView = (ListView)findViewById(R.id.noteList);
        aa = new FancyAdapter();
    
        final EditText price = (EditText)findViewById(R.id.price);
        final EditText name1 = (EditText)findViewById(R.id.name);
        myListView.setAdapter(aa);
    
        myListView.setOnItemClickListener(new OnItemClickListener() {
           public void onItemClick(AdapterView<?> arg0, View arg1,
                     int position, long arg3) {
    
            }
        });
    
        Button btnSimple = (Button)findViewById(R.id.btnSimple);        
    
        btnSimple.setOnClickListener(new OnClickListener() {
            public void onClick(View v)
            {
                try
                {
                double totalPrice = Double.parseDouble(price.getText().toString());
                int position = spinner.getSelectedItemPosition();
                name = name1.getText().toString();
    
                if(position == 0)
                {
                    totalPrice = totalPrice * 1.07;
                    total = String.valueOf(totalPrice);
                    System.out.println(total);
                }
                else
                {
                    totalPrice = (totalPrice * 1.1)*1.07;
                    total = String.valueOf(totalPrice);
                    System.out.println(total);
                }
                String wholeString = name + ":$" +total;
                noteList.add(0, wholeString); 
                System.out.println(total);
                name1.setText("");
                price.setText("");
                aa.notifyDataSetChanged();
                }
                catch (Exception e)
                {
    
                }
            }
        });        
    
    }
    
    class FancyAdapter extends ArrayAdapter<String>
    {
        Button calculate;
        EditText price;
        EditText result;
    
        FancyAdapter()
        {
            super(main.this, android.R.layout.simple_list_item_1, noteList);
        }
    
        public View getView(int position, View convertView, ViewGroup parent)
        {
            View row = convertView;     
    
            if(row == null)
            {
                LayoutInflater inflater = getLayoutInflater();
                row = inflater.inflate(R.layout.custom_list_item, null);
            }
    
            StringTokenizer tokens = new StringTokenizer(noteList.get(position), ":");
            String first = tokens.nextToken();
            String second = tokens.nextToken();
            row.getTag();
            ((TextView)row.findViewById(R.id.nametv)).setText(first);
            ((EditText)row.findViewById(R.id.result)).setText(second);
    
            Button deleteButton = (Button) row.findViewById(R.id.button);
    
            deleteButton.setTag(position);
    
            deleteButton.setOnClickListener(
                    new Button.OnClickListener() {
                        public void onClick(View v) {
    
                        }
                    }
                );
            return (row);
        }
    }
    
    }