gpt4 book ai didi

android - ParseQuery Android - 获取项目的位置

转载 作者:行者123 更新时间:2023-11-29 01:12:00 25 4
gpt4 key购买 nike

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


}}});
}

最佳答案

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);
}
});
}

关于android - ParseQuery Android - 获取项目的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42293248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com