简体   繁体   中英

cannot resolve symbol findViewById in class with extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>

this my class

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

and I tried 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 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>

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

ps 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.

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