我有一个带有cardview的recyclerview,在cardview内我有一堆单选按钮和2个时间选择器和2个textview.i需要在点击按钮时动态地向recyclerview添加新卡。我实现了一个适应器,点击按钮项目正在添加到列表。
问题是当我添加卡片并更改时间或选择单选按钮时,再次单击添加按钮旧卡片被重新校对,我输入的dta消失了,我怎么能得到它
我有一些截图如何工作
请帮我解决这个问题,谢谢
这是我的代码:
适配器代码
@Override
public LabourWorkAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.labour_work_card, parent, false);
return new LabourWorkAdapter.ViewHolder(v);
}
@Override
public void onBindViewHolder(final LabourWorkAdapter.ViewHolder holder, final int position) {
final LabourWorkList listItem = labourWork.get(position);
holder.fromTime.setText(listItem.getFromDate());
holder.toTime.setText(listItem.getToDate());
// holder.cDay.setText(listItem.getFullDay());
holder.fromTimePicker.setOnClickListener(new View.OnClickListener() {
int hour_x = 0;
int minute_x = 0;
@Override
public void onClick(View view) {
TimePickerDialog mTimePicker = new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
String hr, mn, timeMode = "AM";
if (selectedHour > 12){
selectedHour = selectedHour-12;
timeMode = "PM";
}
if (selectedHour < 10){
hr = "0"+selectedHour;
}else{
hr = ""+selectedHour;
}
if (selectedMinute < 10){
mn = "0"+selectedMinute;
}else{
mn = ""+selectedMinute;
}
holder.fromTime.setText( hr + ":" + mn + timeMode);
}
}, hour_x, minute_x, false);
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
});
holder.toTimePicker.setOnClickListener(new View.OnClickListener() {
int hour_x = 0;
int minute_x = 0;
@Override
public void onClick(View view) {
TimePickerDialog mTimePicker = new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
String hr, mn, timeMode = "AM";
if (selectedHour > 12){
selectedHour = selectedHour-12;
timeMode = "PM";
}
if (selectedHour < 10){
hr = "0"+selectedHour;
}else{
hr = ""+selectedHour;
}
if (selectedMinute < 10){
mn = "0"+selectedMinute;
}else{
mn = ""+selectedMinute;
}
holder.toTime.setText( hr + ":" + mn + timeMode);
}
}, hour_x, minute_x, false);
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
});
holder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
labourWork.remove(position);
notifyItemRemoved(position);
notifyDataSetChanged();
notifyItemRangeChanged(position, labourWork.size());
CharSequence text = "Position "+ position;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
});
// holder.labourWorkDay
}
@Override
public int getItemCount() {
return labourWork.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView fromTime, toTime;
Button fromTimePicker, toTimePicker;
ImageView delete;
public ViewHolder(View itemView) {
super(itemView);
fromTime = (TextView) itemView.findViewById(R.id.fromTime);
toTime = (TextView) itemView.findViewById(R.id.toTime);
fromTimePicker = (Button) itemView.findViewById(R.id.fromTimePicker);
toTimePicker = (Button) itemView.findViewById(R.id.toTimePicker);
delete = (ImageView) itemView.findViewById(R.id.deleteLabourCard);
}
}
按钮点击监听器
rId.setText(tempLabourId);
recyclerView = (RecyclerView) findViewById(R.id.workDetailsRecycler);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
workList = new ArrayList<>();
addItem = (FloatingActionButton) findViewById(R.id.addLabor);
addItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addToRecyclerView(sPosition);
sPosition++;
}
});
public void addToRecyclerView(int position){
LabourWorkList listItem = new LabourWorkList(position);
recyclerView.findViewHolderForAdapterPosition(position);
workList.add(listItem);
adapter = new LabourWorkAdapter(workList, LabourWork.this);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
答案 0 :(得分:1)
Recyclerview
每次都会刷新,因为每次添加新项目并调用notifyDataSetChanged
方法时都会设置适配器。
要解决此问题,请仅将适配器设置一次,然后将新项目添加到列表并调用
adapter.notifyItemRangeInserted(lastListSize, newListSize);