从Parse Object获取ArrayList

时间:2015-02-17 23:40:23

标签: java android arrays arraylist parse-platform

我有一个Parse Object,其值包含User Id的ArrayList。我无法弄清楚如何检索整个ArrayList(不只是列表中的1个值)我的代码看起来像这样..但总是带有错误(数组返回空):

ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Food");
    query.whereEqualTo("objectId", parseId);
    query.getFirstInBackground(new GetCallback<ParseObject>() {
        @Override
        public void done(ParseObject parseObject, ParseException e) {
            if (e == null) {
                  ArrayList<String> list = (ArrayList<String>) 
   parseObject.get("userList");// This is where I don't know what to use to get the Array

}

else {
                AlertDialog.Builder builder = new AlertDialog.Builder(ThisActivity.this);
                builder.setTitle(R.string.error_title)
                        .setMessage("error")
                        .setPositiveButton("ok", null);
                AlertDialog dialog = builder.create();
                dialog.show();
            }
         }
    });

我想加载用户id的数组(来自对象)并在此活动的其他位置使用该数组。对象中的所有其他值都是String值,并使用.getString加载。

我知道我可能会离开,所以任何帮助都会受到赞赏。

3 个答案:

答案 0 :(得分:1)

您应该替换此行:

parseObject.get("userList");// This is where I don't know what to use to get the Array

收件人:

JSONArray array = parseObject.getJSONArray("userList");

它应该可以正常工作。

答案 1 :(得分:0)

//Try This To Retrieve All Records In Server using findInBackground()
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Food");
query.whereEqualTo("objectId", parseId);
query.findInBackground(new GetCallback<ParseObject>() {
@Override
public void done(List<ParseObject> parseObject, ParseException e) {
    if (e == null) {
    for (int i = 0; i<parseObject.size(); i++){
        ParseObject stObj = parseObject.get(i);
        List<String>list = stObj.getList("userList");
        //Transfer list content to array stockArr
        String[] stockArr = new String[list.size()];
        stockArr = list.toArray(stockArr);
        //list contains userlist - To add to list without transferring content to array use next line of code below
        list.add("Add This To The List");
        //To save updated list use next two lines of code below
        stObj.put("userList", list);
        stObj.saveInBackground();
    }
    } else {
        AlertDialog.Builder builder = new AlertDialog.Builder(ThisActivity.this);
        builder.setTitle(R.string.error_title)
                .setMessage("error")
                .setPositiveButton("ok", null);
        AlertDialog dialog = builder.create();
        dialog.show();
    }
    }
});


//Try This To Retrieve ONLY First Record In Server using getFirstInBackground()
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Food");
query.whereEqualTo("objectId", parseId);
query.getFirstInBackground(new GetCallback<ParseObject>() {
@Override
public void done(List<ParseObject> parseObject, ParseException e) {
    if (e == null) {
        ParseObject stObj = parseObject.get(0);
        List<String>list = stObj.getList("userList");
        //Transfer list content to array stockArr
        String[] stockArr = new String[list.size()];
        stockArr = list.toArray(stockArr);
    } else {
        AlertDialog.Builder builder = new AlertDialog.Builder(ThisActivity.this);
        builder.setTitle(R.string.error_title)
                .setMessage("error")
                .setPositiveButton("ok", null);
        AlertDialog dialog = builder.create();
        dialog.show();
    }
    }
});

答案 2 :(得分:-1)

我认为您不必使用getFirstInBackground(只检索一个元素),但您必须使用findInBackground(以检索所有对象)。