仅将ParseQuery中的新项添加到ArrayList

时间:2015-12-31 23:35:16

标签: java android parse-platform arraylist

我有一个ParseQuery,我从关系查询中加载项目。查询每次运行时都会返回所有对象。我想将项目存储在我的mRipleListfromParse <DropItem>中,然后只获取新项目,将它们添加到我的ArrayList的开头(位置0),这样我就可以避免每次都获取所有数据。

我尝试过使用Parse本地数据存储并缓存查询但没有最佳结果。我还试图保持对mRipleListFromParse的全局引用,但即使使用&#34; mRipleListFromParse.clear()&#34;列表在每次刷新数据时都是重复的。

有一种简单的方法可以实现这一目标吗?这是查询。感谢。

 public void loadRipleItemsFromParse() {

    ParseUser currentUser = ParseUser.getCurrentUser();

    final ArrayList<DropItem> mRipleListFromParse = new ArrayList<>();

    ParseRelation createdRelation = currentUser.getRelation("createdDrops");
    ParseRelation completedRelation = currentUser.getRelation("completedDrops");

    ParseQuery createdQuery = createdRelation.getQuery();
    ParseQuery completedQuery = completedRelation.getQuery();

    List<ParseQuery<ParseObject>> queries = new ArrayList<>();
    queries.add(createdQuery);
    queries.add(completedQuery);

    ParseQuery<ParseObject> mainQuery = ParseQuery.or(queries);
    mainQuery.include("authorPointer");
    mainQuery.orderByDescending("createdAt");
    mainQuery.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> list, ParseException e) {

            if (e != null) {
                Log.i("KEVIN", "error error");

            } else {
                for (int i = 0; i < list.size(); i++) {

                    final DropItem dropItem = new DropItem();

                    //Drop Author Data//////////////////////////////////////////////////////////
                    ParseObject authorData = (ParseObject) list.get(i).get("authorPointer");

                    ParseFile parseProfilePicture = (ParseFile) authorData.get("parseProfilePicture");
                    if (parseProfilePicture != null) {
                        parseProfilePicture.getDataInBackground(new GetDataCallback() {
                            @Override
                            public void done(byte[] data, ParseException e) {
                                if (e == null) {
                                    Bitmap bmp =     BitmapFactory.decodeByteArray(data, 0, data.length);
//                                        Bitmap resized = Bitmap.createScaledBitmap(bmp, 100, 100, true);
                                    dropItem.setParseProfilePicture(bmp);
                                    updateRecyclerView(mRipleListFromParse);
                                }
                            }
                        });
                    }

                    //dropItemAll.setAuthorName(authorName);
                    dropItem.setAuthorName((String) authorData.get("displayName"));
                    //Author id
                    dropItem.setAuthorId(authorData.getObjectId());
                    //Author Rank
                    dropItem.setAuthorRank(authorData.getString("userRank"));

                    //Drop Data////////////////////////////////////////////////////////////////
                    //DropObjectId
                    dropItem.setObjectId(list.get(i).getObjectId());
                    //CreatedAt
                    dropItem.setCreatedAt(list.get(i).getCreatedAt());
                    //dropItem.createdAt = new SimpleDateFormat("EEE, MMM d yyyy @ hh 'o''clock' a").parse("date");
                    //Drop description
                    dropItem.setDescription(list.get(i).getString("description"));

                    //Riple Count
                    int ripleCount = (list.get(i).getInt("ripleCount"));
                    if (ripleCount == 1) {
                        dropItem.setRipleCount(String.valueOf(list.get(i).getInt("ripleCount") + " Riple"));
                    } else {
                        dropItem.setRipleCount(String.valueOf(list.get(i).getInt("ripleCount") + " Riples"));
                    }

                    //Comment Count
                    int commentCount = (list.get(i).getInt("commentCount"));
                    if (commentCount == 1) {
                        dropItem.setCommentCount(String.valueOf(list.get(i).getInt("commentCount") + " Comment"));
                    }else {
                        dropItem.setCommentCount(String.valueOf(list.get(i).getInt("commentCount") + " Comments"));
                    }

                    mRipleListFromParse.add(dropItem);

                }
            }
        }
    });
}

0 个答案:

没有答案