列表视图中的 csv 和复选框将选中的复选框代码保存在 ArrayAdapter 中
csv and checkbox in listview save checked checkboxes code in ArrayAdapter
我在列表视图中使用获取 csv 文件的功能。我已经将其链接到我的 Arrayadapter。我的 xml 里面有 listview 3 textviews for csv 和一个复选框。对于 csv 中的每一行,都会创建一个复选框。我不想在单击时保存复选框状态,并在刷新、关闭等后回调它们。
`这是我的单个列表项
<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" />
那是我的 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;
}
}
您可以创建一个 ArrayList 来保存用户检查的 csv(列表视图的项目)的位置或 ID。(如果您有任何问题,请随时与我联系:nhat.thtb@gmail.com)
例如:
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);
}
我在列表视图中使用获取 csv 文件的功能。我已经将其链接到我的 Arrayadapter。我的 xml 里面有 listview 3 textviews for csv 和一个复选框。对于 csv 中的每一行,都会创建一个复选框。我不想在单击时保存复选框状态,并在刷新、关闭等后回调它们。
`这是我的单个列表项
<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" />
那是我的 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;
}
}
您可以创建一个 ArrayList 来保存用户检查的 csv(列表视图的项目)的位置或 ID。(如果您有任何问题,请随时与我联系:nhat.thtb@gmail.com) 例如:
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);
}