游标数组进入 ListView 但多列
Cursor Array into ListView but multiple columns
我目前通过数据库查询获取游标数据,将每个选择放入一个数组中,然后显示在我的三个列表视图之一中。
public void insertData() {
ListView listView = (ListView) findViewById(R.id.listWorkouts1);
ListView listView2 = (ListView) findViewById(R.id.listWorkouts2);
ListView listView3 = (ListView) findViewById(R.id.listWorkouts3);
SQLiteController db = new SQLiteController(this);
String username = ma.id;
//populate an ArrayList<String> from the database and then view it
ArrayList<String> theList1 = new ArrayList<>();
ArrayList<String> theList2 = new ArrayList<>();
ArrayList<String> theList3 = new ArrayList<>();
ArrayList<String> finalList = new ArrayList<>();
Cursor data = db.getUserListContents(username);
if (data.getCount() == 0) {
Toast.makeText(this, "There are no contents in this list!", Toast.LENGTH_LONG).show();
} else {
while (data.moveToNext()) {
theList1.add(data.getString(0));
theList2.add(data.getString(1));
theList3.add(data.getString(2));
ListAdapter listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, theList1);
listView.setAdapter(listAdapter);
ListAdapter listAdapter2 = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, theList2);
listView2.setAdapter(listAdapter2);
ListAdapter listAdapter3 = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, theList3);
listView3.setAdapter(listAdapter3);
}
}
}
问题是用户可以分别滚动浏览三个列表视图中的每一个。
如何只使用一个listView,而将要查看的数据分隔在不同的列中?
默认情况下,它会列出先前值下的所有内容。
每次执行 While 循环时,我希望数据显示在同一行上,而不是一行一行显示。
感谢任何帮助。
您可以将 RecyclerView
与 GridLayoutManager
结合使用。这里还有一个. You can also look into this tutorial。
您实际上可以创建一个对象并将该对象的列表传递到您的适配器以从那里加载项目,而不是单独的列表。例如,考虑以下 class。
public class Workout {
public String name;
public String date;
public String weight;
}
现在,当从 cursor
读取值时,创建一个 Workout
class 的对象并创建一个 List<Workout>
,然后修改您的适配器以传递此列表。
我希望我能给你一个优雅地实现它的想法。
我目前通过数据库查询获取游标数据,将每个选择放入一个数组中,然后显示在我的三个列表视图之一中。
public void insertData() {
ListView listView = (ListView) findViewById(R.id.listWorkouts1);
ListView listView2 = (ListView) findViewById(R.id.listWorkouts2);
ListView listView3 = (ListView) findViewById(R.id.listWorkouts3);
SQLiteController db = new SQLiteController(this);
String username = ma.id;
//populate an ArrayList<String> from the database and then view it
ArrayList<String> theList1 = new ArrayList<>();
ArrayList<String> theList2 = new ArrayList<>();
ArrayList<String> theList3 = new ArrayList<>();
ArrayList<String> finalList = new ArrayList<>();
Cursor data = db.getUserListContents(username);
if (data.getCount() == 0) {
Toast.makeText(this, "There are no contents in this list!", Toast.LENGTH_LONG).show();
} else {
while (data.moveToNext()) {
theList1.add(data.getString(0));
theList2.add(data.getString(1));
theList3.add(data.getString(2));
ListAdapter listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, theList1);
listView.setAdapter(listAdapter);
ListAdapter listAdapter2 = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, theList2);
listView2.setAdapter(listAdapter2);
ListAdapter listAdapter3 = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, theList3);
listView3.setAdapter(listAdapter3);
}
}
}
问题是用户可以分别滚动浏览三个列表视图中的每一个。
如何只使用一个listView,而将要查看的数据分隔在不同的列中?
默认情况下,它会列出先前值下的所有内容。
每次执行 While 循环时,我希望数据显示在同一行上,而不是一行一行显示。
感谢任何帮助。
您可以将 RecyclerView
与 GridLayoutManager
结合使用。这里还有一个
您实际上可以创建一个对象并将该对象的列表传递到您的适配器以从那里加载项目,而不是单独的列表。例如,考虑以下 class。
public class Workout {
public String name;
public String date;
public String weight;
}
现在,当从 cursor
读取值时,创建一个 Workout
class 的对象并创建一个 List<Workout>
,然后修改您的适配器以传递此列表。
我希望我能给你一个优雅地实现它的想法。