将字符串添加到ArrayList但仍然看到大小为0

Sub*_*dru -3 java android

public class Thirdfragment extends Fragment {

    List names = new ArrayList();


    public Thirdfragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        ParseQuery<ParseObject> query = ParseQuery.getQuery("serviceop");    
        query.findInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> objects, ParseException e) {
                if (e == null) {

                    if (objects.size() > 0) {    
                        for (ParseObject object : objects) {    
                            String Currentdatatype = object.getString("name");

                       names.add("s");    
                       Log.d("added",Currentdatatype);    
                        }
                    } else {    
                        Log.d("no data","left or right");
                    }
                } else {
                    Log.d("awserror","left or right");
                }
            }
        });

    // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_blank, container, false);

        RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.rv_recycler_view);
        rv.setHasFixedSize(true);

        System.out.println("Using for loop");
        System.out.println("--------------");
        for (int i = 0; i < names.size(); i++) {
            System.out.println(names.get(i));
        }

        System.out.println(names.size());
        MyAdapter adapter = new MyAdapter(new String[]{"ssd","sd"});
        rv.setAdapter(adapter);

        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        rv.setLayoutManager(llm);

        return rootView;
    }

}
Run Code Online (Sandbox Code Playgroud)

我在这里需要一点手,因为我一直试图在我的列表中添加字符串,但是名字的大小仍然显示我0.我不知道为什么会发生这种情况.在logcat中我没有看到任何值,它仍然显示0值.我无法在此发布日志,因为Stack Overflow不接受我的问题并显示一些错误.

ira*_*voi 5

findInBackground方法是异步的(在后台线程中执行).传递给此方法的FindCallback将在执行查询最终执行.

实际添加任何值之前,您将遍历列表.这是你的问题.

使用find()方法获取ParseObjects(但请记住这是一个阻塞操作),或者将迭代逻辑移动到FindCallback.

希望这可以帮助.