Hello I just started the Android and I do not understand how to put more TextView in a RecyclerView
I have already seen this solution but I do not understand: How to create RecyclerView with multiple view type?
I tried to make a table with multiple dimensions but it did not work.
Adapter:
public class DeviceRecyclerView extends RecyclerView.Adapter<DeviceRecyclerView.ViewHolder> {
private String[]data;
public DeviceRecyclerView(String[]data){
this.data = data;
}
#Override
public DeviceRecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.device_list_row, parent , false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
String name = data[position];
String rssi = data[position];
String uuid = data[position];
holder.DeviceNameTextView.setText(name);
holder.DeviceUUIDTextViewValue.setText(rssi);
holder.DeviceRSSIVTextViewValue.setText(uuid);
}
#Override
public int getItemCount() {
return data.length;
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView DeviceNameTextView;
TextView DeviceUUIDTextViewValue;
TextView DeviceRSSIVTextViewValue;
public ViewHolder (View itemView){
super(itemView);
DeviceNameTextView = itemView.findViewById(R.id.DeviceNameTextView);
DeviceUUIDTextViewValue = itemView.findViewById(R.id.DeviceUUIDTextViewValue);
DeviceRSSIVTextViewValue = itemView.findViewById(R.id.DeviceRSSIVTextViewValue);
}
}
MainActivity:
RecyclerView deviceList = (RecyclerView)findViewById(R.id.recycler_view_NoPaired);
deviceList.setLayoutManager(new LinearLayoutManager(this));
String[]names = {"Samsung64656"};
String[]rssi = {"ezez"};
String[]uuid = {"08:90:e5:90"};
deviceList.setAdapter(new DeviceRecyclerView(names));
My XML.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="180dp"
android:gravity="center_vertical"
tools:layout_editor_absoluteX="30dp"
tools:layout_editor_absoluteY="81dp">
<View
android:id="#+id/ColoredRect"
android:layout_width="10dp"
android:layout_height="160dp"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:background="#E27F26"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/DeviceNameTextView"
android:layout_width="133dp"
android:layout_height="24dp"
android:layout_marginStart="20dp"
android:layout_marginTop="16dp"
android:text="#string/device_name_label"
android:textColor="#color/TextColor"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/DeviceUUIDTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="8dp"
android:text="#string/deviceUUIDTextView"
android:textColor="#color/TextColor"
android:textSize="12sp"
app:layout_constraintBottom_toTopOf="#+id/DeviceUUIDTextViewValue"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/DeviceNameTextView" />
<TextView
android:id="#+id/DeviceUUIDTextViewValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="14sp"
android:text=""
android:textColor="#color/TextColor"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/DeviceUUIDTv" />
<TextView
android:id="#+id/rssiLabelTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="16dp"
android:text="#string/deviceRSSITextView"
android:textColor="#color/TextColor"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/DeviceUUIDTextViewValue" />
<TextView
android:id="#+id/DeviceRSSIVTextViewValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="14sp"
android:text=""
android:textColor="#color/TextColor"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/rssiLabelTextView" />
</android.support.constraint.ConstraintLayout>
Please explain me clearly how to do it. I am French sorry for spelling errors.
You just need to add some textView in you'r XML row (named device_list_row.xml), and init their in your adapter, like you have already did with 3 textview.
But's more easy with Object than use array of string, for data. for example in you'r case , make Device class object
Device.java
public class Device {
String name;
String rssi;
String uuid;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRssi() {
return rssi;
}
public void setRssi(String rssi) {
this.rssi = rssi;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
}
And adapter code ->
public class DeviceRecyclerView extends RecyclerView.Adapter<DeviceRecyclerView.ViewHolder> {
private ArrayList<Device> deviceList;
public DeviceRecyclerView(Arraylist<Device> deviceList){
this.deviceList= deviceList;
}
#Override
public DeviceRecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.device_list_row, parent , false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Device device = deviceList.get(position);
holder.deviceNameTextView.setText(device.getName());
holder.deviceUUIDTextViewValue.setText(device.getUuid());
holder.deviceRSSIVTextViewValue.setText(device.getRssi());
}
#Override
public int getItemCount() {
return deviceList.length;
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView deviceNameTextView, deviceUUIDTextViewValue, deviceRSSIVTextViewValue;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
deviceNameTextView = itemView.findViewById(R.id.DeviceNameTextView);
deviceUUIDTextViewValue = itemView.findViewById(R.id.DeviceUUIDTextViewValue);
deviceRSSIVTextViewValue = itemView.findViewById(R.id.DeviceRSSIVTextViewValue);
}
#Override
public void onClick(View v) {
Log.i("DEBUG","Item RecyclerView Cliqué");
}
}
With that, you init your Adapter with list of Device Object, who can have any variable has you want without need to pass more array or data to Adapter:-)
And for make a new Device, and use it in RV
Device device = new Device();
device.setName("Device Name 01");
device.setUuid("uuid value");
device.SetRssi("rssi value");
ArrayList<Device> deviceList = new ArrayList<>();
deviceList.add(device);
//Init adapter and fill it
DeviceRecyclerView deviceRecyclerView = new DeviceRecyclerView(deviceList);
LinearLayoutManager llm = new LinearLayoutManager(getContext());
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(llm);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(deviceRecyclerView);
Related
Actually I m new to android development and i m trying to implement my product images like a carousel with a library called "why not" using glide with recyclerview. but I m not able to see the photos like this. I am not getting how to implement a carousel like this, What i m doing wrong please help me . Please guide me someone please
I am trying to make it like this
MY Design xml
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="5dp"
app:cardCornerRadius="8dp"
app:cardElevation="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<org.imaginativeworld.whynotimagecarousel.ImageCarousel
android:id="#+id/carousel"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop">
</org.imaginativeworld.whynotimagecarousel.ImageCarousel>
<TextView
android:id="#+id/roomName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="Room with kitchen"
android:textColor="#232222"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/roomRent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:text="Rent 4500/- month"
android:textColor="#232222"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</androidx.cardview.widget.CardView>
Model Class
public class Rooms {
public Rooms(String carousel, String title, int rent) {
this.carousel = carousel;
this.title = title;
this.rent = rent;
}
private String carousel,title;
private int rent;
public String getCarousel() {
return carousel;
}
public void setCarousel(String carousel) {
this.carousel = carousel;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getRent() {
return rent;
}
public void setRent(int rent) {
this.rent = rent;
}
}
My Adapter class
public class RoomsAdapter extends RecyclerView.Adapter<RoomsAdapter.RoomsViewHolder>{
Context context;
ArrayList<Rooms> rooms;
public RoomsAdapter(Context context, ArrayList<Rooms> rooms) {
this.context = context;
this.rooms = rooms;
}
#NonNull
#Override
public RoomsViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new RoomsViewHolder(LayoutInflater.from(context).inflate(R.layout.room_design_layout, parent, false));
}
#Override
public void onBindViewHolder(#NonNull RoomsViewHolder holder, int position) {
Rooms room = rooms.get(position);
Glide.with(context)
.load(room.getCarousel())
.into(holder.carousel);
holder.roomtitle.setText(room.getTitle());
holder.roomrent.setText(String.valueOf(room.getRent()));
}
#Override
public int getItemCount() {
return rooms.size();
}
public class RoomsViewHolder extends RecyclerView.ViewHolder{
ImageView carousel;
TextView roomtitle, roomrent;
public RoomsViewHolder(#NonNull View itemView) {
super(itemView);
carousel = itemView.findViewById(R.id.carousel);
roomtitle = itemView.findViewById(R.id.roomName);
roomrent = itemView.findViewById(R.id.roomRent);
}
}
}
Here is Sample Code with carousel View by using WHY NOT IMAGE CAROUSEL library. Check this code
I am trying to load a RecyclerView with data, but I get a NullPointerException every time I want to load the RecyclerView. (The RecyclerView is displayed in a Dialog). First, I have this class:
public class FondosClase {
private int recurso;
private String nombre;
public int getRecurso() {
return recurso;
}
public void setRecurso(int recurso) {
this.recurso = recurso;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public FondosClase(){}
public FondosClase(int recurso, String nombre) {
this.recurso = recurso;
this.nombre = nombre;
}
}
Then my adapter:
public class AdaptadorMenuIzq extends RecyclerView.Adapter<AdaptadorMenuIzq.ViewHolder>{
private FondosClase[] fondosClaseArrayList;
private Context context;
public AdaptadorMenuIzq(FondosClase[] fondosClaseArrayList, Context context) {
this.fondosClaseArrayList = fondosClaseArrayList;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutInflater = LayoutInflater.from(parent.getContext()).inflate(R.layout.design_rv_bg_men_izq, parent, false);
return new AdaptadorMenuIzq.ViewHolder(layoutInflater);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
final FondosClase fondosClase = fondosClaseArrayList[position];
holder.textView.setText(fondosClase.getNombre());
holder.imageView.setImageResource(fondosClase.getRecurso());
}
#Override
public int getItemCount() {
return fondosClaseArrayList.length;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public TextView textView;
public ViewHolder(#NonNull View itemView) {
super(itemView);
this.imageView = itemView.findViewById(R.id.imageView);
this.textView = itemView.findViewById(R.id.textView);
}
}
}
Then create a class where I create the method for Dialog:
public class Alertas {
private AdaptadorMenuIzq adaptadorMenuIzq;
public void AlertaMenuIzquierdoFondo(Context context){
LayoutInflater layoutInflater = LayoutInflater.from(context);
Dialog anuncio = new Dialog(context, R.style.FondoDialog);
final View view = layoutInflater.inflate(R.layout.alerta_fondo_tarjeta_cv_rv, null);
final RecyclerView rv = view.findViewById(R.id.rv_tarjetas);
FondosClase[] fondosClases = new FondosClase[]{
new FondosClase(R.drawable.permanent_bg_a, "a"),
new FondosClase(R.drawable.permanent_bg_b, "b")
};
adaptadorMenuIzq = new AdaptadorMenuIzq(fondosClases, context);
rv.setHasFixedSize(true);
rv.setLayoutManager(new LinearLayoutManager(context));
rv.setAdapter(adaptadorMenuIzq);
anuncio.setContentView(view);
anuncio.show();
}
}
And from my MainActivity I run it through a FAB:
binding.appBarMain.fab.setOnClickListener(view ->{
alertas = new Alertas();
alertas.AlertaMenuIzquierdoFondo(MainActivity.this);
}
);
When I press the button to show it I get the following error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.frabasoft.genshinimpactrecursos.Adaptadores.AdaptadorMenuIzq.onBindViewHolder(AdaptadorMenuIzq.java:35)
The line that marks the error is the following:
holder.textView.setText(fondosClase.getNombre());
I changed it to:
holder.textView.setText(fondosClaseArrayList[position].geNombre());
But, the result was the same.
I attach the xml:
alerta_fondo_tarjeta_cv_rv:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_gravity="center"
app:cardCornerRadius="75dp"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="25dp"
android:layout_marginRight="25dp"
android:layout_marginBottom="25dp"
android:layout_marginLeft="25dp"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_tarjetas"
android:layout_marginTop="25dp"
android:layout_marginRight="25dp"
android:layout_marginBottom="25dp"
android:layout_marginLeft="25dp"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
design_rv_bg_menu_izq:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
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"
style="#style/CardView.Light"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:elevation="5dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/ivIcon"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="#mipmap/ic_launcher"
android:layout_margin="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tvIcon"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="#mipmap/ic_launcher"
android:layout_margin="8dp"
android:text="HOLA"
android:gravity="center"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
Does someone tell me what data I'm going wrong?
in the view holder you are not passing your XML view id's
it should be:
this.imageView = itemView.findViewById(R.id.ivIcon);
this.textView = itemView.findViewById(R.id.tvIcon);
I am taking data from "https://jsonplaceholder.typicode.com/todos" with using Retrofit, and I want to create RecyclerView with these data but my app is crashing.
What is the problem exactly? I also debuged app, I am taking all data from web but cannot put them into the recyclerview..
This is my adapter class
public class RecylerViewAdapter extends RecyclerView.Adapter<RecylerViewAdapter.MyViewHolder> {
List<Bilgiler> bilgilerList;
public RecylerViewAdapter(List<Bilgiler> bilgilerList) {
this.bilgilerList = bilgilerList;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list,parent,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
holder.userid.setText(bilgilerList.get(position).getUserId());
holder.id.setText(bilgilerList.get(position).getId());
holder.title.setText(bilgilerList.get(position).getTitle());
holder.checkBox.setChecked(bilgilerList.get(position).getCompleted());
}
#Override
public int getItemCount() {
return bilgilerList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView userid,id,title;
CheckBox checkBox;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
userid = itemView.findViewById(R.id.userid);
id = itemView.findViewById(R.id.id);
title = itemView.findViewById(R.id.titlee);
checkBox= itemView.findViewById(R.id.checkBox);
}
}
}
Finally, this is my Main class
public class MainActivity extends AppCompatActivity {
private List<Bilgiler> bilgilerList;
private RecyclerView recyclerView;
private RecylerViewAdapter recylerViewAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyler);
take();
}
public void take() {
Call<List<Bilgiler>> bilgiList = ManagerAll.getInstance().getirBilgileri();
bilgiList.enqueue(new Callback<List<Bilgiler>>() {
#Override
public void onResponse(Call<List<Bilgiler>> call, Response<List<Bilgiler>> response) {
if (response.isSuccessful()) {
bilgilerList=response.body();
recylerViewAdapter= new RecylerViewAdapter(bilgilerList);
recyclerView.setAdapter(recylerViewAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
Log.i("xxx",response.body().toString());
}
}
#Override
public void onFailure(Call<List<Bilgiler>> call, Throwable t) {
}
});
}
}
This is XML of page.
<?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="300dp"
android:background="#color/purple_200"
>
<TextView
android:id="#+id/userid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="177dp"
android:layout_marginTop="39dp"
android:layout_marginEnd="177dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="178dp"
android:layout_marginTop="31dp"
android:layout_marginEnd="176dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/userid" />
<TextView
android:id="#+id/titlee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="183dp"
android:layout_marginTop="31dp"
android:layout_marginEnd="170dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/id" />
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="155dp"
android:layout_marginTop="21dp"
android:layout_marginEnd="162dp"
android:text="CheckBox"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/titlee" />
</androidx.constraintlayout.widget.ConstraintLayout>
Try calling the notifyDataSetChanged method on the adapter after getting the new data
bilgiList.enqueue(new Callback<List<Bilgiler>>() {
#Override
public void onResponse(Call<List<Bilgiler>> call, Response<List<Bilgiler>> response) {
if (response.isSuccessful()) {
bilgilerList=response.body();
recylerViewAdapter = new RecylerViewAdapter(bilgilerList);
recyclerView.setAdapter(recylerViewAdapter);
recylerViewAdapter.notifyDataSetChanged();
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
Log.i("xxx",response.body().toString());
}
}
#Override
public void onFailure(Call<List<Bilgiler>> call, Throwable t) {
}
});
This question already has answers here:
FirebaseListAdapter not pushing individual items for chat app - Firebase-Ui 3.1
(2 answers)
Closed 2 years ago.
My RecyclerView is not displaying any data.
buynow.java
public class buynow extends AppCompatActivity {
private RecyclerView recyclerView;
DatabaseReference ProductRef;
private EditText searchField;
private Button search_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buynow);
ProductRef = FirebaseDatabase.getInstance().getReference().child("Products");
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
searchField = (EditText) findViewById(R.id.search_field);
search_btn = (Button) findViewById(R.id.search_button);
search_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
firebaseUserSearch();
}
});
}
private void firebaseUserSearch(){
FirebaseRecyclerOptions<Products> options = new FirebaseRecyclerOptions.Builder<Products>()
.setQuery(ProductRef,Products.class).build();
FirebaseRecyclerAdapter<Products, UsersViewHolder> firebaseRecyclerAdapter = new
FirebaseRecyclerAdapter<Products, UsersViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull UsersViewHolder holder, int position, #NonNull
Products model) {
holder.setDetails(model.getPname(), model.getPprice(), model.getPmrp(),
model.getPcondition(), model.getPimage());
}
#NonNull
#Override
public UsersViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return null;
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
//View Holder Class
public class UsersViewHolder extends RecyclerView.ViewHolder{
View mView;
public UsersViewHolder(#NonNull View itemView) {
super(itemView);
mView = itemView;
}
public void setDetails(String phoneName, String phonePrice, String phoneMrp, String
phoneCondition, String phoneImage){
TextView phone_name = (TextView) mView.findViewById(R.id.product_name);
TextView phone_price = (TextView) mView.findViewById(R.id.product_price);
TextView phone_mrp = (TextView) mView.findViewById(R.id.product_mrp);
TextView phone_condition = (TextView) mView.findViewById(R.id.product_condition);
ImageView phone_image = (ImageView)mView.findViewById(R.id.product_image);
phone_name.setText(phoneName);
phone_price.setText(phonePrice);
phone_mrp.setText(phoneMrp);
phone_condition.setText(phoneCondition);
Picasso.with(getApplicationContext()).load(phoneImage).into(phone_image);
}
}
}
Products.java
public class Products {
private String pname,pprice,pimage,pmrp,pcondition;
public Products(){
}
public Products(String pname, String pprice, String pimage, String pmrp, String pcondition) {
this.pname = pname;
this.pprice = pprice;
this.pimage = pimage;
this.pmrp = pmrp;
this.pcondition = pcondition;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public String getPprice() {
return pprice;
}
public void setPprice(String pprice) {
this.pprice = pprice;
}
public String getPimage() {
return pimage;
}
public void setPimage(String pimage) {
this.pimage = pimage;
}
public String getPmrp() {
return pmrp;
}
public void setPmrp(String pmrp) {
this.pmrp = pmrp;
}
public String getPcondition() {
return pcondition;
}
public void setPcondition(String pcondition) {
this.pcondition = pcondition;
}
}
Layout for RecyclerView
<?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:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:background="#color/primanrybg">
<RelativeLayout
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/buyphones_layout_bg">
<ImageView
android:id="#+id/product_image"
android:layout_width="60dp"
android:layout_height="90dp"
android:src="#drawable/rapid_pickup_foreground" />
<TextView
android:id="#+id/product_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/product_image"
android:textSize="16sp"
android:textColor="#color/black"
android:paddingStart="16dp"
android:paddingBottom="4dp"
android:text="iPhone 6s Space Grey (64GB)"/>
<LinearLayout
android:id="#+id/condition"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/product_image"
android:layout_marginLeft="16dp"
android:background="#drawable/search_frame"
android:layout_below="#id/product_name"
android:layout_marginBottom="3dp"
android:padding="2dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/yes_no_bg"
android:textColor="#color/white"
android:paddingStart="4dp"
android:padding="1dp"
android:paddingEnd="4dp"
android:text="Condition:" />
<TextView
android:id="#+id/product_condition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginRight="3dp"
android:padding="1dp"
android:textColor="#color/black"
android:text="Like New" />
</LinearLayout>
<TextView
android:id="#+id/product_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/condition"
android:layout_toRightOf="#+id/product_image"
android:textSize="18sp"
android:textColor="#color/black"
android:paddingStart="16dp"
android:text="9,9999"/>
<TextView
android:id="#+id/product_mrp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/product_price"
android:layout_below="#id/condition"
android:textSize="18sp"
android:text="24000"
android:textColor="#color/grey"
android:paddingStart="10dp"/>
I am not getting data into my RecyclerView. It is just not showing anything no error nothing. Any help is very appreciated.
I am getting my data from firebase and putting them into the recyclerview to display a list of products.
I would be really great if you can point out what is wrong with my code so that i can correct it.
You need to add startListening() to start listening for data in firebaseui:
The FirebaseRecyclerAdapter uses an event listener to monitor changes to the Firebase query. To begin listening for data, call the startListening() method. You may want to call this in your onStart() method. Make sure you have finished any authentication necessary to read the data before calling startListening() or your query will fail.
#Override
protected void onStart() {
super.onStart();
firebaseRecyclerAdapter.startListening();
}
i have list with 30 items . when i set adapter to RecyclerView
or when notifyDataSetChanged method called , the ui of app will
frezzing for a few seconds.
here my code
adapater and set :
public AdapterBarberWorkTimeDetails(List<BarberWorkPeriodItems> items, int itemLayout, Context context, String date, FragmentReserveBarberWorkTime parentFragment) {
this.items = items;
this.itemLayout = itemLayout;
this.mContext = context;
this.date = date;
this.parentFragment = parentFragment;
}
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(itemLayout, parent, false);
return new ViewHolder(v);
}
public void onBindViewHolder(#NonNull final AdapterBarberWorkTimeDetails.ViewHolder holder, final int position) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// do some thing
}
// set text to items
}
//the viewholder
public static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View itemView) {
super(itemView);
// finde items
}
}
}
----
// the mothod call adaptet and set it to RecyclerView
public void generateSpecialDaysDetails(List<BarberWorkPeriodItems> workPeriods, String date) {
adapterBarberWorkTimeDetails = new AdapterBarberWorkTimeDetails(workPeriods, R.layout.adapter_barber_work_periods, getActivity(), date, this);
rvWorkPeriod.setHasFixedSize(true);
rvWorkPeriod.setAdapter(adapterBarberWorkTimeDetails);
linearLayoutManager = new GridLayoutManager(getActivity(), 4, StaggeredGridLayoutManager.VERTICAL, false);
rvWorkPeriod.setLayoutManager(linearLayoutManager);
}
// my xml
//item view for adapter;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/txtHours"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginLeft="4dp"
android:layout_marginBottom="4dp"
android:gravity="center"
android:text="00:00"
android:textSize="15dp"
android:textStyle="bold" />
</LinearLayout>
// the RecyclerView
<android.support.v7.widget.RecyclerView
android:id="#+id/rvWorkPeriod"
android:layout_width="0dp"
android:layout_height="300dp"
android:layout_margin="8dp"
android:minHeight="100dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
what is the problem??