英文:
RecyclerView display only one value in whole View
问题
MainActivity
@Override
public void onChanged(Map<String, Double> stringDoubleMap) {
HashMap<String, Double> rates = new HashMap<>();
if (!stringDoubleMap.isEmpty()){
rates.putAll(stringDoubleMap);
CurrencyAdapter adapter = new CurrencyAdapter(rates);
recyclerView.setAdapter(adapter);
}
}
Adapter
HashMap<String, Double> rates = new HashMap();
public CurrencyAdapter(HashMap<String, Double> rates){
this.rates = rates;
Log.i(TAG, "CurrencyAdapter: constructor " + rates.size());
}
@NonNull
@Override
public CurrencyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.currency_item, parent, false);
return new CurrencyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull CurrencyViewHolder holder, int position) {
String[] keys = rates.keySet().toArray(new String[0]);
String key = keys[position];
Double value = rates.get(key);
holder.mNationality.setText(key);
holder.mValue.setText(String.valueOf(value));
Log.i(TAG, "onBindViewHolder: " + rates.size());
}
英文:
I'd wanted to make an RecyclerView which will display the response from api call. Everything is setup by MVVM, and Retrofit. My Main struggle is about adapter class, it's basicly displaying same values in every text view. The response is correct for sure (I've checked it by logger) another thing I saw is the copying all response over and over (in console) how I may solve it to have properly displayed response?
MainActivity
@Override
public void onChanged(Map<String, Double> stringDoubleMap) {
private HashMap<String, Double> rates = new HashMap<>();
if (rates.size() > 0){
rates.clear();
}
if (!stringDoubleMap.isEmpty()){
rates.putAll(stringDoubleMap);
CurrencyAdapter adapter = new CurrencyAdapter(rates);
recyclerView.setAdapter(adapter);
}
}
Adapter
HashMap<String, Double> asd = new HashMap();
public CurrencyAdapter(HashMap<String, Double> rates){
this.asd = rates;
Log.i(TAG, "CurrencyAdapter: constructor "+asd.size());
}
@NonNull
@Override
public CurrencyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.currency_item, parent, false);
return new CurrencyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull CurrencyViewHolder holder, int position) {
for (Map.Entry<String, Double> entry : asd.entrySet()){
System.out.println(entry.getKey());
holder.mNationality.setText(entry.getKey());
holder.mValue.setText(String.valueOf(entry.getValue()));
}
Log.i(TAG, "onBindViewHolder: "+asd.size());
}
答案1
得分: 2
不要在onBindViewHolder
中遍历所有项目,因为它每次调用时只为单个项目调用。
因此,用以下内容替换onBindViewHolder
@Override
public void onBindViewHolder(@NonNull CurrencyViewHolder holder, int position) {
Map.Entry<String, Double> entry = asd.get(position);
System.out.println(entry.getKey());
holder.mNationality.setText(entry.getKey());
holder.mValue.setText(String.valueOf(entry.getValue()));
Log.i(TAG, "onBindViewHolder: " + asd.size());
}
英文:
Don't iterate over all items in onBindViewHolder
, as it's called separately for only a single item each time it's get called.
So, replace onBindViewHolder
with
@Override
public void onBindViewHolder(@NonNull CurrencyViewHolder holder, int position) {
Map.Entry<String, Double> entry = asd.get(position);
System.out.println(entry.getKey());
holder.mNationality.setText(entry.getKey());
holder.mValue.setText(String.valueOf(entry.getValue()));
Log.i(TAG, "onBindViewHolder: " + asd.size());
}
答案2
得分: 1
onBindViewHolder
会针对数组中的每个单独项目进行调用(注意position
参数),因此您不应在其中自行进行迭代,只需从您的HashMap
中获取一个项目,并在View
(holder
)上绘制。
英文:
onBindViewHolder
is called for every item in your array separately (note position
param), so you shouldn't iterate by your own in there, just get one item from your HashMap
and draw on View
(holder
)
答案3
得分: 0
onBindViewholder
的工作原理是循环遍历 RecyclerView 的位置,并针对每个位置完成代码。position
变量在 onBindViewholder
中传递给您,以便您可以区分每个位置。
哈希映射在 RecyclerView 中不起作用,因为您无法通过其位置将哈希映射分隔开。在将映射传递给 RecyclerView 之前,将其转换为 ArrayList,然后使用 position
变量获取您的 ArrayList 中相应的位置。
英文:
the way that onBindViewholder works is that it loops through the positions of the recyclerview and completes the code for each position.The position variable is passed to you in the onBindViewholder so that you can differentiate between each position.
Hashmaps don't work with recyclerview because you can't separate the hashmap by its position. Before passing your map the the recyclerview, turn it into an arraylist and then use the position variable to get the corresponding position in your arraylist
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论