简体   繁体   English

Parse.com获取查询并将其转换为字符串Java

[英]Parse.com Getting Query and Converting It into String Java

what I'm trying to do is querying a class and getting some strings. 我想做的是查询一个类并获取一些字符串。 But the below code returns something like ; 但是下面的代码返回类似:

com.parse.ParseObject@b4209180

and I couldn't get it to normal string value. 而且我无法将其恢复为正常的字符串值。

ParseQuery<ParseObject> query = ParseQuery.getQuery("question");
//query.whereKeyExists:@"objectId"
query.whereExists("questionTopic");

query.findInBackground(new FindCallback<ParseObject>() {

    @Override
    public void done(List<ParseObject> topics, ParseException e) {
        // TODO Auto-generated method stub
        if(e==null){


                textview.setText(topics.toString());

            }


        }else{
             Log.d("notretreive", "Error: " + e.getMessage());
        }


    }
});

"topics" in your code is a list of question objects. 代码中的“主题”是问题对象的列表。 You need to get the topic from that object. 您需要从该对象获取主题。 This should get you on the way: 这应该可以帮助您:

ParseQuery<ParseObject> query = ParseQuery.getQuery("question");
query.whereExists("questionTopic");

query.findInBackground(new FindCallback<ParseObject>() {

    @Override
    public void done(List<ParseObject> questions, ParseException e) {
        // The query returns a list of objects from the "questions" class
        if(e==null){
          for (ParseObject question : questions) {
            // Get the questionTopic value from the question object
            Log.d("question", "Topic: " + question.getString("questionTopic");
          }       
        } else {
             Log.d("notretreive", "Error: " + e.getMessage());
        }
    }
});

from the API I can see that the class ParseObject Inherits toString() from Object class. API中,我可以看到ParseObject类从Object类继承了toString()

That means unless there is implementation of custom toString() it will return the human-readable description of this object. 这意味着,除非实现自定义toString(),否则它将返回此对象的人类可读描述。

check here for the toString() being called in your case 在这里检查您的情况下被调用的toString()

EDIT: 编辑:

First of all you are trying to call toString() on List object by 首先,您尝试通过以下方法在List对象上调用toString()

topics.toString()

you will have to iterate over that List like 您将不得不像这样遍历该列表

for(ParseObject parseObj : topics){
//do something with parseObj like
parseObj.get(<provide_key_here>);
//print to check the value
System.out.println(parseObj.get(<provide_key_here>));
//where key is generally attribute name
}

I can see this is about two years old. 我可以看到这大约是两年了。 But I am currently having the same issue. 但是我目前有同样的问题。 SO here is what I did. 所以这就是我所做的。

You can't just call setText.toString because "topics" is returning as a Parse Object. 您不能只调用setText.toString,因为“主题”将作为解析对象返回。 You need to run a for loop to run through each topics object and get the text from that object, store it in a string and THEN you can set text to string. 您需要运行一个for循环以遍历每个主题对象并从该对象获取文本,将其存储在字符串中,然后可以将文本设置为string。 Here is what I did. 这是我所做的。

final List questions = new ArrayList<String>();

final ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("MasterQuestionList");
query.findInBackground(new FindCallback<ParseObject>() {
  public void done(List<ParseObject> Question, ParseException e) {
    if (e == null) {
      if (Question.size() > 0) {

        for (ParseObject user : Question) {

         String tFile = (String) user.get("Question");
          questions.add(tFile);

        }

        TextView a = (TextView) findViewById(R.id.sentence);
        a.setText(String.valueOf(questions));

I created a list, and added all text values to that list. 我创建了一个列表,并将所有文本值添加到该列表。

Hope this helps anyone running into a similar issue. 希望这可以帮助遇到类似问题的任何人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM