我在列表中使用了一个复选框实现了一个名称,我的目的是选中复选框并获取相应的名称。在这里,我已经在适配器和活动类中编写了代码,但我没有得到任何值,也没有显示任何错误。
这是我的适配器代码..
public Add_member_adapter(Context a, ArrayList<ItemsModel> d) {
context = a;
data = d;
checkedHolder = new boolean[data.size()];
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
if (data.size() <= 0)
return 1;
return data.size();
}
public final Object getItem(int position) {
return data.get(position);
}
public final long getItemId(int position) {
return position;
}
public static class ViewHolder {
public TextView Name, UserId_Fk;
CheckBox chkbox;
}
public View getView(final int position, View convertView, ViewGroup parent) {
Log.d("getview:", "position=" + position);
vi = convertView;
final ViewHolder holder;
if (convertView == null) {
vi = inflater.inflate(R.layout.add_members_list_item, null);
holder = new ViewHolder();
holder.Name = (TextView) vi.findViewById(R.id.Name);
holder.UserId_Fk = (TextView) vi.findViewById(R.id.CusId_Fk);
holder.chkbox = (CheckBox) vi.findViewById(R.id.chkbox);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
if (data.size() <= 0) {
vi.setVisibility(View.GONE);
} else {
tempValues = null;
tempValues = (ItemsModel) data.get(position);
holder.Name.setText(tempValues.getItemName().toString());
Log.i(TAG, "Name " + tempValues.getItemName().toString());
holder.UserId_Fk.setText(tempValues.getUserId_Fk().toString());
holder.chkbox.setChecked(false);
holder.chkbox.setChecked(checkedHolder[position]);
Log.i(TAG, "Position " + checkedHolder[position]);
holder.chkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
checkedHolder[position] = isChecked;
Log.i(TAG, "Position " + checkedHolder[position]);
}
});
}
return vi;
}
活动......
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// String Name = list.getItemAtPosition(position).toString();
// CheckBox cb = (CheckBox) view.findViewById(R.id.chkbox);
// TextView tv = (TextView) view.findViewById(R.id.Name);
for (int i = 0; i < adapter.getCount(); i++) {
if (adapter.checkedHolder[i]) {
// get all name values that checked by user
Name = ((TextView) findViewById(R.id.Name)).getText()
.toString();
Log.i(TAG, "Name " + Name);
}
}
}
});
答案 0 :(得分:0)
将setOnItemClikListener更改为如下
for (int i = 0; i < adapter.getCount(); i++) {
if (adapter.checkedHolder[i]) {
// get all name values that checked by user
Name = data.get(i).getItemName().toString();
Log.i(TAG, "Name " + Name);
}
在getView
relpace holder.chkbox.setChecked(false);
中,位于以下行;
holder.chkbox.setOnCheckedChangeListener(null);