我有两个pojo课程,即PostFeed
和SessionFeed
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中显示这两种类型的对象。
如果您需要更多说明,请与我联系。提前致谢。
答案 0 :(得分:4)
public class Feeds implements Parcelable{
private PostFeed postFeed;
private SessionFeed sessionFeed;
Public Feeds(PostFeed postfeed, SessionFeed sessionfeed){
this.postFeed = postfeed
this.sessionFeed = sessionfeed
}
}
然后,Feed的数组列表将解决问题。
并且
for(int i=0: i<postfeedArraylist.size; i ++)
feedsArraylist.add( new Feeds(postfeedArraylist.get(i), null);
对sessionfeedArraylist的循环重复相同的操作。
答案 1 :(得分:1)
由于两者之间似乎没有太多共同点,尽管共享相似的名称,您也可以创建一个包含PostFeed
和SessionFeed
的新对象。
class FeedHolder implements Parcelable {
private final List<PostFeed> postFeeds;
private final List<SessionFeed> sessionFeeds;
FeedHolder(@NonNull List<PostFeed> postFeeds,@NonNull List<SessionFeed> sessionFeeds) {
this.postFeeds = postFeeds;
this.sessionFeeds = sessionFeeds;
}
List<SessionFeed> getSessionFeeds() {
return sessionFeeds;
}
List<PostFeed> getPostFeeds() {
return postFeeds;
}
//If you're using this data in an adapter, knowing the total
//size of both lists might be helpful.
int getTotalFeedCount() {
return postFeeds.size() + sessionFeeds.size();
}
}
//...
List<SessionFeed> sessionFeeds = //whatever you do to populate your
//List of SessionFeeds...
List<PostFeed> postFeeds = //Same again.
FeedHolder feedHolder = FeedHolder(postFeeds, sessionFeeds);
答案 2 :(得分:1)
我认为nikka的建议是正确的。改进它,使用以下类适用于您的适配器
public class Feed implements Parcelable{
private PostFeed postFeed;
private SessionFeed sessionFeed;
public Feed(PostFeed feed){
this.postFeed = feed;
}
public Feed(SessionFeed feed){
this.sessionFeed = feed;
}
//returns true for session feed type
public boolean isSessionFeed(){
return sessionFeed!=null;
}
public SessionFeed getSessionFeed(){
return sessionFeed;
}
public PostFeed getPostFeed(){
return postFeed;
}
}
在数据库帮助方法中,返回Feed对象列表。
public List<Feed> readPostForSearch(String searchText, DatabaseOperations dop){
SQLiteDatabase sqLiteDatabase = dop.getReadableDatabase();
List<Feed> newsFeedList = new ArrayList<>();
...
if (cursor != null && cursor.moveToFirst()) {
L.m("loading entries " + cursor.getCount() + new Date(System.currentTimeMillis()));
do {
PostFeed postFeed = new PostFeed();
...
newsFeedList.add(new Feed(postFeed));
} while (cursor.moveToNext());
}
return newsFeedList;
}
对sessionFeeds重复相同操作,并将此列表从数据库添加到适配器列表中。设置视图时,使用isSessionFeed()
检查类型并相应地设置视图。希望它有所帮助。
答案 3 :(得分:0)
您已实施Parcelable interface
,因此您可以创建此interface
的列表,该列表可用于存储implemented class object
。
ArrayList<Parcelable> pList = new ArrayList<>();