如何制作不同类型对象的ArrayList?

How to make ArrayList of different types of objects?

我有两个 pojo classes,即 PostFeedSessionFeed

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
}

我正在尝试创建一个搜索 activity,它从 MySqlite 数据库中搜索数据。每个类型都有两个单独的表,即两个不同数据库中的 PostFeedTableSessionFeedTable

在我的数据库操作中 class,这就是我要返回的内容:

 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 contains a recycler view whose adapter takes an ArrayList and set that list, which further would contain two types of row elements.

问题:如何创建包含这两种类型对象的数组列表,以及如何使回收器视图从数组列表中显示这两种类型的对象。

如果您需要更多说明,请告诉我。提前致谢。

您已经实施了 Parcelable interface,因此您可以创建此 interface 的列表,该列表可用于存储 implemented class object.

ArrayList<Parcelable> pList = new ArrayList<>();
public class Feeds implements Parcelable{

private PostFeed postFeed;
private SessionFeed sessionFeed;

Public Feeds(PostFeed postfeed, SessionFeed sessionfeed){
this.postFeed = postfeed
this.sessionFeed = sessionfeed
}
}

然后Feeds数组列表就可以解决问题了。

for(int i=0: i<postfeedArraylist.size; i ++)
    feedsArraylist.add( new Feeds(postfeedArraylist.get(i), null);

也为 sessionfeedArraylist 重复相同的 for 循环。

由于两者之间似乎没有太多共同点,尽管名称相似,您不妨创建一个包含 PostFeedSessionFeed 的新对象。

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);

我认为nikka的建议是正确的。对其进行改进,将下面的 class 用于您的适配器

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;
    }

}

并且在您的数据库辅助方法中,return 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() 检查类型并相应地设置视图。希望对你有帮助。