RecyclerView在整个视图中只显示一个值

huangapple go评论69阅读模式
英文:

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&lt;String, Double&gt; stringDoubleMap) {

    private HashMap&lt;String, Double&gt; rates = new HashMap&lt;&gt;();


            if (rates.size() &gt; 0){
                rates.clear();
            }
            if (!stringDoubleMap.isEmpty()){
                rates.putAll(stringDoubleMap);
                CurrencyAdapter adapter = new CurrencyAdapter(rates);
                recyclerView.setAdapter(adapter);
            }
        }

Adapter

HashMap&lt;String, Double&gt; asd = new HashMap();

public CurrencyAdapter(HashMap&lt;String, Double&gt; rates){
    this.asd = rates;
    Log.i(TAG, &quot;CurrencyAdapter: constructor &quot;+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&lt;String, Double&gt; entry : asd.entrySet()){
        System.out.println(entry.getKey());
        holder.mNationality.setText(entry.getKey());
        holder.mValue.setText(String.valueOf(entry.getValue()));

    }
    Log.i(TAG, &quot;onBindViewHolder: &quot;+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&lt;String, Double&gt; entry  = asd.get(position);
	System.out.println(entry.getKey());
	holder.mNationality.setText(entry.getKey());
	holder.mValue.setText(String.valueOf(entry.getValue()));

    Log.i(TAG, &quot;onBindViewHolder: &quot; + asd.size());
}

答案2

得分: 1

onBindViewHolder会针对数组中的每个单独项目进行调用(注意position参数),因此您不应在其中自行进行迭代,只需从您的HashMap中获取一个项目,并在Viewholder)上绘制。

英文:

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

huangapple
  • 本文由 发表于 2020年9月21日 20:57:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63992749.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定