2

嗨,我想在 Android 应用程序中创建动态数量的复选框(这取决于数组“流派”的长度),然后我做了:

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
      ...
4

3 回答 3

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);
}

这样您就不必自己手动创建复选框

于 2012-11-20T13:56:58.037 回答
0

因为这条线

 c.setText(genre[i]);
于 2012-11-20T13:57:43.807 回答
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.
于 2015-11-05T15:03:53.617 回答