0
RecyclerViewアダプターのRecyclerViewを含むボトムシートのダイアログフラグメントがあります。選択するアイテムのリストがあり、ユーザーが1つのアイテムを選択すると、フラグメントのTextViewが色とテキストを変更する必要があります。アダプターからのビューの変更
アダプタ:
public class ServiceStaffAdapter extends RecyclerView.Adapter<ServiceStaffAdapter.ViewHolder> {
List<StaffMoreStoreList> StaffMoreStoreItems;
private Context context;
private SharedPreferences spStore;
private ArrayList<String> staffArray, initialsArray = new ArrayList<String>();
private View vDialogService;
private String staffArrayString, staffIdString;
public ServiceStaffAdapter(List<StaffMoreStoreList> StaffMoreStoreItems, Context context){
super();
this.StaffMoreStoreItems = StaffMoreStoreItems;
this.context = context;
}
@Override
public ServiceStaffAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_staff_list, parent, false);
ServiceStaffAdapter.ViewHolder viewHolder = new ServiceStaffAdapter.ViewHolder(v);
SharedPreferences sp2= context.getSharedPreferences("STORE", Context.MODE_PRIVATE);
staffArrayString = sp2.getString("staffArray","");
staffArray = new ArrayList<String>();
if(staffArrayString.equals("")){
staffArray = new ArrayList<String>();
}else{
String replace = staffArrayString.replace("[","");
String replace1 = replace.replace("]","");
List<String> arrayList = new ArrayList<String> (Arrays.asList(replace1.split(",")));
staffArray = new ArrayList<String>();
for(String fav:arrayList){
staffArray.add(String.valueOf(fav.trim()));
}
}
//array of string for staffs selected
return viewHolder;
}
@Override
public void onBindViewHolder(final ServiceStaffAdapter.ViewHolder holder, int position) {
final StaffMoreStoreList staffMoreStoreList = StaffMoreStoreItems.get(position);
holder.tvStaffName.setText(staffMoreStoreList.getStaffName());
StaffMoreStoreItems.get(position).getStaffId();
Log.e("staffArray size", String.valueOf(staffArray.size()));
Log.e("staff array", staffArray.toString());
staffIdString = staffMoreStoreList.getStaffId();
//pre-select staffs
if(staffArray.contains(StaffMoreStoreItems.get(position).getStaffId())){
holder.profile_image.setBorderColor(ContextCompat.getColor(context, R.color.primary));
}else{
holder.profile_image.setBorderColor(ContextCompat.getColor(context, R.color.transparent));
}
holder.cardStaff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(staffArray.contains(staffMoreStoreList.getStaffId())){
holder.profile_image.setBorderColor(ContextCompat.getColor(context, R.color.transparent));
staffArray.removeAll(Collections.singleton(staffMoreStoreList.getStaffId()));
}else{
holder.profile_image.setBorderColor(ContextCompat.getColor(context, R.color.primary));
staffArray.add(staffMoreStoreList.getStaffId());
}
spStore = context.getSharedPreferences("STORE", Context.MODE_PRIVATE);
SharedPreferences.Editor editor1 = spStore.edit();
editor1.putString("staffArray", staffArray.toString());
editor1.commit();
}
});
}
@Override
public int getItemCount() {
return StaffMoreStoreItems.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
private TextView tvStaffName;
private CardView cardStaff;
private CircleImageView profile_image;
private ViewHolder(View itemView) {
super(itemView);
tvStaffName = (TextView) itemView.findViewById(R.id.tvStaffName);
cardStaff = (CardView) itemView.findViewById(R.id.cardStaff);
profile_image = (CircleImageView) itemView.findViewById(R.id.profile_image);
}
}
}
私はRecyclerViewを含む私のフラグメント内のTextViewを変更するアダプターでCardViewsをクリックしながら。 TextViewはstaffArrayのサイズに応じて変更されます。
おかげで、私はあなたのステップに従っている、onclickを実装するために要求されますがhowerマウス、来ます最後のステップでは、アダプタ内の.setOnClickListner() – JerryKo
メソッドを解決できません。onclickのセッターを作成する必要があります。 adapter.yourSetterMethod(this); – xbadal