从Parse.com检查自定义列表视图中的复选框

时间:2014-12-17 01:46:56

标签: java android android-listview parse-platform android-checkbox

我面临着逻辑问题。

我从Parse.com获取数据,如果当前用户喜欢卡片,我需要将复选框标记为已选中。

一切正常但我必须向下滚动才能更改复选框的状态

这是我的代码

public class CardsFragment extends  ListFragment {

    public static final String TAG = CardsFragment.class.getSimpleName();

    protected List<ParseObject> mCards;
    protected ParseRelation<ParseObject> mUserCategoriesRelation;
    protected ParseRelation<ParseObject> mCardsLikeRelation;
    protected ParseUser mCurrentUser;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_cards, container,
                false);


        return rootView;
    }


    @Override
    public void onResume() {
        super.onResume();


        getActivity().setProgressBarIndeterminateVisibility(true);

        final List<CardModel> list = new ArrayList<CardModel>();

        mCurrentUser = ParseUser.getCurrentUser();
        mCardsLikeRelation = mCurrentUser.getRelation("cardLikesRelation");
        mUserCategoriesRelation = mCurrentUser.getRelation(ParseConstants.KEY_CATEGORIES_RELATION);

        ParseQuery<ParseObject> query = ParseQuery.getQuery(ParseConstants.CLASS_CARD);
        query.whereEqualTo(ParseConstants.KEY_DELETED, false);
        query.orderByDescending(ParseConstants.KEY_CREATED_AT);
        query.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> cards, ParseException e) {

                getActivity().setProgressBarIndeterminateVisibility(false);

                if (e == null) {
                    mCards = cards;

                    for (final ParseObject card : mCards) {

                        list.add(get(card.getString("title"), card.getString("content"), card.getString("background"), card.getBoolean("background_image"),card.getInt("likes")));

                    }


                    if (getListView().getAdapter() == null) {
                        ArrayAdapter<CardModel> adapter = new CardAdapter(getListView().getContext(),
                                list);
                        setListAdapter(adapter);
                    } else {
                        //refill the adapter
                        ((CardAdapter) getListView().getAdapter()).refill(list);

                    }

                    int i = 0;
                    for (final ParseObject card : mCards) {
                        ParseQuery<ParseObject> likesQuery = mCardsLikeRelation.getQuery();
                        likesQuery.whereEqualTo("objectId", card.getObjectId());

                        final int finalI = i;
                        likesQuery.findInBackground(new FindCallback<ParseObject>() {
                            @Override
                            public void done(List<ParseObject> likes, ParseException e) {

                                if (e == null) {
                                    //list returned - look for a match
                                    Log.e(TAG, "likes" + likes.size());

                                    if (likes.size() == 1) {
                                        //liked
                                       list.get(finalI).setSelected(true);


                                    } 

                                } else {

                                    Log.e(TAG, e.getMessage());
                                }

                            }

                        });
                        i++;
                    }

                } else {

                    Log.d(TAG, "Error: " + e.getMessage());
                    AlertDialog.Builder builder = new AlertDialog.Builder(getListView().getContext());
                    builder.setMessage(e.getMessage())
                            .setTitle(R.string.error_title)
                            .setPositiveButton(android.R.string.ok, null);
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }


            }

        });

    }


    private CardModel get(String title, String content, String background, boolean background_image, int likes) {
        return new CardModel(title, content, background, background_image, likes);
    }

}

1 个答案:

答案 0 :(得分:0)

知道了。

在第二个查询中,用户使用

提取喜欢的内容

&#13;
&#13;
likesQuery.find();
&#13;
&#13;
&#13;

而不是

&#13;
&#13;
likesQuery.findInBackground(new FindCallback<ParseObject>() {});
&#13;
&#13;
&#13;