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?
– viana
No, by clicking the button, if it (item )already exists in the listview.
– Boneco Sinforoso
Puts your XML activity_main
– viana
Did you see the answer?! Did you solve there?!
– viana
Yes, thank you very much!!!
– Boneco Sinforoso