How do I delete a certain data from a listView?

2

The app has two buttons that when clicked add an item to a listView (bow and sword), but would like that if the item of each button was already present in the listView, it was 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 up, but I did not find the command for the particular item to be deleted. I looked at this question How do I return a particular character from a text? , it gave me an 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>
    
asked by anonymous 17.02.2017 / 00:55

2 answers

4

The logic is very simple . Just verify that the element you are inserting already exists in your list using the contains() . If it does not exist, you insert it, otherwise you remove it. 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
    
17.02.2017 / 01:16
1

As Ack Lay said, you can use the remove method. But I would just make a change in the algorithm of Ack Lay, would change the if contains by equals, 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 statement, if there is a record called "Arc1" or anything starting with "Arc", the if will return true, and end up undeleting. Equals = equally / equal.

    
18.02.2017 / 01:14