2017-02-17 3 views
2

Ich erstelle eine App, in der es eine Top-Liste von Benutzern gibt. Ich bekomme die Liste von einer ParseQuery. Ich möchte, dass die Liste zu dem bestimmten Benutzer springt.ParseQuery Android - Holen Sie sich die Position eines Artikels

Der Index gibt -1 zurück und ich verstehe nicht warum. Können Sie bitte helfen?

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


      }}}); 
} 

Antwort

1

Set Adapter innerhalb done() und verwenden current Index zu finden, von 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); 
    } 
}); 
} 
+0

Hallo, danke, habe ich den ganzen Block hochgeladen, es innen ist, aber immer noch nicht funktioniert. –

+0

haben Sie überprüft, dass 'currentUser' in' list' existiert? –

+0

Sicher, es ist da. –

Verwandte Themen