如何从附加的 RecyclerView 的特定 CardView 获取数据

How to get data from a particular CardView attached RecyclerView

我用CardView显示了一些数据(通过设置值TextView来显示的数据)。这个CarView附加到RecyclerView。显示的数据来自数据库,因此 CardView 也会根据数据库中的行数在列表中增加。我想要的是从 onclick 上的特定卡获取数据,但我不确定该怎么做。而且我也想知道我使用同一张卡来填充不同的数据并且 TextView 在不同的卡上也有相同的 ID。我如何实现 this.I 想要获取数据 onClick 因为我将使用它来通过修改它来编辑以前的数据而且我只需要获取数据 onClick 的方法 CardView 休息我能做的。
我的代码是:-
医药卡:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent" android:layout_height="match_parent"

    android:padding="5dp">

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:elevation="6sp"
        android:background="@color/colorPrimary"
        card_view:cardUseCompatPadding="true"
        android:id="@+id/cardMedview">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/bg"
            android:padding="4dp">
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/NameSection">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="Name:-  "
                    android:id="@+id/textView6"
                    android:textColor="#0866C4"
                    android:textStyle="bold"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentStart="true" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="Name of Med"
                    android:textStyle="bold"
                    android:id="@+id/nameOfUpmingMed"
                    android:layout_alignParentTop="true"
                    android:layout_toEndOf="@+id/textView6" />
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/QuntitySection"
                android:layout_below="@+id/NameSection"
                android:layout_alignParentEnd="true"
                android:layout_alignParentStart="true">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textStyle="bold"
                    android:text="Quantity:-  "
                    android:textColor="#0866C4"
                    android:id="@+id/textView5"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentStart="true" />
                <TextView
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="crocin"
                    android:layout_marginLeft="3dp"
                    android:id="@+id/QuantOfMed"
                    android:textColor="#000"
                    android:layout_alignParentTop="true"
                    android:layout_toEndOf="@+id/textView5" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textStyle="bold"
                    android:text="Reffil Amount:-  "
                    android:id="@+id/textView7"
                    android:textColor="#0866C4"
                    android:layout_marginStart="18dp"
                    android:layout_alignParentTop="true"
                    android:layout_toEndOf="@+id/QuantOfMed" />
                <TextView
                    android:layout_width="80dp"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="222"
                    android:textColor="#000"
                    android:id="@+id/ReffilAmt"
                    android:layout_alignParentTop="true"
                    android:layout_toEndOf="@+id/textView7" />
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/expirationSection"
                android:layout_below="@+id/QuntitySection">
                <TextView
                    android:layout_width="wrap_content"
                    android:textStyle="bold"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="Expiration Date:-  "
                    android:textColor="#0866C4"
                    android:id="@+id/textView8"/>
                <TextView
                    android:layout_width="150dp"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="XX/YY/ZZZZ"
                    android:textColor="#000"
                    android:id="@+id/ExpDate"
                    android:layout_marginLeft="3dp"
                    android:layout_alignParentTop="true"
                    android:layout_toEndOf="@+id/textView8" />
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/DaysSection"
                android:layout_below="@+id/expirationSection">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="2dp"
                    android:textStyle="bold"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="Days:-  "
                    android:textColor="#0866C4"
                    android:id="@+id/textView11"  />

                <TextView
                    android:layout_width="250dp"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="MON,TUE,WED,THU,FRI,SAT,SUN"
                    android:id="@+id/NoOfDays"
                    android:textColor="#000"
                    android:layout_marginLeft="3dp"
                    android:layout_alignTop="@+id/textView11"
                    android:layout_toEndOf="@+id/textView11" />
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/TimeSection"
                android:layout_below="@+id/DaysSection">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Time:-  "
                android:textColor="#0866C4"
                android:id="@+id/textView9"
               />

            <TextView
                android:layout_width="70dp"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="08:00 AM"
                android:textColor="#000"
                android:layout_marginLeft="3dp"
                android:id="@+id/timeMorning"
                android:layout_alignParentTop="true"
                android:layout_toEndOf="@+id/textView9" />

            <TextView
                android:layout_width="70dp"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="01:00 PM"
                android:textColor="#000"
                android:layout_marginLeft="4dp"
                android:id="@+id/timeAfternoon"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true" />

            <TextView
                android:layout_width="70dp"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="08:00 PM"
                android:textColor="#000"
                android:id="@+id/timeEvening"
                android:layout_alignParentTop="true"
                android:layout_alignParentEnd="true"
                android:layout_marginEnd="49dp" />
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/InstructionSection"
                android:layout_below="@+id/TimeSection">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="Instructions:-  "
                    android:textColor="#0866C4"
                    android:textStyle="bold"
                    android:layout_marginTop="2dp"
                    android:id="@+id/textView10"
                     />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_marginTop="2dp"
                    android:textColor="#000"
                    android:text="Take Medicine before bed and after the food.and he shuld take agood sound sleep and shuld wake up early in morning"
                    android:id="@+id/SpecialInst"
                    android:layout_toEndOf="@+id/textView10" />

            </RelativeLayout>



        </RelativeLayout>

    </android.support.v7.widget.CardView>



</RelativeLayout>

我的片段使用 RecyclerView 在 Card 视图上填充数据。
处方片段

    public class PrescriptionFragment extends Fragment {

private Context context;
    private  RecyclerView recyclerView;
    DatabaseAdaptor databaseAdaptor;
    public PrescriptionFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_prescription, container, false);
        recyclerView = (RecyclerView)view.findViewById(R.id.recyclerView);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        recyclerView.setLayoutManager(linearLayoutManager);
        context=getActivity();
        databaseAdaptor = new DatabaseAdaptor(context);
        initializeData();
        initializeAdapter();




        return view;
    }

    private void initializeData(){
       databaseAdaptor.getDetailData();
    }
    private void initializeAdapter(){
        RVAdapter adapter = new RVAdapter(databaseAdaptor.persons);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        initializeData();
        initializeAdapter();
    }
}

RecyclerViewAdapter:

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {


    public static class PersonViewHolder extends RecyclerView.ViewHolder{
        CardView cardView;
        TextView Name,Qunat,refill,expDate,Days,moring,after,evening,inst;

        public PersonViewHolder(View itemView) {
            super(itemView);
            cardView=(CardView)itemView.findViewById(R.id.cardMedview);
            Name=(TextView)itemView.findViewById(R.id.nameOfUpmingMed);
            Qunat=(TextView)itemView.findViewById(R.id.QuantOfMed);
            refill=(TextView)itemView.findViewById(R.id.ReffilAmt);
            expDate=(TextView)itemView.findViewById(R.id.ExpDate);
            Days=(TextView)itemView.findViewById(R.id.NoOfDays);
            moring=(TextView)itemView.findViewById(R.id.timeMorning);
            after=(TextView)itemView.findViewById(R.id.timeAfternoon);
            evening=(TextView)itemView.findViewById(R.id.timeEvening);
            inst=(TextView)itemView.findViewById(R.id.SpecialInst);
        }
    }
    List<Person> persons;
    RVAdapter(List<Person> persons){
        this.persons=persons;
    }
    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public PersonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.medication_card, parent, false);
        PersonViewHolder pvh = new PersonViewHolder(v);
        return pvh;
    }

    @Override
    public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
        personViewHolder.Name.setText(persons.get(i).name);
        personViewHolder.Qunat.setText(persons.get(i).qunt);
        personViewHolder.refill.setText(persons.get(i).refill);
        personViewHolder.expDate.setText(persons.get(i).date);
        personViewHolder.Days.setText(persons.get(i).days);
        personViewHolder.moring.setText(persons.get(i).morning);
        personViewHolder.after.setText(persons.get(i).after);
        personViewHolder.evening.setText(persons.get(i).evening);
        personViewHolder.inst.setText(persons.get(i).inst);


    }

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

人Class

class Person{
    String name,qunt,refill,inst,date,days,morning,after,evening;
    Person(String name,String quant,String refill,String date,String days,String morn,String Aft,String evening,String inst){
        this.name=name;
        this.qunt=quant;
        this.refill=refill;
        this.date=date;
        this.days=days;
        this.morning=morn;
        this.after=Aft;
        this.evening=evening;
        this.inst=inst;
    }
}

最后是我从中填充数据的数据库。

public List<Person> getDetailData(){
      SQLiteDatabase db= helper.getWritableDatabase();
      String[] columns={DatabaseHelper.medicationId,DatabaseHelper.medicName,DatabaseHelper.medicationQuant,DatabaseHelper.expirationDate,
      DatabaseHelper.RiffilAmount,DatabaseHelper.specialInst,DatabaseHelper.alarmDays,DatabaseHelper.timeMorning,
      DatabaseHelper.timeAfternoon,DatabaseHelper.timeEvening};
     Cursor mcursor= db.query(DatabaseHelper.MEDICATION_DETAILS_TABLE, columns, null, null, null, null, null);
      persons=new ArrayList<>();
      StringBuffer buffer=new StringBuffer();
      while (mcursor.moveToNext()){
         int index1= mcursor.getColumnIndex(DatabaseHelper.medicationId);
          int index2=mcursor.getColumnIndex(DatabaseHelper.medicName);
          int index3=mcursor.getColumnIndex(DatabaseHelper.medicationQuant);
          int index4=mcursor.getColumnIndex(DatabaseHelper.expirationDate);
          int index5=mcursor.getColumnIndex(DatabaseHelper.RiffilAmount);
          int index6=mcursor.getColumnIndex(DatabaseHelper.specialInst);
          int index7=mcursor.getColumnIndex(DatabaseHelper.alarmDays);
          int index8=mcursor.getColumnIndex(DatabaseHelper.timeMorning);
          int index9=mcursor.getColumnIndex(DatabaseHelper.timeAfternoon);
          int index10=mcursor.getColumnIndex(DatabaseHelper.timeEvening);
          int medid=mcursor.getInt(index1);
          String name=mcursor.getString(index2);
          String quant=mcursor.getString(index3);
          String expDate=mcursor.getString(index4);
          String refill=mcursor.getString(index5);
          String specialInst=mcursor.getString(index6);
          String alarmdays=mcursor.getString(index7);
          String timemorning=mcursor.getString(index8);
          String timeafter=mcursor.getString(index9);
          String timeevening= mcursor.getString(index10);
          persons.add(new Person(name,quant,refill,expDate,alarmdays,timemorning,timeafter,timeevening,specialInst));

      }
      mcursor.close();
      db.close();
      return persons;
  }

我的 O/P 是:

CardView 已附加到 RecyclerView。要从任何 CardView 中获取数据,您只需获取项目的位置,该位置是从 RecyclerView 位置获取的。 你可以按照这个答案: RecyclerView ItemClickListener

我是 Android 开发的新手,但是发现了一些有趣的东西...

My screen looks as below...

所以,我有一个 RecyclerView 和一个 CardView 组合,每个项目都会有一个工具栏...

因此,如果我实施 Toolbar.OnMenuItemClickListener,连同您的 ViewHolder class 的定义,应该可以解决问题。

注意:即使您不想使用工具栏,然后实现 View.OnClickListener 和 ViewHolder class 并实现 OnClick 事件,我想,它会成功。

Here is the rest of my ViewHolder class definition...

希望对您有所帮助!!!