为什么我无法从我的活动中调用适配器方法

时间:2018-03-07 10:11:05

标签: android

我在Adapter类中定义了一个方法 removeItem ,我试图从我的活动中调用该方法,但是当我使用带有我的适配器的点运算符时它不会识别我的方法课堂参考。请帮帮我。

public class RecyclerViewCalendarAdapter extends RecyclerView.Adapter<RecyclerViewCalendarAdapter.ViewHolder> {

List<EventDetails_Helper> list;
String startdate,enddate;
List<Date> dates;
MaterialCalendarView calendar_view;
AppBarLayout appbar_calendar;


RecyclerViewCalendarAdapter(List<EventDetails_Helper> list, MaterialCalendarView calendar_view, AppBarLayout appbar_calendar){

    this.list = list;
    this.calendar_view = calendar_view;
    this.appbar_calendar = appbar_calendar;

}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerviewcalendar_item, parent, false);

    ViewHolder vh = new ViewHolder(v);

    return vh;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

    holder.txtview_eventname.setText(list.get(position).name);
    holder.txtview_enddate.setText(list.get(position).target_date);

    holder.linearlayout_calendar.setTag(R.id.startdate,"2018-02-17");
    holder.linearlayout_calendar.setTag(R.id.enddate,list.get(position).target_date);


    holder.linearlayout_calendar.setOnClickListener(new CalendarLinearListener());
}

@Override
public int getItemCount() {
    return list.size();
}

public void removeItem(int position) {
    list.remove(position);
    notifyItemRemoved(position);
}

public void restoreItem(EventDetails_Helper item, int position) {
    list.add(position, item);
    // notify item added by position
    notifyItemInserted(position);
}

    public class ViewHolder extends RecyclerView.ViewHolder{

    TextView txtview_eventname,txtview_enddate,txtview_startdate;
    LinearLayout linearlayout_calendar;
    public RelativeLayout viewBackground, viewForeground;



    public ViewHolder(View itemView) {
        super(itemView);

        linearlayout_calendar = (LinearLayout) itemView.findViewById(R.id.linearlayout_calendar);
        txtview_eventname = (TextView) itemView.findViewById(R.id.txtview_eventname);
        txtview_startdate = (TextView) itemView.findViewById(R.id.txtview_startdate);
        txtview_enddate = (TextView) itemView.findViewById(R.id.txtview_enddate);

        viewBackground = itemView.findViewById(R.id.view_background);
        viewForeground = itemView.findViewById(R.id.view_foreground);

    }

}

private class CalendarLinearListener implements View.OnClickListener {
    @Override
    public void onClick(View v) {

        appbar_calendar.setExpanded(true);
        calendar_view.clearSelection();

        startdate = (String) v.getTag(R.id.startdate);
        enddate = (String) v.getTag(R.id.enddate);

        dates = getDates(startdate,enddate);
        datemarkstyle(dates);

    }
}

private static List<Date> getDates(String dateString1, String dateString2)
{
    ArrayList<Date> dates = new ArrayList<Date>();
    DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");

    Date date1 = null;
    Date date2 = null;

    try {
        date1 = df1 .parse(dateString1);
        date2 = df1 .parse(dateString2);
    }
    catch (Exception e) {
        e.printStackTrace();
    }

    Calendar cal1 = Calendar.getInstance();
    cal1.setTime(date1);


    Calendar cal2 = Calendar.getInstance();
    cal2.setTime(date2);

    while(!cal1.after(cal2))
    {
        dates.add(cal1.getTime());
        cal1.add(Calendar.DATE, 1);
    }
    return dates;
}

private void datemarkstyle(List<Date> dates) {

    try{

        calendar_view.setCurrentDate(dates.get(0));
        int i;
        int year,month,day;

        for(i=0;i<dates.size();i++){

            calendar_view.setDateSelected(dates.get(i),true);
        }

    }catch(Exception e){

        e.printStackTrace();
    }

}}

在我的活动中,我正在执行以下操作: -

        @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position) {

        if(viewHolder instanceof RecyclerViewCalendarAdapter.ViewHolder){

            String name = list.get(viewHolder.getAdapterPosition()).name;

            EventDetails_Helper deleteditem = list.get(viewHolder.getAdapterPosition());
            int deleteIndex = viewHolder.getAdapterPosition();

            mAdapter.removeItem
        }
    }

但问题是它不允许我访问方法 removeItem();

2 个答案:

答案 0 :(得分:0)

我认为您已将mAdapter声明为 RecyclerView.Adapter 不是 RecyclerViewCalendarAdapter

改变它

使用此

<强> RecyclerViewCalendarAdapter mAdapter;

而不是

<强> RecyclerView.Adapter mAdapter;

比你可以使用mAdapter.removeItem(deleteIndex);

答案 1 :(得分:0)

我不知道这是你的sintax在这里还是在项目中但是

git stash -p

不正确

应该是

mAdapter.removeItem 


mAdapter.removeItem(position);

并确保使用mAdapter.removeItem(deleteIndex);实例