1

This is my class:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

And I tried the declaration foto = (ImageView) findViewById(R.id.Foto); in method @Override public void onBindViewHolder(ViewHolder Viewholder, int position) {

And I tried to use protected void onCreate(Bundle savedInstanceState) {, but it's still an error.

Adapter class

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    Context context;
    private static final String TAG = RecyclerViewAdapter.class.getSimpleName();

    List<GetDataAdapter> getDataAdapter;

    ImageLoader imageLoader1;
    private ImageView foto;
    String getFoto;
    String FIXURL = "http://192.168.0.103/AndroidFileUpload/";
    String url = FIXURL + "uploads/";


    public RecyclerViewAdapter(List<GetDataAdapter> getDataAdapter, Context context) {

        super();
        this.getDataAdapter = getDataAdapter;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);

        ViewHolder viewHolder = new ViewHolder(v);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder Viewholder, int position) {

        final GetDataAdapter getDataAdapter1 = getDataAdapter.get(position);

        imageLoader1 = ServerImageParseAdapter.getInstance(context).getImageLoader();

        //        imageLoader1.get(getDataAdapter1.getFoto(),
        //                ImageLoader.getImageListener(
        //                        Viewholder.networkImageView, // Server Image
        //                        R.mipmap.ic_launcher, // Before loading server image the default showing image.
        //                        android.R.drawable.ic_dialog_alert // Error image if requested image dose not found on server.
        //                )
        //        );
        //        getFoto = getDataAdapter1.getIdJalan();;
        //        String FullURL = url + getFoto;
        foto = (ImageView) findViewById(R.id.Foto);
        //        Picasso.with(getApplicationContext()).load(url + getDataAdapter1.getFoto()).into(foto);

        Viewholder.networkImageView.setImageUrl(getDataAdapter1.getFoto(), imageLoader1);

        Viewholder.ImageTitleNameView.setText(getDataAdapter1.getIdJalan());

        Viewholder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "coba nih5: " + getDataAdapter1.getIdJalan());
                //
                //Intent intent = new Intent(v.getContext(), SeconActivity.class);
                //intent.putExtra("key", getDataAdapter1.getImageTitleName()); // getDataAdapter1 in your case
                //context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {

        return getDataAdapter.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        public TextView ImageTitleNameView;
        public NetworkImageView networkImageView;

        public ViewHolder(View itemView) {

            super(itemView);

            ImageTitleNameView = (TextView) itemView.findViewById(R.id.textView_item);

            networkImageView = (NetworkImageView) itemView.findViewById(R.id.VollyNetworkImageView1);
        }
    }
}

This is the layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cardview1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardElevation="3dp"
    card_view:contentPadding="3dp"
    card_view:cardCornerRadius="3dp"
    card_view:cardMaxElevation="3dp"
    >


    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/Foto"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@mipmap/ic_launcher"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Image Name"
            android:id="@+id/textView_item"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/VollyNetworkImageView1"
            android:layout_toEndOf="@+id/VollyNetworkImageView1"
            android:layout_marginLeft="20dp"/>

    </RelativeLayout>

    </android.support.v7.widget.CardView>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Eggy
  • 522
  • 5
  • 29

2 Answers2

6

This error means that you are using the wrong object to call findViewById(). You must use an Activity or a View to call this method. Most likely you need to obtain the view from the ViewHolder which is passed to the onBindViewHolder() method. Something like this

View v = viewholder.itemView;
foto = (ImageView) v.findViewById(R.id.Foto);

p.s. The declaration ViewHolder Viewholder is incredibly confusing. Usually variable names start with lower case. Better yet, use a significantly different word such as ViewHolder holder. This makes it clear what the type and name are without needing to look so closely at the exact letters and case.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
-3

The error message you encountered usually arises due to a mismatch between the XML layout file you are inflating and the layout items used in your ViewHolder class. To resolve this issue, carefully follow these steps:

  1. Check Layout File: Ensure that you have correctly inflated the appropriate layout resource file, which should be named "recyclerview_items.xml," and it should be located in the "res/layout" directory.

  2. Verify ViewHolder Class: Double-check your ViewHolder class to ensure that it correctly references the views present in the "recyclerview_items.xml" layout file. Make sure that the view IDs used in the ViewHolder class match the IDs defined in the layout file.

Here's an example of how to implement the onCreateViewHolder method:

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val view = LayoutInflater.from(parent.context).inflate(R.layout.recyclerview_items, parent, false)
    return ViewHolder(view)
}

By carefully inspecting your layout file and ViewHolder class, you should be able to identify and fix any discrepancies, resolving the error you encountered.

Qadir Khan
  • 87
  • 1
  • 6
  • 1
    This answer looks like ChatGPT – DavidW Aug 01 '23 at 07:31
  • just the text is fixed from chat GPT, but the answer is based on my own experience, when I have too many items layout for different Adapters, sometimes forget to Inflate the correct layout file which cause this issue. – Qadir Khan Aug 01 '23 at 09:09