可以通过在Activity中动态更改Listview的Adapter来解决这个问题。以下是代码示例:
首先,在布局文件中添加Listview:
然后,在Activity中创建Listview的不同Adapter:
private ArrayAdapter adapter1;
private ArrayAdapter adapter2;
private ListView listView;
private String[] strings1 = {"One", "Two", "Three"};
private String[] strings2 = {"Four", "Five", "Six"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listview);
adapter1 = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, strings1);
adapter2 = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, strings2);
}
然后,在需要更改Adapter的时候,调用Listview的setAdapter()方法:
// 显示adapter1中的数据
listView.setAdapter(adapter1);
// 显示adapter2中的数据
listView.setAdapter(adapter2);
这样就可以在同一个Listview中显示不同的数据了。