How to get data from a particular CardView attached RecyclerView - android

I have used a CardView to display some data (data displayed by setting values to TextView ).This CarView is attached to RecyclerView. Data displayed is coming from Database and hence the CardView also increases in list depending on no of rows in Database. What I want is to get data from particular Card on onclick and I am not sure how to do that. and I also wonder that I have used same card to populate different data and the TextView also have the same Id on different Card. How do I achieve this.I want to Take Data onClick because i will use that to edit the previous Data by modifying it And i Just need the way to get Data onClick of CardView rest I can do.
My code is as:-
MedicationCard:
<?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>
My fragment which is using RecyclerView to populate data on Card view.
PrescriptionFragment
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();
}
}
Person 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;
}
}
And finally my Database which from which i am populating Data.
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;
}
And my O/P is as:

This CardView is attached toRecyclerView. To fetch data from any CardView, you just have to get position of item, which is fetched from RecyclerView position.
Yo can follow this answer:
RecyclerView ItemClickListener

I'm a newbie to Android development but, found something interesting...
My screen looks as below...
So, I've a RecyclerView and a CardView combination, and each item would have a Toolbar attached with it...
So, if I implement the Toolbar.OnMenuItemClickListener, along with the definition of your ViewHolder class, it should resolve the issue.
Note: Even if you don't want to use the Toolbar, then implement View.OnClickListener along with the ViewHolder class and implement the OnClick event and I guess, it would do the trick.
Here is the rest of my ViewHolder class definition...
Hope It Helps!!!

Related

TableRow in TableLayout and TableRow from RecyclerView aren’t align

I believe TableRow from activity_main and TableRow from recycler_item_header_row have the same values of fields. But they are not aligned! They are shown like this:
enter image description here
enter image description here
Why the positions of textViews in rows from the activity_main and from the RecyclerView aren’t aligned?
activity_main structure:
<TableLayout>
<TableRow ><\TableRow>
<RecyclerView> <\RecyclerView>
<\Table Layout>
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<TextView
android:layout_column="0"
android:layout_gravity="center"
android:layout_weight="0.3"
android:width="0dp"
android:gravity="center"
android:text="ID"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:layout_column="1"
android:layout_gravity="center"
android:layout_weight="0.4"
android:width="0dp"
android:gravity="center"
android:text="Name"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:layout_column="2"
android:layout_gravity="center"
android:layout_weight="0.3"
android:width="0dp"
android:gravity="center"
android:text="Payment"
android:textSize="16dp"
android:textStyle="bold" />
</TableRow>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.recyclerview.widget.RecyclerView>
</TableLayout>
Two types of item for Recycler View:
recycler_item_header_row:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<TextView
android:layout_column="0"
android:layout_gravity="center"
android:layout_weight="0.3"
android:width="0dp"
android:gravity="center"
android:text="ID"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:layout_column="1"
android:layout_gravity="center"
android:layout_weight="0.4"
android:width="0dp"
android:gravity="center"
android:text="Name"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:layout_column="2"
android:layout_gravity="center"
android:layout_weight="0.3"
android:width="0dp"
android:gravity="center"
android:text="Payment"
android:textSize="16dp"
android:textStyle="bold" />
</TableRow>
recycler_item_regular_row:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
>
<TextView
android:id="#+id/recycler_item_regular_cell_ID"
android:layout_column="0"
android:layout_weight="0.3"
android:width="0dp"
android:gravity="center"
android:text="ID"
android:textSize="16dp" />
<TextView
android:id="#+id/cell_name"
android:layout_column="1"
android:layout_weight="0.4"
android:width="0dp"
android:gravity="center"
android:text="Name"
android:textSize="16dp" />
<TextView
android:id="#+id/cell_payment"
android:layout_column="2"
android:layout_weight="0.3"
android:width="0dp"
android:gravity="center"
android:text="Payment"
android:textSize="16dp" />
</TableRow>
MainActivity
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ArrayList<PaymentModel> paymentData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
paymentData = new ArrayList<>();
add10TestItems(paymentData);
recyclerView = findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(this, paymentData);
recyclerView.setAdapter(adapter);
}
private void add10TestItems(ArrayList<PaymentModel> paymentData) {
for (int i = 0; i < 10; i++) {
paymentData.add(new PaymentModel("A" + i, "Name",
String.valueOf(5 * i)));
}
paymentData.add(new PaymentModel("IDDDDDDDDDDDDDDDDDD", "dddddddddddddddd", "dsfdfdf"));
paymentData.add(new PaymentModel("ID", "Name", "Payment"));
}
}
Adapter:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static final int HEADER_ROW_TYPE = 0;
public static final int REGULAR_ROW_TYPE = 1;
private Context context;
private List<PaymentModel> paymentModelList;
public RecyclerViewAdapter(Context context, List<PaymentModel> paymentModelList) {
this.context = context;
this.paymentModelList = paymentModelList;
}
#Override
public int getItemViewType(int position) {
if (0 == position) {
return HEADER_ROW_TYPE;
} else {
return REGULAR_ROW_TYPE;
}
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view;
RecyclerView.ViewHolder viewHolder;
if (viewType == HEADER_ROW_TYPE) {
view = LayoutInflater.from(context).inflate(R.layout.recycler_item_header_row,
parent, false);
viewHolder = new ViewHolderHeaderRow(view);
} else {
view = LayoutInflater.from(context).inflate(R.layout.recycler_item_regular_row,
parent, false);
viewHolder = new ViewHolderRegularRow(view);
}
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
if (position == 0) {
ViewHolderHeaderRow headerAdapter = (ViewHolderHeaderRow) holder;
} else {
ViewHolderRegularRow regularAdapter = (ViewHolderRegularRow) holder;
regularAdapter.setData(paymentModelList.get(position - 1));
}
}
#Override
public int getItemCount() {
return (paymentModelList.size() + 1);
}
public class ViewHolderRegularRow extends RecyclerView.ViewHolder {
private TextView cellID;
private TextView cellName;
private TextView cellPayment;
public ViewHolderRegularRow(#NonNull View itemView) {
super(itemView);
cellID = itemView.findViewById(R.id.recycler_item_regular_cell_ID);
cellName = itemView.findViewById(R.id.cell_name);
cellPayment = itemView.findViewById(R.id.cell_payment);
}
public void setData(PaymentModel paymentModel) {
cellID.setText(paymentModel.getId());
cellName.setText(paymentModel.getName());
cellPayment.setText(paymentModel.getPayment());
}
}
public class ViewHolderHeaderRow extends RecyclerView.ViewHolder {
public ViewHolderHeaderRow(#NonNull View itemView) {
super(itemView);
}
}
}
Model:
public class PaymentModel {
private String id;
private String name;
private String payment;
public PaymentModel(String id, String name, String payment) {
this.id = id;
this.name = name;
this.payment = payment;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getPayment() {
return payment;
}
}
I would not use TableLayout and TableRow as TableLayout extra logic to resize any child that is a TableRow BUT as you only have one TableRow as a child of the TableLayout that logic is redundant and is less efficient.
The TableRow's that are inside the RecyclerView are the children of the RecyclerView not children of the TableLayout and as the Docs says
If a TableRow's parent is not a TableLayout, the TableRow will behave as an horizontal LinearLayout
And thus won't do any of their normal resizing of the table columns
So in practice you have actually got a structure like:-
<TableLayout>
<TableRow ><\TableRow>
<\TableLayout>
<RecyclerView>
<\LinearLayout>
<\LinearLayout>
.....
<\RecyclerView>
So as the layout weights are based on content and how much each is allowed to grow if need to, the long content RecyclerView rows grow the size of the long content cells differently to the items in the unrelated TableLayout Rows which don't need to grow.
I've designed a similar type layout but used a ConstraintLayout with a layout_constraintWidth_percent value to achieve wider Name column you are trying to achieve with the weights. This works as this is based on the percentage of the fixed width of the parent NOT the variable width of the content and thus the items in the RecyclerView behave the same are the Header line as they both have the same parent size.
Below are the new layout files need
activity_main
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/HeaderID"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="ID"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="#+id/HeaderName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.3"/>
<TextView
android:id="#+id/HeaderName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Name"
android:textSize="16dp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="#+id/HeaderID"
app:layout_constraintEnd_toStartOf="#+id/HeaderPayment"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="#+id/HeaderID"
app:layout_constraintWidth_percent="0.4"/>
<TextView
android:id="#+id/HeaderPayment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Payment"
android:textSize="16dp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="#+id/HeaderName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="#+id/HeaderID"
app:layout_constraintWidth_percent="0.3"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/HeaderID"/>
</androidx.constraintlayout.widget.ConstraintLayout>
recycler_item_header_row
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/HeaderRowID"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="ID"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="#+id/HeaderRowName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.3"/>
<TextView
android:id="#+id/HeaderRowName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Name"
android:textSize="16dp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="#+id/HeaderRowID"
app:layout_constraintEnd_toStartOf="#+id/HeaderRowPayment"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="#+id/HeaderRowID"
app:layout_constraintWidth_percent="0.4"/>
<TextView
android:id="#+id/HeaderRowPayment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Payment"
android:textSize="16dp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="#+id/HeaderRowName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="#+id/HeaderRowID"
app:layout_constraintWidth_percent="0.3"/>
</androidx.constraintlayout.widget.ConstraintLayout>
recycler_item_regular_row
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/recycler_item_regular_cell_ID"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="16dp"
app:layout_constraintEnd_toStartOf="#+id/cell_name"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.3"
tools:text="ID" />
<TextView
android:id="#+id/cell_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="16dp"
app:layout_constraintStart_toEndOf="#+id/recycler_item_regular_cell_ID"
app:layout_constraintEnd_toStartOf="#+id/cell_payment"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="#+id/recycler_item_regular_cell_ID"
app:layout_constraintWidth_percent="0.4"
tools:text="Name" />
<TextView
android:id="#+id/cell_payment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="16dp"
app:layout_constraintBottom_toBottomOf="#+id/recycler_item_regular_cell_ID"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/cell_name"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.3"
tools:text="Payment" />
</androidx.constraintlayout.widget.ConstraintLayout>
This produces the following result with all the columns lined up.

Handle Button Clicks in CursorAdapter

I want to achieve drop down and sum up for a View in my ListView Data is being fetched from the database and am using a cursor adapter, I have tried to add onClickListner for the button in onItemClickListner But no Success.
The problem I am facing is When button DROPDOWN is clicked it will make relative layout Visible but when clicked again it won't relative layout visibility to GONE
Heres my Code for CursorAdapter
public class ProductViewCursorAdapter extends CursorAdapter {
private CardView cv_singleItem;
private RelativeLayout infoLayout;
private Button dropDown;
public ProductViewCursorAdapter(Context context, Cursor c) {
super(context, c, 0);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.custom_product_view_listview,parent,false);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
cv_singleItem = view.findViewById(R.id.cv_custom_single_product);
infoLayout = view.findViewById(R.id.layout_dropdown_relative);
dropDown = view.findViewById(R.id.expand_user_info_listView);
dropDown.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (infoLayout.getVisibility() == View.GONE){
TransitionManager.beginDelayedTransition(cv_singleItem,new AutoTransition());
infoLayout.setVisibility(View.VISIBLE);
dropDown.setBackgroundResource(R.drawable.ic_sumup);
}else{
Log.i("THIS IS TEST","DEMO VIEW "+ infoLayout.getVisibility());
TransitionManager.beginDelayedTransition(cv_singleItem,new AutoTransition());
infoLayout.setVisibility(View.GONE);
dropDown.setBackgroundResource(R.drawable.ic_dropdown);
}
}
});
}
}
ListView single View XML
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:id="#+id/cv_custom_single_product"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/TextAppearance.AppCompat.Title">
<LinearLayout
android:background="#D5000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/image_layout_main"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/iv_product_image_listView"
android:layout_width="match_parent"
android:scaleType="fitCenter"
android:src="#drawable/selimage"
android:layout_height="200dp"/>
<TextView
android:id="#+id/tv_no_image_available"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No Image Available"
android:textAlignment="center"
android:paddingTop="5dp"
android:textSize="15sp"
android:textStyle="bold|italic"
android:textColor="#color/white"/>
<TextView
android:id="#+id/tv_product_name_listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kurti"
android:textSize="18sp"
android:textColor="#FFFFFF"
android:fontFamily="sans-serif-black"
android:textStyle="italic"
android:layout_marginLeft="20dp"
android:layout_below="#id/iv_product_image_listView"
android:layout_marginTop="-25dp"/>
<Button
android:id="#+id/expand_user_info_listView"
android:layout_alignParentEnd="true"
android:layout_below="#id/iv_product_image_listView"
android:focusable="false"
android:layout_marginTop="-49dp"
android:layout_marginEnd="8dp"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/ic_dropdown"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/layout_dropdown_relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<TextView
android:id="#+id/tv_date_dropdown_listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2020-12-14"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#E2FFFFFF"
android:fontFamily="sans-serif-condensed"
android:textStyle="bold"
android:layout_marginStart="4dp"/>
<TextView
android:id="#+id/tv_profit_dropdown_listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profit 500"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#E2FFFFFF"
android:fontFamily="sans-serif-condensed"
android:textStyle="bold"
android:layout_marginStart="4dp"
android:layout_alignParentEnd="true"/>
<TextView
android:id="#+id/tv_sellingPrice_dropdown_listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Selling Price: 800"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#E2FFFFFF"
android:fontFamily="sans-serif-condensed"
android:textStyle="bold"
android:layout_marginStart="4dp"
android:layout_below="#id/tv_date_dropdown_listView"
android:paddingTop="3dp"/>
<TextView
android:id="#+id/tv_actualPrice_dropDown_listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Actual Price: 850"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#E2FFFFFF"
android:fontFamily="sans-serif-condensed"
android:textStyle="bold"
android:layout_marginStart="4dp"
android:layout_alignParentRight="true"
android:layout_below="#id/tv_profit_dropdown_listView"
android:paddingTop="3dp"/>
<TextView
android:id="#+id/tv_pending_dropdown_listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pending: 0"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#F50057"
android:fontFamily="sans-serif-condensed"
android:textStyle="bold"
android:layout_marginStart="4dp"
android:layout_below="#id/tv_sellingPrice_dropdown_listView"
android:paddingTop="3dp"/>
<Button
android:id="#+id/btn_update_single_item"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_below="#id/tv_actualPrice_dropDown_listView"
android:layout_alignParentEnd="true"
android:background="#drawable/ic_update"/>
</RelativeLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
You are using the same infoLayout for every list view row. Every time bindView() gets called you replace the infoLayout with new layout. This of course causes the problem that when you use infoLayout.setVisibility(View.VISIBLE); the infoLayout it's not the same layout it was when the list view row was created because you have replaced it.
So replace,
infoLayout = view.findViewById(R.id.layout_dropdown_relative);
with
final RelativeLayout infoLayout = view.findViewById(R.id.layout_dropdown_relative);

Problem with horizontal recyclerview in android

I'm filling in my Recycler View with the data of all the records from my SQLite DB (one record under another). I need to scroll the Recycler View horizontal, because some words are larger and don't enter into the screen, like you can see in the image below:
This is my RecyclerView Layout, but still not working:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/etIngresar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Ingrese un verbo"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.08"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.023" />
<Button
android:id="#+id/bnMostrar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="mostrar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.822"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.019" />
</android.support.constraint.ConstraintLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/rvListItems"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginTop="70dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="70dp"
android:scrollbarSize="4dp"
android:scrollbars="horizontal"
android:orientation="horizontal" />
</RelativeLayout>
I can't set this in my Activity because it converts the scrolling horizontal BUT it put all the records in one line, and I don't want it.
LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,true);
Like this:
EDITED
My Recycler View class:
public class RecyclerViewAdapter extends RecyclerView.Adapter {
private ArrayList<Items> listItem ;
public RecyclerViewAdapter(ArrayList<Items> listItem) {
this.listItem = listItem;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View contentView = LayoutInflater.from(parent.getContext()).inflate(R.layout.lista, null);
System.out.println("CREATE VIEW HOLDER: " + viewType);
return new Holder(contentView);
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Items item = listItem.get(position);
Holder Holder = (Holder) holder;
Holder.tvVerbo.setText(item.getVerbo());
Holder.tvReferencia.setText(item.getReferencia());
Holder.tvEu.setText(item.getEu());
Holder.tvVoce.setText(item.getVoce());
Holder.tvNos.setText(item.getNos());
Holder.tvVoces.setText(item.getVoces());
System.out.println("BIND VIEW HOLDER: " + position);
}
#Override
public int getItemCount() {
return listItem.size();
}
public class Holder extends RecyclerView.ViewHolder{
TextView tvVerbo;
TextView tvReferencia;
TextView tvEu;
TextView tvVoce;
TextView tvNos;
TextView tvVoces;
public Holder(View itemView) {
super(itemView);
tvVerbo = itemView.findViewById(R.id.tvLista1);
tvReferencia = itemView.findViewById(R.id.tvLista2);
tvEu = itemView.findViewById(R.id.tvLista3);
tvVoce = itemView.findViewById(R.id.tvLista4);
tvNos = itemView.findViewById(R.id.tvLista5);
tvVoces = itemView.findViewById(R.id.tvLista6);
}
}
}
My activity Verbos2 when I call the Recycler:
public class Verbos2 extends AppCompatActivity {
RecyclerView recyclerViewItem;
RecyclerViewAdapter recyclerViewVerbos;
EditText etVerbos;
Button mostrar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.verbos2);
etVerbos = findViewById(R.id.etIngresar);
mostrar = findViewById(R.id.bnMostrar);
recyclerViewItem = findViewById(R.id.rvListItems);
LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false);
recyclerViewItem.setLayoutManager(manager);
mostrar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
List<Items> completeList = new ArrayList<>();
completeList.addAll(mostrarVerbos());
recyclerViewVerbos = new RecyclerViewAdapter((ArrayList<Items>) completeList);
recyclerViewItem.setAdapter(recyclerViewVerbos);
}
});
}
public List<Items> mostrarVerbos(){
BaseDeDatos admin = new BaseDeDatos(getApplicationContext(), "verbos.db", getApplicationContext(), 11);
SQLiteDatabase db = admin.getReadableDatabase();
String[] parametros = {etVerbos.getText().toString()};
Cursor cursor = db.rawQuery("SELECT * FROM verbos WHERE verbos =?", parametros);
List<Items> verbos= new ArrayList<>();
if(cursor.moveToFirst()){
do {
verbos.add(new Items(cursor.getString(1), " " + cursor.getString(2), " " + cursor.getString(3), " " + cursor.getString(4), " " + cursor.getString(5), " " + cursor.getString(6)));
}while (cursor.moveToNext());
}else{
Toast.makeText(getApplicationContext(), "El verbo no existe", Toast.LENGTH_LONG).show();
}
return verbos;
}
}
The list xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:andoid="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tvLista1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="algo1"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvLista2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="algo 2"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvLista3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="algo 3"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvLista4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="algo 4"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvLista5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="algo 5"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvLista6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="algo 5"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
EDITED 2
Ass you can see, it generated an horizontally scroll to each record. I need just one horizontally scroll that envolves all the records at the same time (3 records in this case)
Try to add horizontal scroll view inside of your item layout and set maxLines 1 into your item textView.
Try below code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:overScrollMode="never">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tvLista1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="algo1"
android:maxLines="1"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvLista2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="algo 2"
android:maxLines="1"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvLista3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="algo 3"
android:maxLines="1"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvLista4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="algo 4"
android:maxLines="1"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvLista5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="algo 5"
android:maxLines="1"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvLista6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="algo 5"
android:maxLines="1"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
I hope this can help You!
Thank You.
i don't The problem is originating from your recyclerview. You have to set multiline to true on the TextView inside the recyclerview's layout item so that the text can span multiple lines.

How to change layout height automatically with word wrap?

As you see, height sets normally when there's only one text string, but all crashes, when it's wrapped:
Here's my XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:focusable="true"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingStart="15dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingEnd="15dp"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/title"
android:textSize="16sp"
android:textStyle="bold"
android:layout_marginTop="10dp"
/>
<TextView
android:id="#+id/descr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
</LinearLayout>
<CheckBox
android:id="#+id/checkbox"
android:theme="#style/checkBoxStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_marginTop="7dp"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
/>
</LinearLayout>
Here's my adapter:
class AppsAdapter extends RecyclerView.Adapter<AppsAdapter.ViewHolder> {
private Activity activity;
private ArrayList<Item> items;
private int inflater;
private OnItemCheckListener onItemClick;
AppsAdapter (Activity activity, ArrayList<Item> items, int inflater, OnItemCheckListener onItemCheckListener) {
this.activity = activity;
this.items = items;
this.inflater = inflater;
this.onItemClick = onItemCheckListener;
}
static class ViewHolder extends RecyclerView.ViewHolder {
private ImageView icon;
private TextView title, descr, version;
private CheckBox checkbox;
private ViewHolder (View view) {
super (view);
icon = (ImageView) view.findViewById (R.id.icon);
title = (TextView) view.findViewById (R.id.title);
descr = (TextView) view.findViewById (R.id.descr);
version = (TextView) view.findViewById (R.id.version);
checkbox = (CheckBox) view.findViewById (R.id.checkbox);
}
}
#Override
public ViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {
return new ViewHolder (LayoutInflater.from (parent.getContext ()).inflate (inflater, parent, false));
}
#Override
public void onBindViewHolder (final ViewHolder holder, int position) {
Item item = items.get (position);
if (holder.icon != null)
holder.icon.setImageDrawable (item.icon);
holder.title.setText (item.getItem (0));
holder.descr.setText (item.getItem (1));
// Other actions...
}
}
So, is it really to do it? I don't want to use a standart Android Settings activity, because I want to support KitKat (4.4), but its default themes are awful(
Or perhaps it'll be a solution with TableLayout and so on?
Thanks in advance!
You using your parent layout height was using like this android:layout_height="wrap_content"
User your height android:layout_width="match_parent"
definitely you got o/p
Change the xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:focusable="true"
android:gravity="center"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingTop="5dp">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/colorPrimary"
android:textSize="16sp"
android:text="abababababababababbabababababababab"
android:textStyle="bold" />
<TextView
android:id="#+id/descr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="abababababababababbababababababababsdhsjdhsadsahdkjsahdsadsadhsadhsauidhsauidhsauihsauihsaduisahduisahduisahduisahdusaidhsauidhsauidhsauiidhsauidhsauidhsauidhsauidhsaudhsauidhsadiusahduisahdusahu"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:layout_marginTop="7dp"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:theme="#style/checkBoxStyle" />
</LinearLayout>

How to get id of a spinner in main activity from a recycler view?

I have a recyclerview, I have designed a row which is inflated in the adapter,I want to get hold of the id of the spinner to perform some action with it which I am unable to do.
This is my ApolloActivity.java where I am trying to inflate a recylcer view, in onClick() of the fab i want the id of the spinner.
private List<ApolloPharmacyEntity> entity = null;
private RecyclerView rv;
private FloatingActionButton fab;
Spinner spinner;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setTitle("Apollo Pharmacy");
setContentView(R.layout.activity_apollo);
rv=(RecyclerView)findViewById(R.id.recyclerview2);
Log.i("TAG","recycler view"+rv);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Context context=view.getContext();
LayoutInflater inflater = null;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view= inflater.inflate(R.layout.apollopharmacy_listrow,null);
spinner = (Spinner)view.findViewById(R.id.spinner);//this is becoming null
String Text = spinner.getSelectedItem().toString();
Log.i("TAG","spinnervalue"+spinner);
Intent intent = new Intent(Apollo.this, Cart.class);
startActivity(intent);
}
});
initializeData();
initializeAdapter();
`
This is my adapter
public static class EntityViewHolder extends RecyclerView.ViewHolder {
TextView tabName;
TextView gobacktomenutv;
Spinner spinner;
String[] itemCount = { "1", "2", "3", "4","5","6","7","8","10"};
RelativeLayout addrl;
Dialog dialog;
EntityViewHolder( View itemView) {
super(itemView);
spinner = (Spinner)itemView.findViewById(R.id.spinner);
tabName = (TextView)itemView.findViewById(R.id.textView4);
ArrayAdapter aa = new ArrayAdapter(itemView.getContext(), android.R.layout.simple_spinner_dropdown_item, itemCount);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(aa);
}
}
List<ApolloPharmacyEntity> details;
ApolloPharmacyAdapter(List<ApolloPharmacyEntity> details){
this.details = details;
}
#Override
public ApolloPharmacyAdapter.EntityViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.apollopharmacy_listrow, viewGroup, false);
ApolloPharmacyAdapter.EntityViewHolder pvh = new ApolloPharmacyAdapter.EntityViewHolder(v);
return pvh;
}
#Override
public void onBindViewHolder(ApolloPharmacyAdapter.EntityViewHolder holder, int position) {
holder.tabName.setText(details.get(position).getTabName());
}
#Override
public int getItemCount() {
return details.size();
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
apollopharmacy_listrow.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/relativelayout3"
android:paddingLeft="20dp"
android:layout_below="#id/relativelayout2">
<TextView
android:text="ASPIRIN-250 mg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28sp"
android:textColor="#224e6d"
android:layout_marginTop="20dp"
android:textStyle="bold"
android:id="#+id/textView4" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/relativelayout3"
android:paddingTop="10dp"
android:paddingLeft="30dp"
>
<TextView
android:text="6 tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#224e6d"
android:textSize="26sp"
android:textStyle="bold"
android:id="#+id/textView6" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<View
android:layout_width="90dp"
android:layout_height="2dp"
android:background="#c0c0c0"
android:layout_marginTop="40dp"/>
</RelativeLayout>
<TextView
android:text="1 STRIP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#224e6d"
android:layout_marginTop="50dp"
android:textStyle="bold"
android:textSize="26sp"
android:id="#+id/textView7" />
<RelativeLayout
android:layout_width="100dp"
android:layout_height="45dp"
android:background="#drawable/add_rounded"
android:id="#+id/relativeLayout"
android:layout_marginBottom="15dp"
android:layout_alignBottom="#+id/textView7"
android:layout_alignParentEnd="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Transfer"
android:text="ADD"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/relativelayout3"
>
<TextView
android:text="₹"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:textSize="34sp"
android:textColor="#82BCB4"
android:layout_marginStart="200dp"
android:id="#+id/textView8" />
<TextView
android:text="26"
android:layout_width="wrap_content"
android:layout_height="80sp"
android:layout_marginTop="25dp"
android:textSize="50sp"
android:textColor="#224e6d"
android:textStyle="bold|normal"
android:layout_marginStart="230dp"
android:id="#+id/textView9" />
<Spinner
android:layout_width="55dp"
android:layout_height="55dp"
android:background="#drawable/spinner_outline"
android:id="#+id/spinner"
android:layout_marginEnd="30dp"
android:layout_alignBottom="#+id/textView8"
android:layout_alignEnd="#+id/textView8"
android:layout_marginBottom="9dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#drawable/ic_star_border_black_24px"
android:layout_marginTop="15dp"
android:layout_marginEnd="12dp"
android:id="#+id/imageView2"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</RelativeLayout>
In your ApolloActivity.java's onCreate() method you have not set your ApolloPharmacyAdapter to your RecyclerView. Hence the layout of apollopharmacy_listrow.xml is not inflated.
Please use rv.setAdapter() method in onCreate()
Also if there is no detail found for listrow RecyclerView will not inflate the listrow. Spinner cannot be used like this in onCreate() as there maybe more than one instances of Spinner depending on number of rows in RecyclerView.
You will have to specify which spinner do you want to refer to from you Activity.
I can help if you elaborate on what exactly do you want to achieve by sending a spinner item to next Activity.

Categories

Resources