简体   繁体   中英

Error in RecyclerViewAdapter's onBindViewHolder()

I have a problem in the onBindViewHolder method in my adapter. I want to create a scrollable list of items (each item is composed by an image and 3 text fields) that grabs its information from a random API that I chose and displays it. The problem is that the onBindViewHolder method lets me create the list of items only with the imatge and the first textview, but when it starts executing the second text view it crashes and I don't know why is happennig that. If anyone knows the answer I would be very happy, thanks!

LogCat

    --------- beginning of crash
2021-03-29 16:48:21.353 2548-2548/com.example.prova2 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.prova2, PID: 2548
    android.content.res.Resources$NotFoundException: String resource ID #0x0
        at android.content.res.Resources.getText(Resources.java:339)
        at android.widget.TextView.setText(TextView.java:5496)
        at com.example.prova2.ItemAdapter.onBindViewHolder(ItemAdapter.java:45)
        at com.example.prova2.ItemAdapter.onBindViewHolder(ItemAdapter.java:21)
        at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
        at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
.
.
. etc

Adapter File

@Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Item i = dataset.get(position);
        holder.totalTextView.setText(i.getId());
        Log.e(TAG, "holder.getId fet: " + i.getUser_id());
        holder.totalHitsTextView.setText(i.getFavorites());
        holder.hitsView.setText(i.getImageHeight());

        String url = i.getUserImageURL();
        Glide.with(mContext).load(url).centerCrop().
                diskCacheStrategy(DiskCacheStrategy.ALL).into(holder.fotoImageView);

        holder.parent_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

change holder.totalTextView.setText(Si.getId()); to holder.totalTextView.setText(String.valueOf(Si.getId())); and holder.hitsView.setText(i.getImageHeight()); to holder.hitsView.setText(String.valueOf(i.getImageHeight()))

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