CheckBox 在 android 的 ListView 中自动滚动选中
CheckBox checked on scroll automatically in ListView in android
我是 android 的新手,已经实现了带有复选框和文本视图的列表视图,但我遇到了一个奇怪的选择问题。
当我选中任何复选框时,所有其他复选框都会自动选中,并在随机滚动后更改。
任何人都可以帮助我进行哪些更改以在我的代码中解决此问题?
服务适配器
package one.tusk.stush.adapters;
import android.content.Context;
import android.content.Intent;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import com.company.stush.R;
import java.util.ArrayList;
import java.util.HashMap;
import one.tusk.stush.other.Const;
public class ServiceAdapter extends BaseAdapter {
public ArrayList<HashMap<String, String>> contArray;
SparseBooleanArray mArray;
private Context mContext;
String resnID, reson;
Intent i;
public ServiceAdapter(Context paramContext, ArrayList<HashMap<String, String>> contList) {
this.mContext = paramContext;
this.contArray = contList;
mArray = new SparseBooleanArray(contList.size());
}
public int getCount() {
return this.contArray.size();
}
public Object getItem(int paramInt) {
return Integer.valueOf(paramInt);
}
public long getItemId(int paramInt) {
return paramInt;
}
public View getView(final int paramInt, View paramView,
ViewGroup paramViewGroup) {
LayoutInflater localLayoutInflater = (LayoutInflater) this.mContext
.getSystemService("layout_inflater");
Viewholder localViewholder = null;
Const.selectedIDs.clear();
if (paramView == null) {
paramView = localLayoutInflater.inflate(
R.layout.raw_single_contact, paramViewGroup, false);
localViewholder = new Viewholder();
localViewholder.tv_name = ((TextView) paramView
.findViewById(R.id.tv_name));
localViewholder.chk_id = ((CheckBox) paramView
.findViewById(R.id.chk_id));
paramView.setTag(localViewholder);
} else {
localViewholder = (Viewholder) paramView.getTag();
localViewholder.chk_id.setOnCheckedChangeListener(null);
localViewholder.chk_id.setChecked(mArray.get(paramInt));
}
localViewholder.tv_name.setText(contArray.get(paramInt).get("serviceText"));
localViewholder.chk_id.setChecked(mArray.get(paramInt));
//localViewholder.chk_id.setChecked(false);
//localViewholder.chk_id .setTag(paramInt);
localViewholder.chk_id.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
try {
if (isChecked) {
Const.selectedIDs.add(contArray.get(paramInt));
Const.serviceArrayList.get(paramInt).put("flag", "1");
} else {
Const.selectedIDs.remove(contArray.get(paramInt));
Const.serviceArrayList.get(paramInt).put("flag", "0");
}
}catch (Exception e){
e.printStackTrace();
}
}
});
return paramView;
}
static class Viewholder {
TextView tv_name;
CheckBox chk_id;
}
}
使用此
更改您的适配器class代码
public class ServiceAdapter extends BaseAdapter {
public ArrayList<HashMap<String, String>> contArray;
ArrayList<Boolean> checked;
private Context mContext;
String resnID, reson;
Intent i;
public ServiceAdapter(Context paramContext, ArrayList<HashMap<String, String>> contList) {
this.mContext = paramContext;
this.contArray = contList;
checked = new ArrayList<>();
}
public int getCount() {
return this.contArray.size();
}
public Object getItem(int paramInt) {
return Integer.valueOf(paramInt);
}
public long getItemId(int paramInt) {
return paramInt;
}
public View getView(final int paramInt, View paramView,
ViewGroup paramViewGroup) {
LayoutInflater localLayoutInflater = (LayoutInflater) this.mContext
.getSystemService("layout_inflater");
Viewholder localViewholder = null;
Const.selectedIDs.clear();
paramView = localLayoutInflater.inflate(
R.layout.raw_single_contact, paramViewGroup, false);
localViewholder = new Viewholder();
localViewholder.tv_name = ((TextView) paramView
.findViewById(R.id.tv_name));
localViewholder.chk_id = ((CheckBox) paramView
.findViewById(R.id.chk_id));
paramView.setTag(localViewholder);
localViewholder.tv_name.setText(contArray.get(paramInt).get("serviceText"));
localViewholder.chk_id.setChecked(false);
localViewholder.chk_id .setTag(paramInt);
localViewholder.chk_id.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
try {
if (isChecked) {
Const.selectedIDs.add(contArray.get(paramInt));
Const.serviceArrayList.get(paramInt).put("flag", "1");
} else {
Const.selectedIDs.remove(contArray.get(paramInt));
Const.serviceArrayList.get(paramInt).put("flag", "0");
}
}catch (Exception e){
e.printStackTrace();
}
}
});
return paramView;
}
static class Viewholder {
TextView tv_name;
CheckBox chk_id;
}
}
尝试替换这个
localViewholder.chk_id.setChecked(false);
至此
localViewholder.chk_id.setChecked(localViewholder.chk_id.isChecked());
使用SparseBooleanArray
代替ArrayList<Boolean>
SparseBooleanArray mArray;
public ServiceAdapter(Context paramContext, ArrayList<HashMap<String, String>> contList) {
this.mContext = paramContext;
this.contArray = contList;
mArray = new SparseBooleanArray(data.size());
}
里面 getView()
if (convertView == null) {
localViewholder = new Holder();
convertView = inflater.inflate(R.layout.item1, null);
....
....
convertView.setTag(localViewholder );
} else {
localViewholder = (Viewholder) convertView.getTag();
localViewholder.chk_id.setOnCheckedChangeListener(null);
localViewholder.chk_id.setChecked(mArray.get(position));
}
localViewholder.chk_id.setChecked(mArray.get(position));
localViewholder.chk_id.setOnCheckedChangeListener(new
OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
mArray.put(paramInt, isChecked);
if (isChecked) {
Const.selectedIDs.add(isChecked);
Const.serviceArrayList.get(paramInt).put("flag", "1");
} else {
Const.selectedIDs.remove(contArray.get(isChecked));
Const.serviceArrayList.get(paramInt).put("flag", "0");
}
}
});
快乐编码..
我是 android 的新手,已经实现了带有复选框和文本视图的列表视图,但我遇到了一个奇怪的选择问题。
当我选中任何复选框时,所有其他复选框都会自动选中,并在随机滚动后更改。
任何人都可以帮助我进行哪些更改以在我的代码中解决此问题?
服务适配器
package one.tusk.stush.adapters;
import android.content.Context;
import android.content.Intent;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import com.company.stush.R;
import java.util.ArrayList;
import java.util.HashMap;
import one.tusk.stush.other.Const;
public class ServiceAdapter extends BaseAdapter {
public ArrayList<HashMap<String, String>> contArray;
SparseBooleanArray mArray;
private Context mContext;
String resnID, reson;
Intent i;
public ServiceAdapter(Context paramContext, ArrayList<HashMap<String, String>> contList) {
this.mContext = paramContext;
this.contArray = contList;
mArray = new SparseBooleanArray(contList.size());
}
public int getCount() {
return this.contArray.size();
}
public Object getItem(int paramInt) {
return Integer.valueOf(paramInt);
}
public long getItemId(int paramInt) {
return paramInt;
}
public View getView(final int paramInt, View paramView,
ViewGroup paramViewGroup) {
LayoutInflater localLayoutInflater = (LayoutInflater) this.mContext
.getSystemService("layout_inflater");
Viewholder localViewholder = null;
Const.selectedIDs.clear();
if (paramView == null) {
paramView = localLayoutInflater.inflate(
R.layout.raw_single_contact, paramViewGroup, false);
localViewholder = new Viewholder();
localViewholder.tv_name = ((TextView) paramView
.findViewById(R.id.tv_name));
localViewholder.chk_id = ((CheckBox) paramView
.findViewById(R.id.chk_id));
paramView.setTag(localViewholder);
} else {
localViewholder = (Viewholder) paramView.getTag();
localViewholder.chk_id.setOnCheckedChangeListener(null);
localViewholder.chk_id.setChecked(mArray.get(paramInt));
}
localViewholder.tv_name.setText(contArray.get(paramInt).get("serviceText"));
localViewholder.chk_id.setChecked(mArray.get(paramInt));
//localViewholder.chk_id.setChecked(false);
//localViewholder.chk_id .setTag(paramInt);
localViewholder.chk_id.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
try {
if (isChecked) {
Const.selectedIDs.add(contArray.get(paramInt));
Const.serviceArrayList.get(paramInt).put("flag", "1");
} else {
Const.selectedIDs.remove(contArray.get(paramInt));
Const.serviceArrayList.get(paramInt).put("flag", "0");
}
}catch (Exception e){
e.printStackTrace();
}
}
});
return paramView;
}
static class Viewholder {
TextView tv_name;
CheckBox chk_id;
}
}
使用此
更改您的适配器class代码 public class ServiceAdapter extends BaseAdapter {
public ArrayList<HashMap<String, String>> contArray;
ArrayList<Boolean> checked;
private Context mContext;
String resnID, reson;
Intent i;
public ServiceAdapter(Context paramContext, ArrayList<HashMap<String, String>> contList) {
this.mContext = paramContext;
this.contArray = contList;
checked = new ArrayList<>();
}
public int getCount() {
return this.contArray.size();
}
public Object getItem(int paramInt) {
return Integer.valueOf(paramInt);
}
public long getItemId(int paramInt) {
return paramInt;
}
public View getView(final int paramInt, View paramView,
ViewGroup paramViewGroup) {
LayoutInflater localLayoutInflater = (LayoutInflater) this.mContext
.getSystemService("layout_inflater");
Viewholder localViewholder = null;
Const.selectedIDs.clear();
paramView = localLayoutInflater.inflate(
R.layout.raw_single_contact, paramViewGroup, false);
localViewholder = new Viewholder();
localViewholder.tv_name = ((TextView) paramView
.findViewById(R.id.tv_name));
localViewholder.chk_id = ((CheckBox) paramView
.findViewById(R.id.chk_id));
paramView.setTag(localViewholder);
localViewholder.tv_name.setText(contArray.get(paramInt).get("serviceText"));
localViewholder.chk_id.setChecked(false);
localViewholder.chk_id .setTag(paramInt);
localViewholder.chk_id.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
try {
if (isChecked) {
Const.selectedIDs.add(contArray.get(paramInt));
Const.serviceArrayList.get(paramInt).put("flag", "1");
} else {
Const.selectedIDs.remove(contArray.get(paramInt));
Const.serviceArrayList.get(paramInt).put("flag", "0");
}
}catch (Exception e){
e.printStackTrace();
}
}
});
return paramView;
}
static class Viewholder {
TextView tv_name;
CheckBox chk_id;
}
}
尝试替换这个
localViewholder.chk_id.setChecked(false);
至此
localViewholder.chk_id.setChecked(localViewholder.chk_id.isChecked());
使用SparseBooleanArray
代替ArrayList<Boolean>
SparseBooleanArray mArray;
public ServiceAdapter(Context paramContext, ArrayList<HashMap<String, String>> contList) {
this.mContext = paramContext;
this.contArray = contList;
mArray = new SparseBooleanArray(data.size());
}
里面 getView()
if (convertView == null) {
localViewholder = new Holder();
convertView = inflater.inflate(R.layout.item1, null);
....
....
convertView.setTag(localViewholder );
} else {
localViewholder = (Viewholder) convertView.getTag();
localViewholder.chk_id.setOnCheckedChangeListener(null);
localViewholder.chk_id.setChecked(mArray.get(position));
}
localViewholder.chk_id.setChecked(mArray.get(position));
localViewholder.chk_id.setOnCheckedChangeListener(new
OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
mArray.put(paramInt, isChecked);
if (isChecked) {
Const.selectedIDs.add(isChecked);
Const.serviceArrayList.get(paramInt).put("flag", "1");
} else {
Const.selectedIDs.remove(contArray.get(isChecked));
Const.serviceArrayList.get(paramInt).put("flag", "0");
}
}
});
快乐编码..