从databaseHelper创建的List中填充Gridview和Listview

时间:2017-02-09 08:19:37

标签: android gridview

我找到了一些教程,创建了databaseHelper类,它帮助我从数据库中获取数据,数据是使用List获取的,但不知怎的,我不知道如何将该列表填充到ArrayList中,用于我的gridview和列表视图,当我试试吧,它说List无法转换成ArrayList。我怎样才能做到这一点?这里是我的代码片段

databaseHelper.java

public List<Seat> getAllSeats() {
        List<Seat> seats = new ArrayList<Seat>();
        String selectQuery = "SELECT  * FROM " + TABLE_SEATS + "WHERE status=0";

        Log.e(LOG, selectQuery);

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (c.moveToFirst()) {
            do {
                Seat seat = new Seat();
                seat.set_id(c.getInt((c.getColumnIndex("id"))));
                seat.set_table_no(c.getString(c.getColumnIndex("table_no")));

                // adding to list
                seats.add(seat);
            } while (c.moveToNext());
        }
        return seats;
    }

index.java

/* LIST SEAT */
        final ArrayList<Seat> list_seat = db.getAllSeats();
        final GridView gridview_seat = (GridView) findViewById(R.id.gridviewSeat);
        gridview_seat.setAdapter(new SeatListAdapter(this, list_seat));

1 个答案:

答案 0 :(得分:1)

只需使用以下代码将列表数据转换为数组列表...

List<Seat> list = db.getAllSeats();
ArrayList<Seat> list_seat = new ArrayList<Seat>(list.size());
list_seat .addAll(list);