下面的 ActivityContactMailAdapter.class
是我的包含复选框的列表视图适配器。如果我想获取已检查的行的位置。如何才能做到这一点。我的列表中有几个复选框,我可以选中多个复选框。我的逻辑是我应该存储已检查的每个位置。然后从我的数组列表中调用数据,但现在我无法从我的适配器中获取选中项目的位置。
public class ContactMailAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<Destinataire> data;
private static LayoutInflater inflater = null;
ViewHolder holder;
static String src;
PopMessage main;
public ContactMailAdapter(Activity a, ArrayList<Destinataire> mArticles) {
activity = a;
data = mArticles;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public ContactMailAdapter (PopMessage m){
this.main=m;
}
@Override
public int getCount() {
return data.toArray().length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
public TextView one;
public TextView two;
public CheckBox check;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null) {
vi = inflater.inflate(R.layout.popise_item, null);
holder = new ViewHolder();
holder.one = (TextView) vi.findViewById(R.id.title_top);
holder.two = (TextView) vi.findViewById(R.id.title_bottom);
holder.check = (CheckBox)vi.findViewById(R.id.search_imag);
vi.setTag(holder);
} else
holder = (ViewHolder) vi.getTag();
holder.one.setText(data.get(position).getName());
holder.two.setText(data.get(position).getEmail());
vi.findViewById(R.id.search_imag).setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (holder.check.isChecked())
System.out.println("false"+v.getId());
else
System.out.println("false");
}
});
holder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
}
});
/* vi.findViewById(R.id.search_image).setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
v.findViewById(R.id.search_image).setBackgroundResource(R.drawable.boutton_off);
if (holder.check.isChecked())
System.out.println("true");
else
System.out.println("false");
}
}); */
return vi;
}
}
新更新
ids = new ArrayList<String>();
if (ids.contains(holder.check.getId() + "")) {
holder.check.setChecked(true);
} else {
holder.check.setChecked(false);
}
holder.check
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
int id = buttonView.getId();
if (isChecked) {
if (ids.contains(id + "")) {
} else {
ids.add(id + "");
id++;
System.out.println( "receipe" + id);
}
} else {
if (ids.contains(id + "")) {
ids.remove(id + "");
System.out.println( "receipe" + id);
// ids--;
} else {
// id = 0;
}
}
}
});