Android 如何使放射组显示2个不同的列表视图
我想做的是,你有两个单选按钮,当你点击其中一个时,它会显示一个ListView1,例如,当你点击另一个单选按钮时,它会显示一个ListView2 这是我在xml文件中得到的:Android 如何使放射组显示2个不同的列表视图,android,eclipse,listview,radio-button,radio-group,Android,Eclipse,Listview,Radio Button,Radio Group,我想做的是,你有两个单选按钮,当你点击其中一个时,它会显示一个ListView1,例如,当你点击另一个单选按钮时,它会显示一个ListView2 这是我在xml文件中得到的: <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTo
<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();
}
private void populateListView(){
//创建项目列表
字符串[]项={“测试”、“测试2”、“测试3”};
字符串[]items2={“Bla”,“Bla Bla”,“Bla Bla Bla”};
//构建适配器
ArrayAdapter=新的ArrayAdapter(this,R.layout.main项,items);
ArrayAdapter适配器2=新的ArrayAdapter(此,R.layout.main项,项2);
//配置列表视图
ListView列表=(ListView)findViewById(R.id.listView1);
list.setAdapter(适配器);
}
检查更改后的公共无效(RadioGroup组,int checkedId){
if(checkedId==R.id.0){
//显示项目
}
if(checkedId==R.id.radio1){
//显示项目2
}
}
公共void onClick(视图v){
RadioGroup.clearCheck();
}
如果你还需要什么,请告诉我。谢谢 您只需将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);
}
}
无需反复创建新的listview和适配器实例。您的列表需要在每次更新数组列表时更新
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();
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();
}