游标数组进入ListView但有多个列

问题描述 投票:1回答:1

我目前通过数据库查询获取游标数据,将每个选择放入数组中,然后在我的三个列表视图之一中显示。

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

enter image description here

问题是用户可以分别滚动浏览三个列表视图中的每个列表视图。

我如何只使用一个listView却将要在单独列中看到的数据分开?

默认情况下,它会列出前一个值以下的所有内容。

每次执行While循环时,我希望该数据显示在同一行上,而不是一个显示在另一行下。

感谢您的帮助。

java android sqlite android-listview
1个回答
1
投票

您可以将RecyclerViewGridLayoutManager结合使用。这是一个basic implementation example。您也可以查看this tutorial

代替单独的列表,您实际上可以创建一个对象并将该对象的列表传递到适配器中以从那里加载项目。例如,考虑下面的类。

public class Workout {
    public String name;
    public String date;
    public String weight;
}

现在从cursor读取值时,创建Workout类的对象并创建List<Workout>,然后修改适配器以传递此列表。

我希望我能给您一个优雅地实现这一点的想法。

© www.soinside.com 2019 - 2024. All rights reserved.