【问题标题】:SQLite and ListViewSQLite 和 ListView
【发布时间】:2015-09-01 16:01:32
【问题描述】:

我的班级有一个ListView,我有一个关于数字x 的光标,它每次都在递增,我在ListView 中使用这个光标,但当时我正在使用这个@987654324 @ListView 中仅显示一行。我想显示有关递增值的所有列表。我的代码如下:

if (cursor.moveToFirst()) {
        do {
            do {
                int x = cursor.getInt(cursor.getColumnIndex("trainId"));
                String s1 = "SELECT * FROM route_table WHERE trainId=" + x + " AND stationId=" + FromStationId + ";";
                String s2 = "SELECT * FROM route_table WHERE trainId=" + x + " AND stationId=" + ToStationId + ";";
                final Cursor c1 = SQ.rawQuery(s1, null);
                final Cursor c2 = SQ.rawQuery(s2, null);
                if (c1 != null && c1.moveToFirst()) {
                    i = c1.getInt(c1.getColumnIndex("_id"));
                    c1.close();
                }
                if (c2 != null && c2.moveToFirst()) {
                    j = c2.getInt(c2.getColumnIndex("_id"));
                    c2.close();
                }
                if (i < j) {
                    Toast.makeText(this, "My Train is:" + x, Toast.LENGTH_SHORT).show();
                    String fstring = "SELECT * FROM train_table WHERE _id=" + x + ";";
                    final Cursor c3 = SQ.rawQuery(fstring, null);
                    final ListView lv = (ListView) findViewById(R.id.tiimetable_list);
                    CursorAdapterTimeTable myc = new CursorAdapterTimeTable(this, c3);
                    lv.setAdapter(myc);
                    // List(x);
                }
                break;
            } while (cursor.moveToNext());
            continue;
        } while (cursor.moveToNext());

【问题讨论】:

  • 查询中的X是递增的。

标签: android android-listview android-sqlite


【解决方案1】:

只显示一行,因为每次执行查询时都会创建一个新适配器。

试试这个来获取“_id”大于0的所有项目:

String fstring = "SELECT * FROM train_table WHERE _id > 0";
final Cursor c3 = SQ.rawQuery(fstring, null);
final ListView lv = (ListView) findViewById(R.id.tiimetable_list);
CursorAdapterTimeTable myc = new CursorAdapterTimeTable(this, c3);
lv.setAdapter(myc);

【讨论】:

  • 但是当 x 是某个递增值时,我想获取列表时该怎么办,每次都会递增
  • 您需要更改适配器以接受新项目,包括以前的项目,然后致电myc.notifyDatasetChanged() 我建议您使用“ArrayAdapter”,请参阅:vogella.com/tutorials/AndroidListView/article.html
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-06
相关资源
最近更新 更多