How to delete a particular data from a listview?

Asked

Viewed 611 times

2

The app has two Uttons that when clicked add an item to a listview (bow and sword), but I would like if the item of each button was already present in the listview, the same would be deleted.

package genesysgeneration.list;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btnArco, btnEspada;
    private ListView lvItens;
    private ArrayList<String> itens;
    private ArrayAdapter<String> itensAdapter;
    private int cont01, cont02;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cont01=0;
        cont02=0;

        lvItens=(ListView)findViewById(R.id.lvItens);
        btnArco=(Button)findViewById(R.id.btnArco);
        btnEspada=(Button)findViewById(R.id.btnEspada);

        btnArco.setOnClickListener(this);
        btnEspada.setOnClickListener(this);

        itens = new ArrayList<String>();
        itensAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, itens);
        lvItens.setAdapter(itensAdapter);

    }

    public void onClick(View v){

        switch (v.getId()){

            case R.id.btnArco:

                if (cont01==0){

                    cont01=1;
                    itens.add("Arco");
                    itensAdapter.notifyDataSetChanged();

                }else {

                    cont01=0;
                    //apagar "arco"

                }

                break;

            case R.id.btnEspada:

                if (cont02==0){

                    cont02=1;
                    itens.add("Espada");
                    itensAdapter.notifyDataSetChanged();

                }else {

                    cont02=0;
                    //apagar "Espada"

                }

                break;

        }

    }

}

I used an int variable, because it was the reasoning that came to me, but I could not find the command to delete the specific item. I looked into that question How do I return a certain character of a text?, she gave me the idea of a getPosition, but it’s in LUA.

xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="genesysgeneration.list.MainActivity">

    <Button
        android:text="Espada"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/btnEspada" />

    <Button
        android:text="Arco"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/btnEspada"
        android:layout_toEndOf="@+id/btnEspada"
        android:id="@+id/btnArco" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnEspada"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="83dp"
        android:id="@+id/lvItens" />
</RelativeLayout>
  • You want to delete the item clicked on the listview?

  • No, by clicking the button, if it (item )already exists in the listview.

  • Puts your XML activity_main

  • Did you see the answer?! Did you solve there?!

  • 1

    Yes, thank you very much!!!

2 answers

5


The logic is very simple. Just check that the element you are inserting already exists in your list using the method contains(). If it does not exist, you enter, otherwise you remove. See the example for the first case:

if (itens.contains("Arco"))          // verifica se existe
    itens.remove("Arco");            // remove     
else itens.add("Arco");              // adiciona
itensAdapter.notifyDataSetChanged(); // atualiza o adaptador caso haja mudanças

1

Like Ack Lay said, you can use the method remove. However I would only make a change to Ack Lay’s algorithm, swap if contains for equais, as an example:

if  (itens.equals("Arco"))          // verifica se existe
    itens.remove("Arco");            // remove     
else 
    itens.add("Arco");              // adiciona

itensAdapter.notifyDataSetChanged(); // atualiza o adaptador caso haja mudanças

With the contains command, if there is a record called "Arco1" or anything that starts with "Arco" if will return true, and end up removing improperly. Equals = equal/equal.

  • 1

    Peterr,itens is a ArrayList and cannot compare whether a ArrayList is equal to a string, using the method equals(). The right logic is, you compare array with array and string with string. The method contains(), as per itself, checks whether it already contains an item with this name within the list.

  • Here it did not work with ". equals". Kept adding repeatedly and did not delete the existing ones.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.