【问题标题】:How to make ArrayList of different types of objects?如何制作不同类型对象的ArrayList?
【发布时间】:2017-02-02 17:30:58
【问题描述】:

我有两个 pojo 课程,即 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
}

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

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

 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,以及如何让recycler view显示arraylist中的这两种对象。

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

【问题讨论】:

  • 您可以创建一个包含对象 a 和 b 的对象 c。然后创建一个c的arraylist。
  • 听起来很简单,你能帮忙写一下代码 sn-ps 吗? @nikka
  • 添加了一个答案,如果你能使用相同的答案,请评论
  • 请接受正确答案。快乐编码:)
  • @nikka ,当问题解决后,我一定会接受答案。 :)

标签: android arraylist android-sqlite android-recyclerview


【解决方案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() 检查类型并相应地设置视图。希望对您有所帮助。

【讨论】:

  • 如果你真的想帮助我,让我们做adpater?帮助我为多个视图制作适配器。我不能做 getItemViewType()。
  • 我已经从 ArrayList 的数据库中获得了我的搜索结果,现在如何在适配器中进行设置
【解决方案2】:

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

ArrayList<Parcelable> pList = new ArrayList<>();

【讨论】:

  • pList.add((Parcelable) sessionFeed); pList.add((Parcelable) postFeed);
  • 我理解你刚才展示的内容,但请仔细阅读问题,我如何执行数据库操作并最终获得我发送给我的适配器的列表或数组列表。请尝试分解并详细说明。
【解决方案3】:

由于两者之间似乎没有太多共同点,尽管名称相似,您不妨创建一个包含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);

【讨论】:

  • 数据库操作呢?如何使用上述访问存储的数据。主要是:我正在从数据库访问我的数据
  • 我对这个FeedHolder 课程进行了一些修改,可能更适合您的情况。由你决定。
【解决方案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 也重复相同的 for 循环。

【讨论】:

  • 数据库操作呢?如何使用上述访问存储的数据。主要是:我正在从数据库中访问我的数据。
  • 获取两个不同的 postfeed 和 sessionfeed 数组列表,然后简单地将它们放入 feeds 数组列表。因此,对于提要的每个成员,postfeed 将为 null,而 sessionfeed 将为 null。
  • 请注意:列表中的任何一个都不可能为空。两个arraylist都有一些数据,现在该怎么办?
  • 不,你误会了。假设您有 3 个 postfeed 和 2 个 sessionfeed。 Feed 将有 5 个成员。在开始 2 sessionfeed 将具有值,并且 feed 的 postfeed 对象将为 null,在最后 3 个成员中,postfeed 具有值并且 sessionfeed 将为 null。
  • 明白了。我会执行此操作,完成后,请务必接受您的回答,您介意如果我在某个地方与您交谈以寻求帮助,如果卡在某个地方吗?
猜你喜欢
  • 2011-03-25
  • 2015-06-22
  • 1970-01-01
  • 2018-07-20
  • 1970-01-01
  • 1970-01-01
  • 2018-10-31
  • 1970-01-01
  • 2010-09-12
相关资源
最近更新 更多