ParseQuery Android-获取项目的位置
我正在创建一个应用程序,其中有一个顶级用户列表。我从ParseQuery中获取列表。我希望列表跳转到特定用户 索引返回-1,我不明白为什么。你能帮忙吗ParseQuery Android-获取项目的位置,android,parsing,parse-platform,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(
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();
}}});
}
void queryTopTen(){
pd.setMessage(“Lista betöltése…”);
pd.show();
final ParseQuery=ParseQuery.getQuery(Configs.USER\u CLASS\u NAME);
查询.设定限额(100000);
query.orderByDescending(Configs.USER_POINTS);//用户_怪物_捕获
findInBackground(新的FindCallback(){
公共作废完成(列出对象,ParseException错误){
如果(错误==null){
usersArray=对象;
pd.解散();
//自定义列表适配器
类ListAdapter扩展了BaseAdapter{
私人语境;
公共ListAdapter(上下文、列表对象){
超级();
this.context=上下文;
}
//配置单元
@TargetApi(Build.VERSION\u code.LOLLIPOP)
@凌驾
公共视图getView(最终整数位置、视图单元格、视图组父级){
if(单元格==null){
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
单元=充气机。充气(R.layout.topten\u单元,空);
}
//获取解析对象
最终ParseObject uObj=usersArray.get(位置);
TextView fnTxt=(TextView)cell.findviewbyd(R.id.fullnameTxt);
int listPosition=位置+1;
//SharedReferences.Editor=positionScroll.edit();
//编辑。输入(“位置”,位置);
//commit();
字符串topListPosition=“”+listPosition;
setText(uObj.getString(Configs.USER_FULLNAME));
//设置列表编号
TextView avatarImg=(TextView)cell.findViewById(R.id.avatarImage);
头像框。setText(topListPosition);
//获取统计数据
TextView statsTxt=(TextView)cell.findViewById(R.id.statsTxt);
int-catched=0;
积分=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”);
返回单元;
}
@凌驾
public int getCount(){return usersArray.size();}
@凌驾
公共对象getItem(int位置){return usersArray.get(position);}
@凌驾
公共长getItemId(int位置){return position;}
}
//初始化ListView并设置其适配器
String nameTitle=currUser.getString(配置.USER\u CLASS\u NAME);
int index=usersArray.indexOf(currUser);//解析对象列表中cuurent用户的索引
Toast.makeText(getApplicationContext(),“”+索引,Toast.LENGTH_LONG.show();
ListView storesList=(ListView)findViewById(R.id.toptenListView);
setAdapter(新的ListAdapter(TopTen.this,usersArray));
店名选择(索引);
//查询错误
}否则{
Toast.makeText(getApplicationContext(),error.getMessage().toString(),Toast.LENGTH_LONG).show();
pd.解散();
}}});
}
在done()中设置适配器,并使用currentUser从列表中查找索引
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);
}
});
}
query.findInBackground(新的FindCallback(){
公共作废完成(列出对象,ParseException错误){
如果(错误==null){
usersArray=对象;
pd.解散();
String nameTitle=currUser.getString(配置.USER\u CLASS\u NAME);
字符串userName=currUser.getUsername();
for(int ind=0;ind Hi,谢谢,我已经上传了完整的块,它在里面,但仍然不工作。您是否检查了列表中是否存在currentUser
?当然,它在那里。问题是两个对象不相等。您可以使用for
循环执行操作,请参阅更新的答案!。