gpt4 book ai didi

java - RecyclerView 适配器与卡片 View 仅显示散列图中的最后一项

转载 作者:行者123 更新时间:2023-12-01 17:00:30 25 4
gpt4 key购买 nike

我有一个包含卡片 View 的回收器 View ,我将数据存储在 HashMap 中,但只有最后一个项目显示在回收器 View 上,如附图所示。当我传递一个列表时,如果我删除 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 个元素的 HashMap 。当我将一些愚蠢的数据放入数组列表中时,它可以工作,但随后我的数据存储在 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());



}

关于java - RecyclerView 适配器与卡片 View 仅显示散列图中的最后一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61513678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com