我使用的列表视图具有自定义 xml 和适配器。我想从任何地方从此列表视图中获取选中的项目。我的xml在这里:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="15dp"
android:paddingBottom="30dp"
android:focusable="false"
android:focusableInTouchMode="false" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textStyle="bold"
android:paddingTop="15dp"
android:paddingRight="15dp"
android:paddingLeft="50dp"
android:text="Item" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:paddingRight="15dp"
android:paddingBottom="15dp"
android:paddingLeft="50dp"
android:text="Subtitle"
android:layout_below="@+id/text" />
</RelativeLayout>
我的适配器在这里:
private final List<Model> list;
private final List<Model> list2;
private final Activity context;
boolean checkAll_flag = false;
boolean checkItem_flag = false;
public Adapter(Activity context, List<Model> list, List<Model> list2) {
super(context, R.layout.listview, list);
this.context = context;
this.list = list;
this.list2 = list2;
}
static class ViewHolder {
protected TextView text;
protected TextView text2;
protected CheckBox checkbox;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.listview, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.text);
viewHolder.text2 = (TextView) convertView.findViewById(R.id.text2);
viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.cb);
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
list.get(getPosition).sec(buttonView.isChecked());
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.text, viewHolder.text);
convertView.setTag(R.id.text2, viewHolder.text2);
convertView.setTag(R.id.cb, viewHolder.checkbox);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkbox.setTag(position);
viewHolder.text.setText(list.get(position).isimGetir());
viewHolder.text2.setText(list2.get(position).isimGetir());
viewHolder.checkbox.setChecked(list.get(position).secilimi());
return convertView;
}
我的目标是从放置在主要活动中的方法中获取检查项目。但是我无法捕捉复选框的值,因为它们是自定义的。