我是android的新手并使用checkbox和textViews实现了一个listView,我面临一个奇怪的选择问题。
当我选中任何复选框时,会自动选中所有其他复选框,并在随机滚动后更改。
有人可以帮助我在我的代码中修改这个更改吗?
ServiceAdapter
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;
}
}
答案 0 :(得分:1)
使用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");
}
}
});
快乐编码..
答案 1 :(得分:0)
使用此
更改适配器类代码 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;
}
}
答案 2 :(得分:0)
尝试替换此
localViewholder.chk_id.setChecked(false);
到这个
localViewholder.chk_id.setChecked(localViewholder.chk_id.isChecked());