如何从特定的CardView连接RecyclerView获取数据

时间:2016-05-11 10:02:15

标签: android android-recyclerview android-cardview

我使用了CardView来显示一些数据(通过将值设置为TextView来显示数据)。这个CarView附加到RecyclerView。显示的数据来自数据库,因此CardView也会在列表中增加,具体取决于数据库中的行数。我想要的是从onclick上的特定卡获取数据,我不知道该怎么做。我也想知道我使用相同的卡来填充不同的数据,TextView在不同的卡上也有相同的ID。我如何实现这一目标。我想获取数据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> 我能做到。
我的代码如下: -
MedicationCard:

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

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

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

RecyclerViewAdapter:

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

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

Type error: Argument 1 passed to App\Http\Controllers\Auth\AuthController::authenticated() must be an instance of App\Http\Controllers\Auth\Request, instance of Illuminate\Http\Request given, called in /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php on line 115

我的O / P如下: enter image description here

2 个答案:

答案 0 :(得分:0)

RecyclerView已附加到CardView。要从任何public void MoveGeom() { var x = _r.Next(-60, 200); var y = _r.Next(-60, 200); _geom.Transform = new TranslateTransform(x, y); } 获取数据,您只需获取item的位置,该位置从RecyclerView位置获取。 哟可以按照这个答案: RecyclerView ItemClickListener

答案 1 :(得分:0)

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

My screen looks as below...

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

因此,如果我实现Toolbar.OnMenuItemClickListener以及ViewHolder类的定义,它应该可以解决问题。

注意:即使您不想使用工具栏,也要实现View.OnClickListener以及ViewHolder类并实现OnClick事件,我想,它可以解决问题。

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

希望它能帮助!!!