[英]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);
我尝试了声明foto = (ImageView) findViewById(R.id.Foto);
in method @Override public void onBindViewHolder(ViewHolder Viewholder, int position) {
在方法@Override public void onBindViewHolder(ViewHolder Viewholder, int position) {
and I tried to use protected void onCreate(Bundle savedInstanceState) {
but it's still error 并且我尝试使用protected void onCreate(Bundle savedInstanceState) {
但这仍然是错误的
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()
. 此错误意味着您使用了错误的对象来调用findViewById()
。 You must use an Activity
or a View
to call this method. 您必须使用Activity
或View
来调用此方法。 Most likely you need to obtain the view from the ViewHolder
which is passed to the onBindViewHolder()
method. 您最有可能需要从ViewHolder
获取视图,该视图已传递给onBindViewHolder()
方法。 Something like this 像这样
View v = viewholder.itemView;
foto = (ImageView) v.findViewById(R.id.Foto);
ps The declaration ViewHolder Viewholder
is incredibly confusing. ps声明ViewHolder Viewholder
令人难以置信。 Usually variable names start with lower case. 通常,变量名以小写字母开头。 Better yet, use a significantly different word such as ViewHolder holder
. 更好的是,使用明显不同的词,例如ViewHolder holder
。 This makes it clear what the type and name are without needing to look so closely at the exact letters and case. 这样就可以清楚地知道类型和名称,而不必仔细查看确切的字母和大小写。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.