我是android的新手并且在listView上工作,其中包含项目中的复选框,当我检查任何项目时其他项目也自动cehcked,任何人都可以帮助我如何解决它我的适配器如下,
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();
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);
paramView.setTag(R.id.chk_id, localViewholder.chk_id );
} else {
localViewholder = (Viewholder) paramView.getTag();
}
localViewholder.tv_name.setText(contArray.get(paramInt).get("serviceText"));
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 :(得分:0)
检查此代码,这对您有帮助,
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.country_info, null);
holder = new ViewHolder();
holder.code = (TextView) convertView.findViewById(R.id.code);
holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(holder);
holder.name.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
Country country = (Country) cb.getTag();
Toast.makeText(getApplicationContext(),
"Clicked on Checkbox: " + cb.getText() +
" is " + cb.isChecked(),
Toast.LENGTH_LONG).show();
country.setSelected(cb.isChecked());
}
});
}
else {
holder = (ViewHolder) convertView.getTag();
}
Country country = countryList.get(position);
holder.code.setText(" (" + country.getCode() + ")");
holder.name.setText(country.getName());
holder.name.setChecked(country.isSelected());
holder.name.setTag(country);
return convertView;
}
}
private void checkButtonClick() {
Button myButton = (Button) findViewById(R.id.findSelected);
myButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer responseText = new StringBuffer();
responseText.append("The following were selected...\n");
ArrayList<Country> countryList = dataAdapter.countryList;
for(int i=0;i<countryList.size();i++){
Country country = countryList.get(i);
if(country.isSelected()){
responseText.append("\n" + country.getName());
}
}
Toast.makeText(getApplicationContext(),
responseText, Toast.LENGTH_LONG).show();
}
});
}
答案 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;
//update code there make list of same size as contList
checked = new ArrayList<>(contList.size());
//On start all are unchecked
Collections.fill(checked, Boolean.FALSE);
}
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);
// dont need below line try without below line if it works comment it otherwise let it stay there.
paramView.setTag(R.id.chk_id, localViewholder.chk_id);
} else {
localViewholder = (Viewholder) paramView.getTag();
}
localViewholder.tv_name.setText(contArray.get(paramInt).get("serviceText"));
localViewholder.chk_id.setChecked(checked.get(paramInt));
localViewholder.chk_id.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
try {
if (isChecked) {
checked.set(paramInt, true);
Const.selectedIDs.add(contArray.get(paramInt));
Const.serviceArrayList.get(paramInt).put("flag", "1");
} else {
checked.set(paramInt, false);
Const.selectedIDs.remove(contArray.get(paramInt));
Const.serviceArrayList.get(paramInt).put("flag", "0");
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
return paramView;
}
public class Viewholder {
TextView tv_name;
CheckBox chk_id;
}
}