package com.example.project.Holder;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.project.Model.User;
import com.example.project.R;
import java.util.List;
public class AttendanceViewHolder {
private Context mContext;
private UserAdapter userAdapter;
public void setConfig(RecyclerView recyclerView,Context context,List<User> users,List<String> keys){
mContext=context;
userAdapter=new UserAdapter(users,keys);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setAdapter(userAdapter);
}
class UserItemView extends RecyclerView.ViewHolder {
public ImageView attendanceitem_imageview;
private TextView attendanceitem_textview;
private String key;
public CheckBox attendance_checkBox;
public UserItemView(ViewGroup parent) {
super(LayoutInflater.from(mContext).
inflate(R.layout.item_attendance_bottom, parent, false));
attendanceitem_imageview = (ImageView) itemView.findViewById(R.id.attendanceitem_imageview);
attendanceitem_textview = (TextView) itemView.findViewById(R.id.attendanceitem_textview);
attendance_checkBox =(CheckBox) itemView.findViewById(R.id.attendance_checkBox);
}
public void bind(User user, String key) {
attendanceitem_textview.setText(user.getUsername());
this.key = key;
}
}
class UserAdapter extends RecyclerView.Adapter<UserItemView> {
List<User> userList;
private List<String> mkeys;
public UserAdapter(List<User> userList, List<String> mkeys) {
this.userList = userList;
this.mkeys = mkeys;
}
@NonNull
@Override
public UserItemView onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new UserItemView(parent);
}
@Override
public void onBindViewHolder(@NonNull UserItemView holder, int position) {
final int pos = position;
holder.attendance_checkBox.setChecked(userList.get(position).isCheckBox());
holder.attendance_checkBox.setTag(userList.get(position));
holder.bind(userList.get(position),mkeys.get(position));
Glide.with(holder.attendanceitem_imageview.getContext())
.load(userList.get(position).profileImageUrl)
.apply(new RequestOptions())
.into(holder.attendanceitem_imageview);
holder.attendance_checkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
CheckBox attendance_checkBox = (CheckBox) view;
User contact = (User) attendance_checkBox.getTag();
contact.setCheckBox(attendance_checkBox.isChecked());
userList.get(pos).setCheckBox(attendance_checkBox.isChecked());
}
});
}
@Override
public int getItemCount() {
return userList.size();
}
}
}
package com.example.project.Model;
import android.net.Uri;
import android.widget.ImageView;
public class User {
public String email;
public String profileImageUrl;
public String username;
public String uid;
public String pushToken;
public String job;
public String comment;
public Boolean checkBox;
public User() {
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getProfileImageUrl() {
return profileImageUrl;
}
public void setProfileImageUrl(String profileImageUrl) {
this.profileImageUrl = profileImageUrl;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getPushToken() {
return pushToken;
}
public void setPushToken(String pushToken) {
this.pushToken = pushToken;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public boolean isCheckBox() {
return checkBox;
}
public void setCheckBox(Boolean chechBox) {
this.checkBox = chechBox;
}
}
'''
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Boolean.booleanValue()' on a null object reference
at com.example.project.Model.User.isCheckBox(User.java:77)
at com.example.project.Holder.AttendanceViewHolder$UserAdapter.onBindViewHolder(AttendanceViewHolder.java:80)
at com.example.project.Holder.AttendanceViewHolder$UserAdapter.onBindViewHolder(AttendanceViewHolder.java:58)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6937)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6979)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5896)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6163)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6002)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5998)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2226)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1558)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1518)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:613)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4033)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3750)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4303)
at android.view.View.layout(View.java:21927)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
at android.view.View.layout(View.java:21927)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:21927)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
at android.view.View.layout(View.java:21927)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:779)
at android.view.View.layout(View.java:21927)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3080)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2590)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1721)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7598)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:966)
at android.view.Choreographer.doCallbacks(Choreographer.java:790)
at android.view.Choreographer.doFrame(Choreographer.java:725)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:951)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
'''
答案 0 :(得分:0)
onClick
内的样板代码太多。
仅使用
public void onClick(View view) {
userList.get(pos).setCheckBox(holder.attendance_checkBox.isChecked());
}
代替
public void onClick(View view) {
CheckBox attendance_checkBox = (CheckBox) view;
User contact = (User) attendance_checkBox.getTag();
contact.setCheckBox(attendance_checkBox.isChecked());
userList.get(pos).setCheckBox(attendance_checkBox.isChecked());
}
答案 1 :(得分:0)
我认为您需要尝试以下代码
用于初始化活动中的适配器
UserAdapter userAdapter = new UserAdapter(MainActivity.this, userList);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setAdapter(userAdapter);
,对于UserAdapter来说,
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.Holder> {
private Context mContext;
private List<User> userList;
public UserAdapter(Context mContext, List<User> userList) {
this.mContext = mContext;
this.userList = userList;
}
@Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
View rowView = LayoutInflater.from(mContext).inflate(R.layout.item_attendance_bottom, parent, false);
return new Holder(rowView);
}
@Override
public void onBindViewHolder(Holder holder, int position) {
User userModel = userList.get(position);
if (userModel.isCheckBox()) {
holder.attendance_checkBox.setChecked(true);
} else {
holder.attendance_checkBox.setChecked(false);
}
/* further set date to textview*/
}
@Override
public int getItemCount() {
return userList.size();
}
public class Holder extends RecyclerView.ViewHolder {
public ImageView attendanceitem_imageview;
private TextView attendanceitem_textview;
public CheckBox attendance_checkBox;
public Holder(View itemView) {
super(itemView);
attendanceitem_imageview = (ImageView) itemView.findViewById(R.id.attendanceitem_imageview);
attendanceitem_textview = (TextView) itemView.findViewById(R.id.attendanceitem_textview);
attendance_checkBox = (CheckBox) itemView.findViewById(R.id.attendance_checkBox);
attendance_checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
int position = getAdapterPosition();
User tmpUser = userList.get(getAdapterPosition());
if (isChecked) {
tmpUser.setCheckBox(true);
notifyItemChanged(position);
} else {
tmpUser.setCheckBox(false);
notifyItemChanged(position);
}
}
});
}
}
}