Android,recyclerview 未更新列表
Android, recyclerview not updating list
我有一个带行的回收视图,回收视图的数据源是一个列表。警报对象包含 getTime() 和 setTime(...)
我有一个 RecyclerItemClick 处理程序,所以我知道我点击了哪一行中的哪个元素。
在我的 onclick 中,我这样做了:
recyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(getContext(), recyclerView ,new RecyclerItemClickListener.OnItemClickListener() {
@Override public void onItemClick(View view, final int position) {
String elementID;
try
{
elementID = view.getResources().getResourceName(view.getId());
}
catch(Exception e)
{
elementID = "row";
}
if (elementID.contains("btnRemove"))
{
alarmList.remove(position);
mAdapter.notifyDataSetChanged();
}
if (elementID.contains("clockTime"))
{
//Set time click action
Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
final Alarm alarmToUpdate = mAdapter.alarmsList.get(position);
alarmToUpdate.setTime(selectedHour + ":" + selectedMinute);
mAdapter.RemoveAlarm(position);
mAdapter.alarmsList.clear();
mAdapter.AddAlarm(position, alarmToUpdate);
//mAdapter.alarmsList.get(position).setTime(selectedHour + ":" + selectedMinute);
Log.d(TAG, "pos: " + position + " & "+ mAdapter.alarmsList.get(position).getTime());
mAdapter.notifyDataSetChanged();
}
}, hour, minute, true);//Yes 24 hour time
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
else
{
//
}
}
@Override public void onLongItemClick(View view, int position) {
// do whatever
}
})
);
如你所见,我调用 adapter.notifyDataSetChanged(),
但时间没有改变。
我的 Log.d 结果显示我的 alarmList 已更新。我也可以删除一个警报(它上面的 onclick),并且 alarmsList.clear() 也在工作。
更新:
实施了建议的解决方案,删除了本地警报列表以使代码更清晰。
还用 "normal" textview 替换了 TimeClock 以确保问题不在那里。
结果:仍然没有更新的 recyclerview,log.d 表明我的列表已更新 :(.
我的适配器:
(现在有一个 public 临时列表)
public class AlarmsAdapter extends RecyclerView.Adapter<AlarmsAdapter.MyViewHolder> {
// make this private, find best way to update alarms.
public List<Alarm> alarmsList = new ArrayList<Alarm>();
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView time;
public TextView days;
public MyViewHolder(View view) {
super(view);
time = (TextView) view.findViewById(R.id.clockTime);
days = (TextView) view.findViewById(R.id.days);
}
}
public AlarmsAdapter() {
//
}
public void AddAlarm(Alarm alarm) {
alarmsList.add(alarm);
}
public void AddAlarm(int position, Alarm alarm) {
alarmsList.add(position, alarm);
}
public void RemoveAlarm(int position) {
alarmsList.remove(position);
}
public void UpdateAlarm(Alarm alarm) {
this.alarmsList = alarmsList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.timer_row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
//Log.d(TAG, "inBindViewHolder");
//Alarm alarm = alarmsList.get(position);
//holder.time.setFormat24Hour("04:20");
//holder.time.setText("04:20");
//holder.days.setText("mon, tue");
}
@Override
public int getItemCount() {
return alarmsList.size();
}
}
创建刷新数据集的方法假设方法名称是setData
mAdapter.setData(alarmsList);
然后
mAdapter.notifyDataSetChanged();
我找到了解决方案,我没有更新我的观点 -_-
我调用了 AlarmsAdapter.notifyDataSetChanged() 并且在 AlarmsAdapter.onBindViewHolde(...) 中你必须更新视图。
现在我这样做了,效果很好:
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
//Log.d(TAG, "inBindViewHolder");
Alarm alarm = alarmsList.get(position);
holder.time.setText(alarm.getTime());
holder.days.setText(alarm.getDays());
}
谢谢大家帮我找出我的愚蠢:)
我有一个带行的回收视图,回收视图的数据源是一个列表。警报对象包含 getTime() 和 setTime(...) 我有一个 RecyclerItemClick 处理程序,所以我知道我点击了哪一行中的哪个元素。
在我的 onclick 中,我这样做了:
recyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(getContext(), recyclerView ,new RecyclerItemClickListener.OnItemClickListener() {
@Override public void onItemClick(View view, final int position) {
String elementID;
try
{
elementID = view.getResources().getResourceName(view.getId());
}
catch(Exception e)
{
elementID = "row";
}
if (elementID.contains("btnRemove"))
{
alarmList.remove(position);
mAdapter.notifyDataSetChanged();
}
if (elementID.contains("clockTime"))
{
//Set time click action
Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
final Alarm alarmToUpdate = mAdapter.alarmsList.get(position);
alarmToUpdate.setTime(selectedHour + ":" + selectedMinute);
mAdapter.RemoveAlarm(position);
mAdapter.alarmsList.clear();
mAdapter.AddAlarm(position, alarmToUpdate);
//mAdapter.alarmsList.get(position).setTime(selectedHour + ":" + selectedMinute);
Log.d(TAG, "pos: " + position + " & "+ mAdapter.alarmsList.get(position).getTime());
mAdapter.notifyDataSetChanged();
}
}, hour, minute, true);//Yes 24 hour time
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
else
{
//
}
}
@Override public void onLongItemClick(View view, int position) {
// do whatever
}
})
);
如你所见,我调用 adapter.notifyDataSetChanged(), 但时间没有改变。 我的 Log.d 结果显示我的 alarmList 已更新。我也可以删除一个警报(它上面的 onclick),并且 alarmsList.clear() 也在工作。
更新: 实施了建议的解决方案,删除了本地警报列表以使代码更清晰。 还用 "normal" textview 替换了 TimeClock 以确保问题不在那里。 结果:仍然没有更新的 recyclerview,log.d 表明我的列表已更新 :(.
我的适配器: (现在有一个 public 临时列表)
public class AlarmsAdapter extends RecyclerView.Adapter<AlarmsAdapter.MyViewHolder> {
// make this private, find best way to update alarms.
public List<Alarm> alarmsList = new ArrayList<Alarm>();
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView time;
public TextView days;
public MyViewHolder(View view) {
super(view);
time = (TextView) view.findViewById(R.id.clockTime);
days = (TextView) view.findViewById(R.id.days);
}
}
public AlarmsAdapter() {
//
}
public void AddAlarm(Alarm alarm) {
alarmsList.add(alarm);
}
public void AddAlarm(int position, Alarm alarm) {
alarmsList.add(position, alarm);
}
public void RemoveAlarm(int position) {
alarmsList.remove(position);
}
public void UpdateAlarm(Alarm alarm) {
this.alarmsList = alarmsList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.timer_row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
//Log.d(TAG, "inBindViewHolder");
//Alarm alarm = alarmsList.get(position);
//holder.time.setFormat24Hour("04:20");
//holder.time.setText("04:20");
//holder.days.setText("mon, tue");
}
@Override
public int getItemCount() {
return alarmsList.size();
}
}
创建刷新数据集的方法假设方法名称是setData
mAdapter.setData(alarmsList);
然后
mAdapter.notifyDataSetChanged();
我找到了解决方案,我没有更新我的观点 -_- 我调用了 AlarmsAdapter.notifyDataSetChanged() 并且在 AlarmsAdapter.onBindViewHolde(...) 中你必须更新视图。
现在我这样做了,效果很好:
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
//Log.d(TAG, "inBindViewHolder");
Alarm alarm = alarmsList.get(position);
holder.time.setText(alarm.getTime());
holder.days.setText(alarm.getDays());
}
谢谢大家帮我找出我的愚蠢:)