您好我想在Android应用中创建动态数量的复选框(这取决于数组“genre”的长度)然后我做了:
CheckBox [] checkbox;
String [] genre={"party","sport","music","café","education"};
@Override
protected void onCreate(Bundle savedInstanceState) {
checkbox=new CheckBox[genre.length];
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbox);
for(int i=0;i<genre.length;i++){
CheckBox c=new CheckBox(this);
c.setText(genre[i]);
checkbox[i]=c;
}
ArrayAdapter<CheckBox>adapter= new ArrayAdapter<CheckBox>(this, android.R.layout.simple_list_item_1,checkbox);
ListView lv= (ListView)findViewById(R.id.genre_list);
lv.setAdapter(adapter);
}
但是在列表视图中它显示了内存中复选框的数量(我猜),为什么?
like : Android.wiget.CheckBox@44f03160
Android.wiget.CheckBox@44f03d60
...
答案 0 :(得分:0)
您宁愿使用android.R.simple_list_item_checked
项目布局并在列表中启用多项选择:
String [] genre={"party","sport","music","café","education"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbox);
ArrayAdapter<String> adapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, genre);
ListView lv= (ListView)findViewById(R.id.genre_list);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setAdapter(adapter);
}
这样您就不必自己手动创建复选框
答案 1 :(得分:0)
因为这行
c.setText(genre[i]);
答案 2 :(得分:0)
String [] genre={"party","sport","music","café","education"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbox);
ArrayAdapter<String> adapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, genre);
ListView lv= (ListView)findViewById(R.id.genre_list);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setAdapter(adapter);
}
This looks like checkboxes.