Android,recyclerview没有更新列表

时间:2017-11-06 19:56:40

标签: android list android-recyclerview alarm

我有一个包含行的recyclelerview,recyclelerview的数据源是一个List。报警对象包含getTime()&设置时间(...) 我有一个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),alarmList.clear()也可以。

更新: 实施建议的解决方案,删除本地警报器以使代码更清晰。 还用一个" normal"替换了TimeClock。 textview以确保问题不在那里。 结果:仍然没有更新的recyclerview,log.d显示我的列表已更新:(。

我的适配器: (现在临时公开名单)

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();
}

}

2 个答案:

答案 0 :(得分:2)

创建方法以刷新数据集,假设方法名称为setDat

mAdapter.setData(alarmsList);

然后做

mAdapter.notifyDataSetChanged();

答案 1 :(得分:0)

我找到了解决方案,我没有更新我的观点-_- 我调用了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());
}

谢谢大家帮助我找出我的愚蠢行为:)