我在网上搜索了近2个小时,但找不到任何适合我的东西。
简单,我有一个带有列表视图的Android应用程序,其中包含复选框。
现在,当我点击一个按钮时,我想要的就是检查一个复选框(并且这在屏幕上可见) 无论我到目前为止尝试了什么,都不会检查复选框。
那是我的代码:
public class MainActivity extends Activity {
SparseArray<Group> groups = new SparseArray<Group>();
List<String> checkedMedia = new ArrayList<String>();
ExpandableListView listView;
MyExpandableListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createData();
listView = (ExpandableListView) findViewById(R.id.listView);
adapter = new MyExpandableListAdapter(this,
groups);
listView.setAdapter(adapter);
listView.expandGroup(0);
}
public void onCheckboxClicked(View view) {
CheckBox checkBox = (CheckBox) view;
boolean checked = checkBox.isChecked();
View chBoxParent = (View) checkBox.getParent();
TextView textView = (TextView) chBoxParent.findViewById(R.id.textView1);
String medium = textView.getText().toString();
if (checked) {
Toast.makeText(this, "checked: " + medium, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "unchecked: " + medium, Toast.LENGTH_SHORT).show();
}
}
public void onButtonClicked(View view) {
View view0 = adapter.getChildView(0, 0, false, null, null);
CheckBox cb0 = (CheckBox) view0.findViewById(R.id.checkBox);
cb0.setChecked(true);
cb0.setWillNotDraw(false);
cb0.invalidate();
Toast.makeText(this, "button clicked", Toast.LENGTH_SHORT).show();
}
public void createData() {
Group group = new Group("Test ");
for (int i = 0; i < 5; i++) {
group.children.add("Sub Item" + i);
}
groups.append(0, group);
}
}
答案 0 :(得分:4)
您可以像这样以编程方式检查复选框...
cb0.post(new Runnable() {
@Override
public void run() {
cb0.setChecked(true);
}
});