本文介绍了解析findInBackground不会添加到全局arraylist吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码段,其中对名称进行了少量修改.

This is a snippet of my code with small edits in names.

String[] items;
ArrayList<String>tempListItems;

public void initList() {
        if(query()){
            items = new String[tempListItems.size()];
            items = tempListItems.toArray(items);
            Arrays.sort(items);

            Log.d("HSearch - initList", "Generate Clean List");
            Log.d("initList - temp size", Integer.toString(tempListItems.size()));
            Log.d("initList - items size", Integer.toString(items.length));
        }
    }

private boolean query() {
        tempListItems = new ArrayList<String>();
        ParseQuery<ParseObject> query = ParseQuery.getQuery("Table");
        query.orderByAscending("name");
        query.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> list, com.parse.ParseException e) {
                if (e == null) {
                    for (ParseObject name : list) {
                        tempListItems.add(name.getString("name"));
                    }
                } else {
                    Log.d("score", "Error: " + e.getMessage());
                }
            }
        });
        return true;
    }

我的问题出在query()中,如果我在done()之后检查tempListItems的大小,则该大小不为空.但是,一旦完成了query()并返回到initList(),如果再次检查tempListItems,它将为空.

My problem is in query(), if I check the size of tempListItems after done() the size is not empty. However, once query() is done and I return back to initList(), if I check tempListItems again it is empty.

done()是query()中的一种方法.我忘记了它的技术术语!

done() is a method within query(). I forgot the technical term of it!

我不确定如何确保在query()期间对tempListItems的更改,在返回后,done()仍然保留.

I'm unsure of how to make sure changes to tempListItems during query() and done() stays after it returns.

推荐答案

String[] items;
final ArrayList<String>tempListItems;
{
    tempListItems= new ArrayList<>();
}

public void initList() {
    if(query()){
        items = new String[tempListItems.size()];
        items = tempListItems.toArray(items);
        Arrays.sort(items);

        Log.d("HSearch - initList", "Generate Clean List");
        Log.d("initList - temp size", Integer.toString(tempListItems.size()));
        Log.d("initList - items size", Integer.toString(items.length));
    }
}

private boolean query() {
    ParseQuery<ParseObject> query = ParseQuery.getQuery("Table");
    query.orderByAscending("name");
    query.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> list, com.parse.ParseException e) {
            if (e == null) {
                for (ParseObject name : list) {
                    tempListItems.add(name.getString("name"));
                }
            } else {
                Log.d("score", "Error: " + e.getMessage());
            }
        }
    });
    return true;
}

将您的方法公开以在其他类中使用.

Make your methods public to use in other classes.

这篇关于解析findInBackground不会添加到全局arraylist吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 07:11