2017-02-09 33 views
-1

我发现一些教程,创建databaseHelper类,它帮我来从数据库中的数据填充的GridView和ListView,数据被读取使用名单,但不知何故,我不知道如何来填充列表到ArrayList中我的GridView和列表视图,当我尝试,它说列表无法转换成ArrayList中。我怎样才能做到这一点?我在这里的代码片断从databaseHelper创建的列表

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

只需将您的列表数据转换成数组列表与下面的代码...

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

为第三行:arrayList.addAll(名单);,不应该是list_seat呢?我更新到list_seat及其作品!谢谢! – Gabriel

+0

是的,你是对的。 –