I'm trying to use multiple checkboxes through a listView. But only the first checkbox connected to the first element of the array appears to me. The Class:
ArrayList<String> selectedItems = new ArrayList<>();
ListView lvCheckboxList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_site);
lvCheckboxList = (ListView) findViewById(R.id.lvCheckboxList);
lvCheckboxList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
String[] stringa = {"a","b","c"};
ArrayAdapter<String> adapterName = new ArrayAdapter<String>(SingleSiteActivity.this, R.layout.check_list, R.id.checkboxList, stringa);
lvCheckboxList.setAdapter(adapterName);
lvCheckboxList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedItem = ((TextView)view).getText().toString();
if (selectedItems.contains(selectedItem)){
selectedItems.remove(selectedItem);
} else {
selectedItems.add(selectedItem);
}
}
});
The xml file where there is the CheckedTextView consists of these few lines:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/checkboxList"
android:layout_width="match_parent"
android:layout_height="400dp"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:padding="5dp"/>
While the main xml file containing the ListView is this:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.XXXXXXXX">
<ListView
android:id="@+id/lvCheckboxList"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
<Button
android:id="@+id/bSelection"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SELEZIONA"/>
</android.support.constraint.ConstraintLayout>
Aucun commentaire:
Enregistrer un commentaire