Question

What I'm trying to do is, you have 2 RadioButton, and when you click 1 of them, it shows a ListView1, for example, and when you click the other RadioButton, it shows a ListView2

This is what i got in my xml file:

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/radio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:paddingRight="5dp"
        android:text="Free Apps" />

    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Paid Apps" />

</RadioGroup>

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/radioGroup1" >
</ListView>

And this is what I have in my .java:

private void populateListView() {
    // Create list of items
    String[] items = {"Test", "Test 2", "Test 3"};
    String[] items2 = {"Bla", "Bla bla", "Bla bla bla"};

    // Build Adapter
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.mainitem, items);
    ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, R.layout.mainitem, items2);

    // Configure the list view
    ListView list = (ListView) findViewById(R.id.listView1);
    list.setAdapter(adapter);

}

public void onCheckedChanged(RadioGroup group, int checkedId) {
    if(checkedId == R.id.radio0){
         // Show items
    }
    if(checkedId == R.id.radio1){
        // Show items2
    }
}

public void onClick(View v) {
    RadioGroup.clearCheck();
}

If you need something else, please let me know. Thanks!

Was it helpful?

Solution

You just need to add what you have in the populateListView method to the listener:

public void onCheckedChanged(RadioGroup group, int checkedId) {
    if(checkedId == R.id.radio0){

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.mainitem, items);
        ListView list = (ListView) findViewById(R.id.listView1);
        list.setAdapter(adapter);

    } else if(checkedId == R.id.radio1){

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.mainitem, items2);
        ListView list = (ListView) findViewById(R.id.listView1);
        list.setAdapter(adapter);

    }
}

If you want to hide or clear the list when you clear the RadioGroup selection you just need to add an extra if for when it returns -1 and make the changes to the listview. From the android reference: "When the selection is cleared, checkedId is -1."

if (checkedId == -1) { ... }

OTHER TIPS

No need to create new listview and adapter instance again and again. your list need to update every time just update the array list.

String[] items = {"Test", "Test 2", "Test 3"};
        String[] items2 = {"Bla", "Bla bla", "Bla bla bla"};
    List<String> arrayList1 = new ArrayList( Arrays.asList(items));
    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, arrayList1);
ListView list = (ListView) findViewById(R.id.listView1);
list.setAdapter(adapter);


public void onCheckedChanged(RadioGroup group, int checkedId) {

   if(checkedId == R.id.radio0){
         arrayList1.clear();
         arrayList1.addAll(Arrays.asList(items));
    } else if(checkedId == R.id.radio1){
          arrayList1.clear();
         arrayList1.addAll(Arrays.asList(items2));
    }
if(adapter != null){
adapter.notifyDataChange();

}

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top