如何使用Recyclerview中单击的适配器位置获取数据?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用Recyclerview中单击的适配器位置获取数据?相关的知识,希望对你有一定的参考价值。

我需要专门获取我之前在Recyclerview中设置为textview的Row Id的数据。

在这里,我得到了我用左手滑动的适配器的位置。使用它我想获得我之前设置的隐藏ID并删除SQlite中的数据。

recycler view adapter.Java

public class RecyclerviewAdapter_list extends RecyclerView.Adapter<RecyclerviewAdapter_list.MyViewHolder> {

    private Context mContext;
    private List<myAlarms> myAlarmsdata;
    Database_Helper database_helper;

    public RecyclerviewAdapter_list(Context mContext, List<myAlarms>myAlarmsdata){
        this.mContext = mContext;
        this.myAlarmsdata =myAlarmsdata;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        view = inflater.inflate(R.layout.cardview_alarm, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        myAlarms myAlarms =myAlarmsdata.get(position);

        String mTimeSplit = myAlarms.getALARM_TIME();
        SpannableString mtime=  new SpannableString(mTimeSplit);
        mtime.setSpan(new RelativeSizeSpan(2f), 0,5, 0);
        holder.mTime.setText(mtime);

        holder.mTitle.setText(myAlarms.getALARM_TITLE());
        holder.mTitle.setTag(myAlarms.getALARM_ID());

        String mStatus = myAlarms.getALARM_STATUS();
        if(mStatus.equals("Active") && !holder.mbtn.isChecked()){
            holder.mbtn.setChecked(true);
        }else{
            holder.mbtn.setChecked(false);
        }
    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView mTime,mTitle;
        com.suke.widget.SwitchButton mbtn;
        public RelativeLayout viewBackground;
        public RelativeLayout viewForeground;
        public MyViewHolder(View itemView) {
            super(itemView);

            mTime = (TextView) itemView.findViewById(R.id.mTime);
            mTitle = (TextView) itemView.findViewById(R.id.mTitle);
            mbtn = (com.suke.widget.SwitchButton)itemView.findViewById(R.id.mbtn);
            viewBackground = itemView.findViewById(R.id.view_background);
            viewForeground = itemView.findViewById(R.id.view_foreground);
            database_helper = new Database_Helper(mContext);

            mbtn.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(SwitchButton view, boolean isChecked) {

                    if(isChecked == false) {
                        String mStatus = "InActive";
                        Integer mRowId = (Integer) mTitle.getTag();
                        database_helper.updateStatus(mRowId,mStatus);
                    }else{
                        if(isChecked == true){
                            String mStatus = "Active";
                            Integer mRowId = (Integer) mTitle.getTag();
                            database_helper.updateStatus(mRowId,mStatus);
                        }
                    }
                }
            });
        }
    }
    public void removeItem(int position) {
        myAlarmsdata.remove(position);
        notifyItemRemoved(position);

       //need to get the data by using the adapter position
    }

}

任何人都可以通过使用适配器位置帮助我获取数据...

答案

你在myAlarmsdata有它。

public void removeItem(int position) {
    myAlarms myAlarms = myAlarmsdata.get(position);
    int id = myAlarms.getALARM_ID();

    myAlarmsdata.remove(position);
    notifyItemRemoved(position);

   // use id to get the data by using the adapter position
}

我理解正确吗?

以上是关于如何使用Recyclerview中单击的适配器位置获取数据?的主要内容,如果未能解决你的问题,请参考以下文章

如何从recyclerview适配器获取选定项目的数量?

如何在 RecyclerView 适配器中获取第二个模型的位置

从片段中获取意图值后,我如何在 recyclerview 项目中实现单击

如何更改 bg recyclerview 特定位置 [Kotlin]

单击按钮后,Recyclerview适配器中的项目会跳转到右侧

将 RecyclerView 与 FirestoreRecyclerAdapter 一起使用时如何设置滚动位置?