我正在创建一个列表视图,每行都有自定义布局。每个自定义布局都有一个复选框。我有一个复选框,点击它必须单击列表视图中的所有复选框。我能够单击可见行中的复选框,但我遇到不可见行的问题。我尝试检查所有列表适配器但它的不工作。感谢任何帮助。谢谢你...
MainActivity.java
public class MainActivity extends Activity implements OnItemClickListener{
ListView listView;
ArrayAdapter<Model> adapter;
static List<Model> list = new ArrayList<Model>();
public static int count = 0;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.my_list);
adapter = new MyAdapter(this,getModel());
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
count = listView.getCount();
}
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
TextView label = (TextView) v.getTag(R.id.label);
CheckBox checkbox = (CheckBox) v.getTag(R.id.check);
Toast.makeText(v.getContext(), label.getText().toString()+" "+isCheckedOrNot(checkbox), Toast.LENGTH_LONG).show();
}
private String isCheckedOrNot(CheckBox checkbox) {
if(checkbox.isChecked())
return "is checked";
else
return "is not checked";
}
private List<Model> getModel() {
list.add(new Model("Linux"));
list.add(new Model("Windows7"));
list.add(new Model("Suse"));
list.add(new Model("Eclipse"));
list.add(new Model("Ubuntu"));
list.add(new Model("Solaris"));
list.add(new Model("Android"));
list.add(new Model("iPhone"));
list.add(new Model("Java"));
list.add(new Model(".Net"));
list.add(new Model("PHP"));
list.add(new Model("U1"));
list.add(new Model("S1"));
list.add(new Model("A1"));
list.add(new Model("i1"));
list.add(new Model("J1"));
list.add(new Model(".N1"));
list.add(new Model("PP1"));
return list;
}
}
MyAdapter .java
public class MyAdapter extends ArrayAdapter<Model> {
private final List<Model> list;
private final Activity context;
TextView text;
CheckBox checkbox;
boolean flag;
public MyAdapter(Activity context, List<Model> list) {
super(context, R.layout.row, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
final CheckBox c = (CheckBox) ((ViewGroup) parent.getParent()).getChildAt(0);
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.row, null);
}
text = (TextView) convertView.findViewById(R.id.label);
checkbox = (CheckBox) convertView.findViewById(R.id.check);
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
list.get(getPosition).setSelected(buttonView.isChecked());
}
});
checkbox.setTag(position);
text.setText(list.get(position).getName());
c.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
for (int i = 0; i < parent.getChildCount(); i++) {
RelativeLayout r = (RelativeLayout) parent.getChildAt(i);
CheckBox check1 = (CheckBox) r.getChildAt(1);
if(check1 instanceof CheckBox){
check1.setChecked(true);
}
}
}
});
if(flag){
list.get(position).setSelected(flag);
}
checkbox.setChecked(list.get(position).isSelected());
return convertView;
}
}
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<CheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="4dip"
android:layout_marginRight="10dip"
android:focusable="false"
android:focusableInTouchMode="false" >
</CheckBox>
<ListView
android:id="@+id/my_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
row.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" >
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="30sp" >
</TextView>
<CheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="4dip"
android:layout_marginRight="10dip"
android:focusable="false"
android:focusableInTouchMode="false" >
</CheckBox>
</RelativeLayout>
答案 0 :(得分:1)
列表视图中只有您可以看到的子项数,因此getChildCount()
只会返回可见的内容。
您需要在Model
类中包含一个存储其checked
州的字段。因此,在适配器的getView
方法中,您将执行类似
Model myModel = getItem(position);
checkBox.setChecked(myModel.isChecked());
Select all
应遍历列表并将所有Model
个对象标记为已选中,并更新列表视图中的可见视图。下次滚动复选框时,将从相关模型对象中读取其检查状态,从getView()