Please, help to figure out why my implementation of RecyclerView doesn't show anything.
Retrieving data asynchronously and result is successfull, but don't know how to display it correctly.
Activity
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
checkAndRequestPermissions(Manifest.permission.INTERNET);
RecyclerView recyclerView = binding.recyclerView;
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
NewsAdapter adapter = new NewsAdapter();
recyclerView.setAdapter(adapter);
RssService.runRssFeed(newsList -> {
System.out.println("SIZE ----- " + newsList.size());
adapter.setItems(newsList);
});
}
}
Adapter
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
private final List<NewsModel> news = new ArrayList<>();
#Override
public NewsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.news_item, parent, false);
return new ViewHolder(view);
}
public void setItems(List<NewsModel> news) {
this.news.addAll(news);
notifyDataSetChanged();
}
public void clearItems() {
this.news.clear();
notifyDataSetChanged();
}
#Override
public void onBindViewHolder(#NonNull NewsAdapter.ViewHolder holder, int position) {
holder.title.setText(news.get(holder.getAdapterPosition()).getTitle());
holder.date.setText(news.get(holder.getAdapterPosition()).getDate());
holder.link.setText(news.get(holder.getAdapterPosition()).getLink());
}
#Override
public int getItemCount() {
return news.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
final TextView title, date, link;
public ViewHolder(View view) {
super(view);
title = view.findViewById(R.id.titleTxt);
date = view.findViewById(R.id.dateTxt);
link = view.findViewById(R.id.linkTxt);
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
</LinearLayout>
news_item.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:layout_gravity="top"
android:orientation="vertical">
<TextView
android:id="#+id/titleTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="16sp"
android:textStyle="normal"
android:typeface="sans" />
<TextView
android:id="#+id/linkTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textColor="#color/white"
android:textSize="12sp"
android:textStyle="normal"
android:typeface="sans" />
<TextView
android:id="#+id/dateTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginTop="10dp"
android:textColor="#color/white"
android:textSize="12sp"
android:textStyle="normal"
android:typeface="sans" />
</LinearLayout>
I think It showed correctly but you can't see because your text color is white
**issue in these lines**
holder.title.setText(news.get(holder.getAdapterPosition()).getTitle());
holder.date.setText(news.get(holder.getAdapterPosition()).getDate());
holder.link.setText(news.get(holder.getAdapterPosition()).getLink());
change these like
holder.title.setText(news.get(position).getTitle());
Related
I am using card view within recycler view and I want to change the color of each single card view means Every card view has different color like in the picture I'm using retrofit technique for fetching data. please tell me where to put what code as I'm new in java android TIA
This is my adapter class
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.MyViewHolder>{
private Context context;
private List<Corona> dataList;
public DataAdapter(Context context, List<Corona> dataList) {
this.context = context;
this.dataList = dataList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.data,parent,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.nameTv.setText(dataList.get(position).getName());
holder.cityTv.setText(dataList.get(position).getCity());
}
#Override
public int getItemCount() {
return dataList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
public TextView nameTv, cityTv;
public MyViewHolder(View itemView) {
super(itemView);
nameTv = (TextView) itemView.findViewById(R.id.tvName);
cityTv = (TextView) itemView.findViewById(R.id.tvCity);
}
}
}
here is XML of Card View
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:cardBackgroundColor="#EDCC1A"
app:cardCornerRadius="15dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<TextView
android:id="#+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Hello World!"
android:textSize="25sp"
android:textStyle="bold"
android:textColor="#F00"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tvCity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#0000FF"
android:textSize="25sp"
android:textStyle="bold"
android:layout_below="#+id/tvName"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
here is my main activity
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private DataAdapter dataAdapter ;
private List<Corona> dataArrayList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews(){
recyclerView=(RecyclerView) findViewById(R.id.recview);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
getCoronaData();
}
private void getCoronaData() {
ApiInterface apiInterface;
apiInterface = ApiClient.getClient().create(ApiInterface.class);
dataArrayList = new ArrayList<>();
Call<List<Corona>> call = apiInterface.getCoronaDataForParsing();
call.enqueue(new Callback<List<Corona>>() {
#Override
public void onResponse(Call<List<Corona>> call, Response<List<Corona>> response) {
dataArrayList = response.body();
dataAdapter=new DataAdapter(getApplicationContext(),dataArrayList);
recyclerView.setAdapter(dataAdapter);
}
#Override
public void onFailure(Call<List<Corona>> call, Throwable t) {
}
});
}
}
first create integer.xml file under /res/values like below:
#Add as many color items as your want.
integer.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer-array name="color_array">
<item>#color/color1</item>
<item>#color/color2</item>
<item>#color/color3</item>
<item>#color/color4</item>
<item>#color/color5</item>
</integer-array>
Then, Inside RecyclerViewAdapter pass it through the constructor like below:
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.MyViewHolder>{
private Context context;
private List<Corona> dataList;
private int[] colorList;
public DataAdapter(Context context, List<Corona> dataList){
this.context = context;
this.dataList = dataList;
colorList = context.getResources().getIntArray(R.array.color_array);
}
........
#Override
public void onBindViewHolder(.....){
......
//for setting the different colors to each cardview.
GradientDrawable drawable = (GradientDrawable) holder.cardLayout.getBackground();
drawable.setColor(colorList[position % colorList.length]);
}
public class MyViewHolder extends RecyclerView.ViewHolder{
CardView cardLayout;
.........
public MyViewHolder(View itemView){
super(itemView)
.........
cardLayout = itemView.findViewById(R.id.cardLayout);
}
}
}
And, finally Inside XML of CardView provide id to Cardview as follow:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.cardview.widget.CardView
android:id="#+id/cardLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:cardBackgroundColor="#EDCC1A"
app:cardCornerRadius="15dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<TextView
android:id="#+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Hello World!"
android:textSize="25sp"
android:textStyle="bold"
android:textColor="#F00"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tvCity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#0000FF"
android:textSize="25sp"
android:textStyle="bold"
android:layout_below="#+id/tvName"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
First of all set an ID to cardView in your XML file, then initialize cardView in MyViewHolder :
CardView cv = (CardView) itemView.findViewById(R.id.YOUR_CARDVIEW_ID);
for adding random colors, generate some colors and add them to a List and pass this list to the adapter.
now go to onBindViewHolder and add this code :
holder.cv.setCardBackgroundColor(YourList.get(i));
RecyclerView is not showing at all. Only the TextView is displayed.
I looked at other questions and used all answers from previous questions.
Other answers suggested that recycler's width can't be set wrap content or that setAdapter should be called after setting layout manager and I meet these conditions.
MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.main_recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
final MainAdapter adapter = new MainAdapter();
recyclerView.setAdapter(adapter);
List<FirebaseProduct> firebaseProductList = new ArrayList<>();
firebaseProductList = getData(); //Here is my Firebase code
adapter.setList(firebaseProductList);
}
}
activity_main.xml
<RelativeLayout 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/main_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/aktualne_produkty"
android:gravity="center"
android:background="#color/colorPrimary"
android:textAppearance="#style/TextAppearance.AppCompat.Large">
</TextView>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/main_recycler"
android:layout_height="wrap_content"
android:layout_width="match_parent"
tools:listitem="#layout/card_main"
android:layout_below="#id/main_text"/>
</RelativeLayout>
MainAdapter
public class MainAdapter extends RecyclerView.Adapter<MainAdapter.MainHolder> {
private List<FirebaseProduct> productList = new ArrayList<>();
#NonNull
#Override
public MainHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_main, parent, false);
return new MainHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull MainHolder holder, int position) {
FirebaseProduct product = productList.get(position);
Log.d("Prod holder", "name - " + product.getProductName());
Log.d("Prod holder", "num - " + product.getProductNumber());
holder.nameView.setText(product.getProductName());
holder.numView.setText(product.getProductNumber());
}
#Override
public int getItemCount() {
return productList.size();
}
public void setProductList(List<FirebaseProduct> productList1){
this.productList = productList1;
notifyDataSetChanged();
}
public List<FirebaseProduct> getList(){
return productList;
}
public static class MainHolder extends RecyclerView.ViewHolder{
private TextView nameView;
private TextView numView;
public MainHolder(View itemView){
super(itemView);
nameView = itemView.findViewById(R.id.product_name);
numView = itemView.findViewById(R.id.product_count);
}
}
}
card_main
<androidx.cardview.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:layout_weight="3">
<TextView
android:id="#+id/product_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
</TextView>
<TextView
android:id="#+id/product_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/product_name">
</TextView>
</RelativeLayout>
<ImageView
android:id="#+id/product_image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:contentDescription="#string/product_description">
</ImageView>
</LinearLayout>
</androidx.cardview.widget.CardView>
You are calling adapter.setList(firebaseProductList); in MainActivity but the function name in MainAdapter is setProductList(). So change it to adapter. setProductList(firebaseProductList)
Also, you want to be sure getData(); in MainActivity should return a non-empty list. you can verify it using debugger or by just adding a log statement before adapter.setList(firebaseProductList). like this:
Log.d("LIST_SIZE", firebaseProductList.size()); //this will print list size
adapter.setList(firebaseProductList); //you have to change it to adapter. setProductList(firebaseProductList)
I've been trying to get a custom Recycler view adapter to work. I can't quite see why it wouldn't work. What I find strange is that the code for the custom adapter does not execute (as in it doesn't break when I put a breakpoint there) and I'm certain I bind the custom adapter to the recycler view. I hope someone can see why it doesn't work.
Activity:
public class payment_history extends AppCompatActivity {
RecyclerView list;
ArrayList<pay_item> list_data;
pay_adapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_payment_history);
list_data = new ArrayList<>();
list_data.add(new pay_item(10, 100, "jow", "10"));
list_data.add(new pay_item(10, 100, "joe", "10"));
list_data.add(new pay_item(10, 100, "joe", "10"));
list = findViewById(R.id.payment_history_list);
list.setLayoutManager(new LinearLayoutManager(this));
adapter = new pay_adapter(list_data);
list.setHasFixedSize(true);
list.setAdapter(adapter);
}
}
class pay_item {
int time;
double amount;
String name, table;
pay_item(int time, double amount, String name, String table) {
this.time = time;
this.amount = amount;
this.name = name;
this.table = table;
}
}
Adapter:
class pay_adapter extends RecyclerView.Adapter<pay_adapter.viewholder> {
private ArrayList<pay_item> data;
pay_adapter(ArrayList<pay_item> in) {
data = in;
}
#NonNull
#Override
public viewholder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.payment_history_item, parent, false);
return new viewholder(v);
}
#Override
public void onBindViewHolder(#NonNull viewholder holder, int position) {
holder.name.setText(data.get(position).name);
holder.table.setText(data.get(position).table);
holder.amm.setText(String.valueOf(data.get(position).amount));
holder.time.setText(data.get(position).time);
}
#Override
public int getItemCount() {
return 0;
}
static class viewholder extends RecyclerView.ViewHolder {
TextView time, amm, name, table;
viewholder(View view) {
super(view);
time = view.findViewById(R.id.pay_time);
amm = view.findViewById(R.id.pay_amount);
name = view.findViewById(R.id.pay_name);
table = view.findViewById(R.id.pay_table);
}
}
}
Row view:
<?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:paddingStart="16dp"
android:paddingEnd="16dp">
<TextView
android:id="#+id/pay_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Time"
android:textColor="#000000"
android:textSize="36sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/pay_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
android:textColor="#000000"
android:textSize="24sp" />
<TextView
android:id="#+id/pay_table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:text="table" />
</LinearLayout>
<TextView
android:id="#+id/pay_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0"
android:text="Amount"
android:textColor="#color/colorPrimary"
android:textSize="36sp" />
</LinearLayout>
Layout with recycler view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".payment_history">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/payment_history_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Any help is appreciated.
Two things
pay_adapter(ArrayList<pay_item> in) {
data = in;
}
#Override
public int getItemCount() {
return data.size() ;
}
I have a common problem - recyclerView lags and scroll unsmooth. I've read many tutorials and implemented their methods, but no success. I have imageView and textView in each raw of recycleView. I tried to use image loaders like Glide or Picasso, but there are some delay when load images. What is the reason of bad performance? Here is row layout:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingEnd="5dp"
android:paddingStart="5dp"
android:text="#string/textview"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="#string/image"
android:scaleType="fitCenter"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text2" />
and list layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/list_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollingCache="false"
android:animationCache="false"
/>
CustomAdapter:
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private Context context;
private ArrayList<Image> DrinkArrayList;
public CustomAdapter(Context context, ArrayList<Image> DrinkArrayList) {
this.context = context;
this.DrinkArrayList = DrinkArrayList;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(
LayoutInflater.from(context)
.inflate(R.layout.my_image_list, parent, false));
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Image image = DrinkArrayList.get(position);
holder.name1.setText(image.getName());
holder.iv1.setImageResource(image.getImageResourceId());
}
#Override
public int getItemCount() {
return this.DrinkArrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView name1;
private ImageView iv1;
public ViewHolder(View itemView) {
super(itemView);
this.name1 = itemView.findViewById(R.id.text2);
this.iv1 = itemView.findViewById(R.id.imageView2);
}
}
}
related methods in activity:
RecyclerView listDrinks2 = findViewById(R.id.list_image);
listDrinks2.setAdapter(new CustomAdapter(this, DrinkArrayList));
listDrinks2.setLayoutManager(new LinearLayoutManager(this));
I want to make this:
I have a RecyclerView, wherein for each item I create I want to show the time when the item was created. I want to display the time within this item.
My code:
public class VerIncidencia extends AppCompatActivity {
private FirebaseRecyclerAdapter mAdapter;
private DatabaseReference mDatabase;
String idIncidencia;
String idEmpresa;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ver_incidencia);
String uid = FirebaseAuth.getInstance().getUid();
idIncidencia = getIntent().getStringExtra("INCIDENCIA_KEY");
idEmpresa = getIntent().getStringExtra("EMPRESA_KEY");
mDatabase = FirebaseDatabase.getInstance().getReference().child("incidencia").child(uid);
RecyclerView recyclerView = findViewById(R.id.list_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
Query postsQuery = mDatabase;
FirebaseRecyclerOptions options = new FirebaseRecyclerOptions.Builder<Incidencia>()
.setQuery(postsQuery, Incidencia.class)
.setLifecycleOwner(this)
.build();
mAdapter = new FirebaseRecyclerAdapter<Incidencia, IncidenciaViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull IncidenciaViewHolder holder, final int position, #NonNull final Incidencia empresa) {
holder.departamento.setText(empresa.departamento);
holder.prioridad.setText(empresa.prioridad);
holder.motivo.setText(empresa.motivo);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(VerIncidencia.this, VerIncidenciaCompleta.class);
intent.putExtra("INCIDENCIA_KEY", getRef(position).getKey());
startActivity(intent);
}
});
}
#Override
public IncidenciaViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_incidencia, parent, false);
return new IncidenciaViewHolder(view);
}
};
recyclerView.setAdapter(mAdapter);
}
}
ViewHolder:
public class IncidenciaViewHolder extends RecyclerView.ViewHolder{
TextView departamento;
TextView prioridad;
TextView motivo;
public IncidenciaViewHolder(View itemView) {
super(itemView);
departamento = itemView.findViewById(R.id.departamento);
prioridad = itemView.findViewById(R.id.prioridad);
motivo = itemView.findViewById(R.id.motivo);
}
}
XML:
<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/departamento"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_marginTop="-40dp"
android:textStyle="bold"
android:textColor="#android:color/black"
android:textSize="25sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/prioridad"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_marginTop="30dp"
android:textColor="#android:color/black"
android:textSize="25sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/motivo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_marginTop="85dp"
android:textColor="#android:color/black"
android:textSize="25sp" />
</LinearLayout>