无法从解析用户检索数组

时间:2015-08-13 15:39:38

标签: android parse-platform android-arrayadapter

您好我是Android和Parse.com的新手。我正在尝试从ParseUser类中获取数组并将其设置为Spinner。我跟着this Link,但它对我不起作用。

我在此代码中选择Spinner course中的其他项目时尝试更新Spinner faculty_initial

Spinner faculty_initial; //another spinner
Spinner course;
ArrayAdapter<String> courseAdapter;

faculty_initial.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        //getting the selected item from the faculty_initial spinner
        selectedInitial = initial.getSelectedItem().toString().trim();

        ParseQuery<ParseUser> query = ParseUser.getQuery();
        query.whereEqualTo("username", selectedInitial);
        query.getFirstInBackground(new GetCallback<ParseUser>() {
            @Override
            public void done(ParseUser pu, ParseException e) {
                if (e == null) {
                    JSONArray arr = pu.getJSONArray("Taken_Courses");
                    ArrayList<String> stringArrayList = new ArrayList<String>();
                    synchronized(stringArrayList) {
                        if (arr != null) {
                            for (int i = 0; i < arr.length(); i++) {
                                try {
                                    stringArrayList.add(arr.get(i).toString());
                                } catch (Exception exp) {
                                    e.printStackTrace();
                                }
                            }
                        }
                        courseAdapter = new ArrayAdapter<String>(student_to_faculty.this, android.R.layout.simple_spinner_dropdown_item, stringArrayList);
                        courseAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        course.setAdapter(courseAdapter);
                        Toast.makeText(student_to_faculty.this, " Everything is alright in course spinner ", Toast.LENGTH_LONG).show();
                    }//synchronized
                } else {
                    Toast.makeText(student_to_faculty.this, "new : " + e.toString() + " ", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
    }
});                

但是这段代码给了我

  

com.parse.exception:找不到查询结果。

1 个答案:

答案 0 :(得分:0)

您使用了错误的查询来获取用户。您必须使用以下方式来查询单个用户。试试看,看看。

ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("username", selectedInitial);
query.getFirstInBackground(new GetCallback<ParseUser>() {
  public void done(ParseUser pu, ParseException e) {
                if (e == null) {
                    ArrayList<String> stringArrayList = (ArrayList<String>)pu.get("Taken_Courses");
                    courseAdapter = new ArrayAdapter<String>(student_to_faculty.this, android.R.layout.simple_spinner_item, stringArrayList);
                    courseAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    course.setAdapter(courseAdapter);
                    Toast.makeText(student_to_faculty.this, " Everything is alright in course spinner ", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(student_to_faculty.this, "new : " + e.toString() + " ", Toast.LENGTH_LONG).show();
                }
  }
});

您使用的查询是,获取用户的给定objectId的ParseUser。当你拥有所需用户的objectID时,可以使用它。

希望这有帮助。