2015-11-19 6 views
14

Sto cercando di ottenere un oggetto con una query Parse.Query on ParseObject

Questo è il mio codice:

ParseQuery<ParseObject> query = ParseQuery.getQuery("Conference"); 
    query.findInBackground(new FindCallback<ParseObject>() { 
     public void done(List<ParseObject> results, ParseException e) { 
      if (e == null) { 
       // Results were successfully found from the local datastore. 
      } else { 
       showLog(e.toString()); 
      } 
     } 
    }); 

ottengo questo errore:

com.parse.ParseException: java.lang.IllegalStateException: ParseObject has no data for 'objectId'. Call fetchIfNeeded() to get the data.

BTW mia Conference classe contiene puntatori.

+0

dovete recuperare i dati di quell'oggetto puntatore. –

+0

Ottengo lo stesso errore e la mia classe non contiene nemmeno alcun puntatore. È molto semplice e contiene solo una colonna 'name' (oltre alle colonne predefinite). –

risposta

8

Se stai interrogando direttamente da Parse, si può fare:

ParseQuery<ParseObject> query = ParseQuery.getQuery("Conference"); 
... 
query.include("name_of_the_column_containing_a_pointer"); 
query.include("another_pointer_column"); 
query.findInBackground(new FindCallback<ParseObject>() { 
    public void done(List<ParseObject> results, ParseException e) { 
     if (e == null) { 
      // You can now access the pointers specified in the include 
     } else { 
      showLog(e.toString()); 
     } 
    } 
}); 

In caso contrario, se si sta interrogando il datastore locale:

ParseQuery<ParseObject> query = ParseQuery.getQuery("Conference"); 
... 
query.findInBackground(new FindCallback<ParseObject>() { 
    public void done(List<ParseObject> results, ParseException e) { 
     if (e == null) { 
      ParseObject parseObject = results.get(0); // Get the object 
      parseObject.fetchIfNeededInBackground(new GetCallback<ParseObject>() { 
       public void done(ParseObject result, ParseException e) { 
        if (e == null) 
         // Do something with result 
       } 
      } 
     } else { 
      showLog(e.toString()); 
     } 
    } 
}); 
+0

E se la mia classe non contiene un puntatore e lancia comunque l'errore? –

1

Ho avuto lo stesso problema. Il problema era che il metodo overrided getObjectId nei miei ParseObject implementazioni:

@ParseClassName("MyOwnParseClass") 
public class MyOwnParseClassextends ParseObject {  
    public static ParseQuery<MyOwnParseClass> getQuery() { 
     return ParseQuery.getQuery(MyOwnParseClass.class); 
    } 

    public String getObjectId() { 
     return getString("objectId"); 
    } 

    public String getName() { 
     return getString("name"); 
    } 

    public void setName(String name) { 
     put("name", name); 
    } 
} 

Ma ParseObject ha già il metodo getObjectId() e utilizzando

return getString("objectId"); 

non restituisce il objectId.

+0

Ciao juergen Sono riuscito a ottenere la stringa objectId utilizzando uno dei seguenti modi: String id = ParseUser.getCurrentUser(). GetObjectId(); all'interno della funzione Trova richiamata. Un secondo modo: anche nella funzione done() del findCallBack in cui i dati restituiti sono denominati oggetti. objects.get (0) .getObjectId(); –

+0

Ecco una terza via. La chiave principale è impostare ParseObject come variabile finale in modo da poter fare riferimento ad esso nel metodo done(). finale ParseObject po = new ParseObject ("Test"); po.put ("nomeutente", temp.getUserName()); po.saveInBackground (new SaveCallback() {public void done (ParseException e) { if (e == null) {String id = po.getObjectId(); Log.d (TAG, "L'ID oggetto è:" + id); } else {// Il salvataggio fallito Log.d (TAG, "errore di aggiornamento utente:". + e); }} }); Spero che questo ti aiuta. –

+0

Questi sono solo 3 diversi segmenti di codice che chiamano lo stesso metodo: 'getObjectId()' di 'ParseObject' che ho già menzionato nella mia risposta –

1

provare questo codice

ParseQuery<ParseObject> query = ParseQuery.getQuery("MyClass"); 
     ParseUser user = ParseUser.getCurrentUser(); 
     query.whereEqualTo("owners", user); 
     query.findInBackground(new FindCallback<ParseObject>() { 
      @Override 
      public void done(List<ParseObject> objects, com.parse.ParseException e) { 
       if (e == null) { 
        for (ParseObject parseObject : objects){ 
         String task; 
         task= parseObject.get("owners").toString(); 
         adapter.add(task); 
         adapter.notifyDataSetChanged(); 
        } 
       } else { 
        // Something went wrong. 
        Toast.makeText(getActivity(),"Error: " + e.getMessage().toString(),Toast.LENGTH_SHORT).show(); 
       } 
      } 
     }); 
2

Hi @Juergen ero in grado di ottenere la stringa objectId utilizzando uno dei seguenti modi:

String id = ParseUser.getCurrentUser().getObjectId(); 

all'interno della funzione Findcallback done.

Un secondo modo - anche nella funzione done() del findCallBack in cui i dati restituiti si chiama

objects.objects.get(0).getObjectId(); 

Ecco una terza via. Il tasto principale qui è impostare la variabile ParseObject come variabile finale in modo da poterla consultare nel metodo done().

final ParseObject po = new ParseObject("Test"); 
po.put("username", temp.getUserName()); 
po.saveInBackground(new SaveCallback() { 
public void done(ParseException e) { 
    if (e == null) { 
    String id = po.getObjectId(); 
    Log.d(TAG, "The object id is: " + id); 
    } else { // The save failed. Log.d(TAG, "User update error: " + e); 
    } 
} 
}); 

Spero che questo ti aiuti.

1
ParseQuery<ParseObject> queryP = ParseQuery.getQuery("Courses"); 
    queryP.whereEqualTo("Student", nameStudent); 
    queryP.findInBackground(new FindCallback<ParseObject>() { 
     @Override 
     public void done(List<ParseObject> coursesList, ParseException e) 
     { 
      ArrayList<String> courses = null; 
      if (e == null) 
      { 
       courses = new ArrayList<String>(); 
       for (ParseObject course : coursesList) 
       { 
        String courseName = course.getString("CoursesNameInParseColumn");  
        courses.add(courseName); 
       } 
      } 
      else 
      { 
       Log.d("Post retrieval", "Error: " + e.getMessage()); 
      } 

      populateCoursesList(courses); 
     } 
    }); 

provare questo

Problemi correlati