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

时间:2018-06-17 11:46:41

标签: android android-recyclerview

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

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

RecyclerviewAdapter.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
    }

}

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

1 个答案:

答案 0 :(得分:0)

你在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
}

我没有正确理解你吗?