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>