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

时间:2018-08-25 03:05:41

标签: 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;
    }

}

请给我一点帮助,因为我一直在尝试将字符串添加到列表中,但是名称的大小仍然显示0。我不知道为什么会这样。在logcat中,我看不到任何值,它仍显示0值。我无法在此处发布日志,因为Stack Overflow不接受我的问题,并显示了一些错误。

2 个答案:

答案 0 :(得分:5)

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

您正在之前遍历列表,实际上未添加任何值。这是你的问题。

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

希望这会有所帮助。

答案 1 :(得分:1)

根本原因::您正在使用query.findInBackground将数据添加到后台线程中的数组列表中,与此同时,您正在访问UI线程中的数组列表值。由于2个线程正在运行并发,因此在某些情况下,UI线程在后台线程之前执行或运行。这就是为什么您的数组列表为空。

解决方案::准备好数据后,将适配器设置为RecyclerView

ThirdFragment.java

public class Thirdfragment extends Fragment {

    List<String> 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) {

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

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

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

        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);
                        }

                        // The data (names array list) is already to use.
                        String[] namesArray = new String[names.size()];
                        namesArray = names.toArray(namesArray);
                        MyAdapter adapter = new MyAdapter(namesArray);
                        rv.setAdapter(adapter);
                    } else {
                        Log.d("no data", "left or right");
                    }
                } else {
                    Log.d("awserror", "left or right");
                }
            }
        });

        return rootView;
    }
}