I am using in my listview a get csv file function. I have already linked this to my Arrayadapter. I have inside my xml for listview 3 textviews for the csv and a checkbox. For every line from the csv it will be created a checkbox. I wan't save the checkbox states if clicked and call them back after refresh, closing etc.
`Thats's my single list item
<CheckBox
android:id="@+id/cbSomeCheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
//the 3 textviews are for my csv file
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="LVL"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/score"
app:layout_constraintStart_toEndOf="@+id/cbSomeCheckbox"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/score"
android:layout_width="wrap_content"
android:layout_height="16dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/tvSomeText"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499" />
<TextView
android:id="@+id/tvSomeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
That's my ArrayAdapter
package com.example.customlist;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class ItemArrayAdapter extends ArrayAdapter<String[]> {
private List<String[]> scoreList = new ArrayList<String[]>();
static class ItemViewHolder {
TextView name;
TextView score;
}
public ItemArrayAdapter(Context context, int resource) {
super(context, resource);
}
public void add(String[] object) {
scoreList.add(object);
super.add(object);
}
@Override
public int getCount() {
return this.scoreList.size();
}
@Override
public String[] getItem(int position) {
return this.scoreList.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ItemViewHolder viewHolder;
if(row == null){
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.single_list_item, parent, false);
viewHolder = new ItemViewHolder();
viewHolder.name = (TextView) row.findViewById(R.id.name);
viewHolder.score = (TextView) row.findViewById(R.id.score);
row.setTag(viewHolder);
} else {
viewHolder = (ItemViewHolder) row.getTag();
}
String[] stat = getItem(position);
viewHolder.name.setText(stat[0]);
viewHolder.score.setText(stat[1]);
return row;
}
}
You can create an ArrayList to save position or id of csv (item of listview) that user check.( if you have any issue, feel free contact me: nhat.thtb@gmail.com) Eg:
holder.cbDelete.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (null != mCallBack) {
if(isChecked){
mListIDChecked.add(id);
}else if(mListIDChecked.contains(id)){
mListIDChecked.remove(id);
}
}
}
});
if( mListIDChecked.contains(id)){
holder.cbDelete.setChecked(true);
}else{
holder.cbDelete.setChecked(false);
}