更新RecyclerView列表的最佳方法是什么,因为我的方法出错了?

时间:2016-02-28 19:00:19

标签: java android nullpointerexception android-recyclerview

因此,当用户自行添加时,我尝试更新recyclerview。现在,我正在尝试实现一项功能,用户可以点击CardView中的recyclerviewCardView中的文字立即更新。根据我的方法,NullPointerException失败了,因为adapter显然是空的。这是我的源代码:

public class UsersList extends Fragment {
    private ArrayList<UserInfo> userInfo;
    RecyclerView recyclerView;
    static UsersListAdapter adapter;
    RecyclerView.LayoutManager layoutManager;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.userslistones, container, false);
        recyclerView = (RecyclerView) rootView.findViewById(R.id.usersrecycler);
        recyclerView.setHasFixedSize(true);
        userInfo = new ArrayList<UserInfo>();
        adapter = new UsersListAdapter(userInfo);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(adapter);
        conditionNames = new ArrayList<>();
        populateUsersList();
        return rootView;
    }

    public void populateUsersList(){
        UsersListPopulate usersListPopulate = new UsersListPopulate(userInfo, adapter);
        usersListPopulate.execute();
    }

    public class UsersListPopulate extends AsyncTask<String, String, String> {
        ArrayList<UserInfo> list;
        UsersListAdapter adapter;

        public UsersListPopulate(ArrayList<UserInfo> list, UsersListAdapter adapter){
            this.list = list;
            this.adapter = adapter;
        }


        @Override
        protected String doInBackground(String... params) {
            ...
        }

        @Override
        protected void onPostExecute(String postData) {
            try {
                list.clear();
                JSONArray jsonArray = new JSONArray(postData);
                for(int i=0; i < jsonArray.length(); i++){
                    String forename = jsonArray.getJSONObject(i).getString("forename");
                    String surname = jsonArray.getJSONObject(i).getString("surname");
                    UserInfo userInfo = new UserInfo(forename, surname);
                    list.add(userInfo);
                }
                recyclerView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    public class MarkUser extends AsyncTask<String, String, String> {
        @Override
        protected String doInBackground(String... params) {
            ...
        }

        @Override
        protected void onPostExecute(String postData) {
            populateUsersList();
        }
    }
}

我在NullPointerException的{​​{1}}方法调用中不断获得populateUsersList()

我的MarkUser

stack trace

1 个答案:

答案 0 :(得分:0)

RecyclerView.Adapter中为DataSet更改创建一个setter。

notifyDataSetChanged onPostExecute UsersListPopulate中与Adapter一起使用,看看会发生什么。

另外,为什么你的Checked是静态的?