我有一个带有自定义数组适配器的listview,我正在试图弄清楚如何保持检查位置的状态。我想在每次检查项时将复选框位置保存到int数组,然后在getView中如果数组包含它将设置的数字isChecked()== true。但是,我真的不知道如何搜索数组。任何帮助都会很棒!这是适配器上的一些基本代码:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Contact c = (Contact) getItem(position);
final ViewHolder viewHolder = new ViewHolder();
// if the array item is null, nothing to display, just return null
if (c == null) {
return null;
}
LayoutInflater inflater = (LayoutInflater)
getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TwoLineListItem view;
if (convertView == null) {
view = (TwoLineListItem) inflater.inflate(resourceId, parent, false);
} else {
view = (TwoLineListItem) convertView;
}
final int pos = position;
final SharedPreferences myprefs = PreferenceManager.getDefaultSharedPreferences(getContext());
final SharedPreferences.Editor editor = myprefs.edit();
viewHolder.checkbox = (CheckBox)view.findViewById(R.id.checkbox);
viewHolder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.isChecked()){
contactInfo.add(pos+","+c.getName()+","+c.getNumber());
StringBuilder sb = new StringBuilder();
for (String s : contactInfo)
{
sb.append(s);
sb.append("\t");
}
editor.putString("contacts", sb.toString()).commit();
}else {
contactInfo.remove(pos+","+c.getName()+","+c.getNumber());
StringBuilder sb = new StringBuilder();
for (String s : contactInfo)
{
sb.append(s);
sb.append("\t");
}
editor.putString("contacts", sb.toString()).commit();
}
}
});
答案 0 :(得分:0)
CheckBox check =(CheckBox)v.findViewById(R.id.check);
check.setTag(new Integer(childPosition));
check.setChecked(listEditable.get(childPosition).isSelected());
check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
Integer myPosition=(Integer)buttonView.getTag();
if ( isChecked )
{
listEditable.get(myPosition).setSelected(true);
}
else
{
listEditable.get(myPosition).setSelected(false);
}
}
});