具有汽车视图的 RecyclerView 适配器仅显示哈希图中的最后一项
RecyclerViewAdapter with carview only showing the last item from hashmap
我有一个包含 cardview 的 recyclerview,我将我的数据存储在哈希图中,但只有最后一项显示在 recyclerview 上,如所附图片所示。当我传递一个列表时,如果我删除 forloop 并改为放置 .get(position) ,一切似乎都有效。
package www.atralk.co.atralk;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;
import com.firebase.geofire.GeoFire;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
public class AdapterRequestList extends RecyclerView.Adapter<AdapterRequestList.MyViewHolders>
{
private String key;
private Spinner cn,sn;
private String hairstyleName;
private String customerCategory;
private String sessionDate;
private String cComment;
private String price;
private String clientID;
private String isPaid;
private DatabaseReference history ;
private DatabaseReference stylistHistoryRef ;
private String stylistID;
private Map<String,Request_View> map;
private Request_View itemDetails;
private int no1,no2;
private String assignedRef;
private Context context;
private String categoryItem;
private String styleItem;
private WebView payWebView;
private Checkout_Page checkoutPage;
private TextView priceText=null;
Request_Details request_details;
public AdapterRequestList(Map<String,Request_View> map, Context c) {
//this.itemList = List;
this.map=map;
this.context = c;
}
@Override
public www.atralk.co.atralk.AdapterRequestList.MyViewHolders onCreateViewHolder(ViewGroup parent, int viewType)
{
return new MyViewHolders(LayoutInflater.from(context).inflate(R.layout.recycler_item, parent, false));
}
@Override
public void onBindViewHolder(final AdapterRequestList.MyViewHolders holder, int position)
{
int i = 0;
for ( Map.Entry<String, Request_View> entry : map.entrySet())
{
if(position == i)
{
key = entry.getKey();
itemDetails = entry.getValue();
holder.title.setText(itemDetails.getCustomerCategory());
holder.description.setText(itemDetails.getHairstyleName());
}
i++;
}
}
private String getDate(Long time)
{
Calendar cal = Calendar.getInstance(Locale.getDefault());
cal.setTimeInMillis(time*1000);
String date = DateFormat.format("MM-dd-yyyy", cal).toString();
return date;
}
@Override
public int getItemCount() {
return this.map.size();
}
class MyViewHolders extends RecyclerView.ViewHolder
{
TextView title,description,options;
public MyViewHolders(@NonNull View itemView)
{
super(itemView);
title=itemView.findViewById(R.id.txtTitle);
description=itemView.findViewById(R.id.txtDescription);
options=itemView.findViewById(R.id.txtOption);
}
}
}
图像显示了一项,但我有一个包含 3 个元素的哈希图。
当我将一些哑数据放入 arraylist 时它可以工作,但随后我的数据存储在 hashmap 中
enter image description here
也许在构造函数中将 map
转换为 List
并改用列表:
public class AdapterRequestList extends RecyclerView.Adapter<AdapterRequestList.MyViewHolders>
{
//add a field in your class
private List<Request_View> list;
.......
.......
//constructor
public AdapterRequestList(Map<String,Request_View> map, Context c) {
//this.itemList = List;
this.map=map;
this.context = c;
//convert map to a list
for ( Map.Entry<String, Request_View> entry : map.entrySet()){
list.add(entry.getValue());
}
}
@Override
public int getItemCount() {
return list.size();
}
@Override
public void onBindViewHolder(final AdapterRequestList.MyViewHolders holder, int position){
Request_View itemDetails = list.get(position);
holder.title.setText(itemDetails.getCustomerCategory());
holder.description.setText(itemDetails.getHairstyleName());
}
我有一个包含 cardview 的 recyclerview,我将我的数据存储在哈希图中,但只有最后一项显示在 recyclerview 上,如所附图片所示。当我传递一个列表时,如果我删除 forloop 并改为放置 .get(position) ,一切似乎都有效。
package www.atralk.co.atralk;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;
import com.firebase.geofire.GeoFire;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
public class AdapterRequestList extends RecyclerView.Adapter<AdapterRequestList.MyViewHolders>
{
private String key;
private Spinner cn,sn;
private String hairstyleName;
private String customerCategory;
private String sessionDate;
private String cComment;
private String price;
private String clientID;
private String isPaid;
private DatabaseReference history ;
private DatabaseReference stylistHistoryRef ;
private String stylistID;
private Map<String,Request_View> map;
private Request_View itemDetails;
private int no1,no2;
private String assignedRef;
private Context context;
private String categoryItem;
private String styleItem;
private WebView payWebView;
private Checkout_Page checkoutPage;
private TextView priceText=null;
Request_Details request_details;
public AdapterRequestList(Map<String,Request_View> map, Context c) {
//this.itemList = List;
this.map=map;
this.context = c;
}
@Override
public www.atralk.co.atralk.AdapterRequestList.MyViewHolders onCreateViewHolder(ViewGroup parent, int viewType)
{
return new MyViewHolders(LayoutInflater.from(context).inflate(R.layout.recycler_item, parent, false));
}
@Override
public void onBindViewHolder(final AdapterRequestList.MyViewHolders holder, int position)
{
int i = 0;
for ( Map.Entry<String, Request_View> entry : map.entrySet())
{
if(position == i)
{
key = entry.getKey();
itemDetails = entry.getValue();
holder.title.setText(itemDetails.getCustomerCategory());
holder.description.setText(itemDetails.getHairstyleName());
}
i++;
}
}
private String getDate(Long time)
{
Calendar cal = Calendar.getInstance(Locale.getDefault());
cal.setTimeInMillis(time*1000);
String date = DateFormat.format("MM-dd-yyyy", cal).toString();
return date;
}
@Override
public int getItemCount() {
return this.map.size();
}
class MyViewHolders extends RecyclerView.ViewHolder
{
TextView title,description,options;
public MyViewHolders(@NonNull View itemView)
{
super(itemView);
title=itemView.findViewById(R.id.txtTitle);
description=itemView.findViewById(R.id.txtDescription);
options=itemView.findViewById(R.id.txtOption);
}
}
}
图像显示了一项,但我有一个包含 3 个元素的哈希图。 当我将一些哑数据放入 arraylist 时它可以工作,但随后我的数据存储在 hashmap 中 enter image description here
也许在构造函数中将 map
转换为 List
并改用列表:
public class AdapterRequestList extends RecyclerView.Adapter<AdapterRequestList.MyViewHolders>
{
//add a field in your class
private List<Request_View> list;
.......
.......
//constructor
public AdapterRequestList(Map<String,Request_View> map, Context c) {
//this.itemList = List;
this.map=map;
this.context = c;
//convert map to a list
for ( Map.Entry<String, Request_View> entry : map.entrySet()){
list.add(entry.getValue());
}
}
@Override
public int getItemCount() {
return list.size();
}
@Override
public void onBindViewHolder(final AdapterRequestList.MyViewHolders holder, int position){
Request_View itemDetails = list.get(position);
holder.title.setText(itemDetails.getCustomerCategory());
holder.description.setText(itemDetails.getHairstyleName());
}