2014-03-30 44 views
1

我想要做的是,你有2個RadioButton,當你點擊其中的一個時,它顯示一個ListView1,例如,當你點擊另一個RadioButton時,它顯示了一個ListView2如何使一個RadioGroup顯示2個不同的ListViews

這是我在XML文件中有:

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

而這就是我在我的.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(); 
} 

如果您需要別的東西,請讓我知道。謝謝!

回答

0

你只需要在populateListView方法中添加你所監聽器:

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

    } 
} 

如果您希望在清除RadioGroup中選擇隱藏或清除列表,你只需要添加一個額外的,如果當它返回-1並對listview進行更改時。從android參考:"When the selection is cleared, checkedId is -1."

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

不需要一次又一次創建新的列表視圖和適配器實例。你的列表需要每次更新時更新數組列表。

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

}

} 
相關問題