如何在android中动态添加格式视图中的Checkbox

时间:2014-01-22 10:36:14

标签: android gridview checkbox

大家好,             我想在android中的gridview中添加复选框。我有一个绑定gridview的列表,根据条件我必须动态添加gridview中的复选框。我不想得到任何检查项目。如何动态显示gridview内的复选框..我使用了相对布局。我是android应用程序的新手。请告诉解决方案。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginRight="10dp"
    android:layout_marginTop="20dp"
    android:layout_toLeftOf="@+id/spinner1"
    android:text="@string/Input_Label" />

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="3dp" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_alignTop="@+id/spinner2"
    android:layout_marginTop="5dp"
    android:text="@string/Input_Label1" />

<Spinner
    android:id="@+id/spinner2"
    android:layout_width="125dp"
    android:layout_height="50dp"
    android:layout_alignLeft="@+id/spinner1"
    android:layout_below="@+id/spinner1"
    android:layout_marginTop="10dp" />




<Button
    android:id="@+id/button1"
    android:layout_width="80dp"
    android:layout_height="50dp"
    android:layout_alignTop="@+id/spinner2"
    android:layout_toRightOf="@+id/spinner2"
    android:text="@string/Input_btn" />

<GridView
    android:id="@+id/gridView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/spinner2"
    android:layout_marginTop="30dp"
    android:layout_toRightOf="@+id/textView2"
    android:numColumns="3" >

</GridView>

**MainActivity.java**

公共类MainActivity扩展了Activity {

private Spinner spinner1, spinner2;
GridView grid;

static final String[] letters = new String[] {

    "A", "B", "C", "D", "E",

    "F", "G", "H", "I", "J",

    "K", "L", "M", "N", "O",

    "P", "Q", "R", "S", "T",

    "U", "V", "W", "X", "Y", "Z"};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    addItemsOnSpinner1();
    addItemsOnSpinner2();

    Button next = (Button) findViewById(R.id.button1);

    grid = (GridView) findViewById(R.id.gridView1);
    //simple_list_item_multiple_choice
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, letters);

    grid.setAdapter(adapter);
}

public void addItemsOnSpinner1() {

    spinner1 = (Spinner) findViewById(R.id.spinner1);
    List<String> list = new ArrayList<String>();
    list.add("Select");
    list.add("SCUF");
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_spinner_item, list);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(dataAdapter);
  }

public void addItemsOnSpinner2() {

    spinner2 = (Spinner) findViewById(R.id.spinner2);
    List<String> list = new ArrayList<String>();
    list.add("Select");
    list.add("Andra Pradesh");
    list.add("Tamilnadu");
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_spinner_item, list);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(dataAdapter);
  }

}

1 个答案:

答案 0 :(得分:1)

您的ArrayAdapter需要具有 getView()方法。您可以在此处添加复选框。像这样:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null || !(convertView instanceof CheckBox)) {
            CheckBox checkBox = new CheckBox(getContext());
            convertView = checkBox;
        }
        ((CheckBox) convertView).setText(getItem(position));

        return convertView;
    }