ParseQuery Android - 获取项目的位置

时间:2017-02-17 08:46:22

标签: android parsing parse-platform

我正在创建一个应用程序,其中有一个顶级用户列表。我从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();


            }}});
}

1 个答案:

答案 0 :(得分:1)

done()内设置适配器并使用 currentUser 查找list

的索引
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);
     }
});
}