使用.clear()后ArrayList不更新 [英] ArrayList not updating after using .clear()

查看:102
本文介绍了使用.clear()后ArrayList不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android开发的新手,所以请多多包涵.

I'm new to Android development, so please bear with me.

我有一个自定义ArrayAdapter,我想通过向下滑动以刷新来进行更新.

I have a custom ArrayAdapter which I want to update by swiping down to refresh.

我了解要执行此操作,我需要:

I understand that in order to do this I need to:

  • 使用.clear()清除保存我的数据的ArrayList
  • 使用新数据(使用getAbsentTeachers()方法)更新ArrayList
  • 在ArrayAdapter上使用notifyDataSetChanged()

清除了ArrayList之后,调用getAbsentTeachers()和notifyDataSetChanged(),即使getAbsentTeachers()被调用,ArrayList仍保持为空.我知道这是因为ListView中什么都没有显示.

After I clear the ArrayList, call getAbsentTeachers() and notifyDataSetChanged() the ArrayList appears to remain empty even though getAbsentTeachers() is beeing called. I know this because nothing is displayed in my ListView.

getAbsentTeachers()会在我的应用程序首次启动时很好地填充ArrayList,但是当我再次调用它以更新ArrayList时,它似乎无法正常工作.

getAbsentTeachers() populates the ArrayList fine when my app is first launched however it doesn't seem to work when I call it again to update the ArrayList.

关于为什么数组不更新的任何想法吗?

Any ideas as to why the array is not beeing updated?

MainActivity.java: 软件包uk.co.bobsmith.drawview.drawviewtest;

MainActivity.java: package uk.co.bobsmith.drawview.drawviewtest;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private ArrayList<Absent> absences = new ArrayList<>();
    private SwipeRefreshLayout swipeContainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        //---------------------INITIALISING PARSE-------------------------------
        Parse.enableLocalDatastore(this);

        // Add your initialization code here
        Parse.initialize(new Parse.Configuration.Builder(getApplicationContext())
                //Information obscured for privacy - tested and working though.
                .applicationId("")
                .clientKey(null)
                .server("")
                .build()
        );

        ParseUser.enableAutomaticUser();
        ParseACL defaultACL = new ParseACL();
        // Optionally enable public read access.
        // defaultACL.setPublicReadAccess(true);
        ParseACL.setDefaultACL(defaultACL, true);

        ParseInstallation.getCurrentInstallation().saveInBackground();
        ParsePush.subscribeInBackground("Absent");

        //-----------------------CREATE ADAPTER OBJECT--------------------------------

        getAbsentTeachers();

        final ListView lv = (ListView)findViewById(R.id.listView);
        final ArrayAdapter<Absent> adapter = new absenceArrayAdapter(this, 0, absences);
        lv.setAdapter(adapter);

        swipeContainer = (SwipeRefreshLayout) findViewById(R.id.swipeContainer);
        // Setup refresh listener which triggers new data loading
        swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // Code to refresh the list.
                absences.clear();
                getAbsentTeachers();
                adapter.notifyDataSetChanged();
                swipeContainer.setRefreshing(false);
                // Call swipeContainer.setRefreshing(false) when finished.
            }
        });

    }

    // Populates absences ArrayList with absence data
    public void getAbsentTeachers() {

        ParseQuery<ParseObject> query = ParseQuery.getQuery("Absences");

        query.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> objects, ParseException e) {
                if(e == null){

                    for(ParseObject object : objects){

                        String name = String.valueOf(object.get("name"));
                        Log.i("Name", name);
                        String desc = String.valueOf(object.get("description"));
                        Log.i("Description", desc);

                        absences.add(new Absent(name, desc, "employees"));

                    }

                } else {

                    Log.i("Get data from parse", "There was an error getting data!");
                    e.printStackTrace();

                }
            }
        });

    }



    //custom ArrayAdapter
    class absenceArrayAdapter extends ArrayAdapter<Absent> {

        private Context context;
        private List<Absent> absencesList;

        public absenceArrayAdapter(Context context, int resource, ArrayList<Absent>                 objects) {
            super(context, resource, objects);

            this.context = context;
            this.absencesList = objects;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            //get the property we are displaying
            Absent teacher = absencesList.get(position);

            //get the inflater and inflate the XML layout for each item
            LayoutInflater inflater = (LayoutInflater)        context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.custom_listview, null);

            TextView name = (TextView) view.findViewById(R.id.name);
            TextView description = (TextView) view.findViewById(R.id.description);
            ImageView teacherImage = (ImageView) view.findViewById(R.id.imageView);

            String teacherName = teacher.getTeacherName();
            name.setText(teacherName);

            int descriptionLength = teacher.getDescription().length();
            if(descriptionLength >= 75){
                String descriptionTrim = teacher.getDescription().substring(0, 75) + "...";
                description.setText(descriptionTrim);
            }else{
                description.setText(teacher.getDescription());
            }

            return view;

        }

    }
}

推荐答案

将这两行移动到Parse调用的done中的if-else之后.

Move these two lines to after the if-else within the done of the Parse call.

adapter.notifyDataSetChanged();
swipeContainer.setRefreshing(false);

您当前不等待解析调用完成,然后再通知适配器.

You currently are not waiting for the Parse call to finish before notifying the adapter.

另一种解决方案是更改

absences.add(new Absent(name, desc, "employees"));

adapter.add(new Absent(name, desc, "employees"));

这篇关于使用.clear()后ArrayList不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆