我有兩個POJO類,即PostFeed
和SessionFeed
如何製作不同類型對象的ArrayList?
PostFeed.class
public class PostFeed implements Parcelable{
private int pid;
private int uid;
private String link;
private String title;
private String description;
private int up;
private int comment_count;
private String postPicUrl;
private Date dop;
private String user_name;
private String user_pic;
private String user_status;
private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//getters and setters
}
SessionFeed.class
public class SessionFeed implements Parcelable{
private String session_title;
private String session_image;
private String session_description;
private int session_id;
private String s_venue;
private String s_coordinator;
private String s_c_email;
private String s_c_phone;
private String resource_person;
private String rp_desg;
private String time_and_date;
private String address;
private String room;
private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
private Date Dosp;
String user_name;
int uid;
String user_status;
String user_pic;
// getters and setters
}
我試圖創建一個搜索活動,其從MySqlite數據庫搜索數據。在兩個不同的數據庫中,每個類型有兩個單獨的表,即PostFeedTable
和SessionFeedTable
。
在我的數據庫操作類,這就是我要回:
public ArrayList<PostFeed> readPostForSearch(String searchText, DatabaseOperations dop){
SQLiteDatabase sqLiteDatabase = dop.getReadableDatabase();
ArrayList<PostFeed> newsFeedList = new ArrayList<>();
String query ="select * from " + html_test_const.getTable_name() + " where " + html_test_const.getTitle() +" LIKE '%"+searchText+"%' OR "+ html_test_const.getDescription() + " LIKE '%"+searchText+"%'"+" order by date desc " +";";
Cursor cursor = sqLiteDatabase.rawQuery(query,null);
if (cursor != null && cursor.moveToFirst()) {
L.m("loading entries " + cursor.getCount() + new Date(System.currentTimeMillis()));
do {
//create a new object and retrieve the data from the cursor to be stored in this object
PostFeed postFeed = new PostFeed();
postFeed.setTitle(cursor.getString(cursor.getColumnIndex(html_test_const.getTitle())));
postFeed.setLink(cursor.getString(cursor.getColumnIndex(html_test_const.getLink())));
postFeed.setDescription(cursor.getString(cursor.getColumnIndex(html_test_const.getDescription())));
long dateOfPost = cursor.getLong(cursor.getColumnIndex(html_test_const.getDate()));
postFeed.setDop(new java.sql.Date(dateOfPost));
postFeed.setUser_name(cursor.getString(cursor.getColumnIndex(html_test_const.getUser_name())));
postFeed.setPid(cursor.getInt(cursor.getColumnIndex(html_test_const.getSr_key())));
postFeed.setUp(cursor.getInt(cursor.getColumnIndex(html_test_const.getUp_down())));
postFeed.setComment_count(cursor.getInt(cursor.getColumnIndex(html_test_const.getComment_count())));
postFeed.setUid(cursor.getInt(cursor.getColumnIndex(html_test_const.getUid())));
postFeed.setPostPicUrl(cursor.getString(cursor.getColumnIndex(html_test_const.getPost_pic())));
postFeed.setUser_pic(cursor.getString(cursor.getColumnIndex(html_test_const.getUser_pic())));
postFeed.setUser_status(cursor.getString(cursor.getColumnIndex(html_test_const.getUser_status())));
newsFeedList.add(postFeed);
} while (cursor.moveToNext());
}
return newsFeedList;
}
這是相同的與各個變量的會話數據庫操作。
SearchActivity包含一個回收站視圖,其適配器採用
ArrayList
並設置該列表,該列表還包含兩種類型的行元素。
問題:如何創建一個包含這兩種類型的對象,並進一步如何使回收觀點表明,這兩種類型的ArrayList中的對象的ArrayList。
如果你需要更多的澄清讓我知道。提前致謝。
您可以創建一個包含對象a和b的對象c。然後創建一個c的數組列表。 – nikka
聽起來很簡單,你可以請代碼片段幫忙嗎? @nikka –
添加了一個答案,請評論,如果你能夠使用相同或不是 – nikka