简体   繁体   中英

RecyclerView display only one value in whole View

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());
}

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());
}

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 )

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM