【问题标题】:ParseQuery Android - getting the position of an itemParseQuery Android - 获取项目的位置
【发布时间】:2017-02-17 08:46:22
【问题描述】:

我正在创建一个包含顶级用户列表的应用。我从 ParseQuery 获取列表。我希望列表跳转到特定用户。

索引返回-1,我不明白为什么。你能帮忙吗?

 void queryTopTen() {
    pd.setMessage("Lista betöltése...");
    pd.show();


    final ParseQuery query = ParseQuery.getQuery(Configs.USER_CLASS_NAME);
    query.setLimit(100000);
    query.orderByDescending(Configs.USER_POINTS);   //USER_MONSTERS_CATCHED
    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> objects, ParseException error) {
            if (error == null) {
                usersArray = objects;
                pd.dismiss();


                // CUSTOM LIST ADAPTER
                class ListAdapter extends BaseAdapter {
                    private Context context;

                    public ListAdapter(Context context, List<ParseObject> objects) {
                        super();
                        this.context = context;
                    }

                    // CONFIGURE CELL
                    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
                    @Override
                    public View getView(final int position, View cell, ViewGroup parent) {
                        if (cell == null) {
                            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                            cell = inflater.inflate(R.layout.topten_cell, null);
                        }
                        // Get Parse object
                        final ParseObject uObj = usersArray.get(position);

                        TextView fnTxt = (TextView) cell.findViewById(R.id.fullnameTxt);
                        int listPosition = position + 1;

                        //SharedPreferences.Editor editor = positionScroll.edit();
                        //editor.putInt("position", position);
                        //editor.commit();

                        String topListPosition = "" + listPosition;
                        fnTxt.setText(uObj.getString(Configs.USER_FULLNAME));

                        // Set list number
                        TextView avatarImg = (TextView) cell.findViewById(R.id.avatarImage);
                        avatarImg.setText(topListPosition);


                        // Get Stats
                        TextView statsTxt = (TextView) cell.findViewById(R.id.statsTxt);
                        int catched = 0;
                        int points = 0;
                        if (uObj.getNumber(Configs.USER_MONSTERS_CATCHED) != null){ catched = (int) uObj.getNumber(Configs.USER_MONSTERS_CATCHED); }
                        if (uObj.getNumber(Configs.USER_POINTS) != null){ points = (int) uObj.getNumber(Configs.USER_POINTS); }
                        statsTxt.setText(points + " pont | " + catched + " lopás");

                        return cell;
                    }

                    @Override
                    public int getCount() { return usersArray.size(); }

                    @Override
                    public Object getItem(int position) { return usersArray.get(position); }

                    @Override
                    public long getItemId(int position) { return position; }



                }

                // Init ListView and set its adapter

                String nameTitle = currUser.getString(Configs.USER_CLASS_NAME);

                int index = usersArray.indexOf(currUser); //index of cuurent user from list of parse objec
                Toast.makeText(getApplicationContext(), "" + index, Toast.LENGTH_LONG).show();

                ListView storesList = (ListView) findViewById(R.id.toptenListView);
                storesList.setAdapter(new ListAdapter(TopTen.this, usersArray));
                storesList.setSelection(index);


                // Error in query
            } else {
                Toast.makeText(getApplicationContext(), error.getMessage().toString(), Toast.LENGTH_LONG).show();
                pd.dismiss();


            }}});
}

【问题讨论】:

    标签: android parsing parse-platform


    【解决方案1】:

    done() 中设置适配器并使用currentUserlist 中查找索引

    query.findInBackground(new FindCallback<ParseObject>() {
    public void done(List<ParseObject> objects, ParseException error) {
        if (error == null) {
            usersArray = objects;
            pd.dismiss();
            String nameTitle = currUser.getString(Configs.USER_CLASS_NAME);
    
            String userName = currUser.getUsername();
            for(int ind = 0; ind <objects.size();ind++){
                String usernow = objects.get(ind).getString("user");
                if(usernow.equals(userName)){
                    index=ind;
                }
            }
    
    Toast.makeText(getApplicationContext(), "" + index, Toast.LENGTH_LONG).show();
    
    ListView storesList = (ListView) findViewById(R.id.toptenListView);
    storesList.setAdapter(new ListAdapter(TopTen.this, usersArray));
    storesList.setSelection(index);
         }
    });
    }
    

    【讨论】:

    • 嗨,谢谢,我已经上传了完整的块,它在里面,但还是不行。
    • 您是否检查过currentUser 存在于list 中?
    • 当然,它就在那里。
    • 问题是两个对象不相等。您可以使用for 循环查看更新的答案!。
    猜你喜欢
    • 1970-01-01
    • 2021-03-10
    • 2015-02-16
    • 2014-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-20
    相关资源
    最近更新 更多