因此,当用户自行添加时,我尝试更新recyclerview
。现在,我正在尝试实现一项功能,用户可以点击CardView
中的recyclerview
,CardView
中的文字立即更新。根据我的方法,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
答案 0 :(得分:0)
在RecyclerView.Adapter
中为DataSet
更改创建一个setter。
在notifyDataSetChanged
onPostExecute
UsersListPopulate
中与Adapter
一起使用,看看会发生什么。
另外,为什么你的Checked
是静态的?