【问题标题】:How to make a RadioGroup show 2 different ListViews如何使 RadioGroup 显示 2 个不同的 ListView
【发布时间】:2014-05-10 00:42:59
【问题描述】:

我想要做的是,你有 2 个 RadioButton,当你点击其中的 1 个时,它会显示一个 ListView1,例如,当你点击另一个 RadioButton 时,它会显示一个 ListView2

这是我在我的 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();
}

如果您需要其他内容,请告诉我。谢谢!

【问题讨论】:

    标签: android eclipse listview radio-button radio-group


    【解决方案1】:

    您只需将 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 时添加一个额外的 if for 并对列表视图进行更改。来自安卓参考:"When the selection is cleared, checkedId is -1."

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

    【讨论】:

      【解决方案2】:

      无需一次又一次地创建新的列表视图和适配器实例。每次更新数组列表时,您的列表都需要更新。

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

      }

      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多