在将代码从我的主要活动转移到适配器的过程中,我开始遇到一个问题:The method findViewById(int) is undefined for the type VideosAdapter
和The method getResources() is undefined for the type new View.OnClickListener(){}
我找到了这个解决方案:
The method findViewById(int) is undefined for the type
然而,当我试图改变这个时:
fav_up_btn1 = (Button) findViewById(R.id.fav_up_btn1);
到此:
fav_up_btn1 = (Button) v.findViewById(R.id.fav_up_btn1);
我最终得到了这个:
v cannot be resolved
我不确定如何在我的情况下申报v。
public class VideosAdapter extends BaseAdapter {
// The list of videos to display
List<Video> videos;
// An inflator to use when creating rows
private LayoutInflater mInflater;
Button fav_up_btn1;
Button fav_dwn_btn1;
/**
* @param context this is the context that the list will be shown in - used to create new list rows
* @param videos this is a list of videos to display
*/
public VideosAdapter(Context context, List<Video> videos) {
this.videos = videos;
this.mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return videos.size();
}
@Override
public Object getItem(int position) {
return videos.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// If convertView wasn't null it means we have already set it to our list_item_user_video so no need to do it again
if(convertView == null){
// This is the layout we are using for each row in our list
// anything you declare in this layout can then be referenced below
convertView = mInflater.inflate(R.layout.list_item_user_video, parent, false);
}
// We are using a custom imageview so that we can load images using urls
// For further explanation see: http://blog.blundell-apps.com/imageview-with-loading-spinner/
UrlImageView thumb = (UrlImageView) convertView.findViewById(R.id.userVideoThumbImageView);
TextView title = (TextView) convertView.findViewById(R.id.userVideoTitleTextView);
fav_up_btn1 = (Button) findViewById(R.id.fav_up_btn1);
fav_up_btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
boolean favIsUp = fav_up_btn1
.getBackground()
.getConstantState()
.equals(getResources().getDrawable(
R.drawable.fav_up_btn1).getConstantState());
// set the background
fav_up_btn1
.setBackgroundResource(favIsUp ? R.drawable.fav_dwn_btn1
: R.drawable.fav_up_btn1);
}
});
// Get a single video from our list
final Video video = videos.get(position);
// Set the image for the list item
thumb.setImageDrawable(video.getThumbUrl());
// Set the title for the list item
title.setText(video.getTitle());
return convertView;
}
}
答案 0 :(得分:1)
你需要:
fav_up_btn1 = (Button) convertView.findViewById(R.id.fav_up_btn1);
在getView中,它是您正在使用的View'convertView'中的元素。在这种情况下,该布局是list_item_user_video.xml
你说你也遇到了getResources()问题。该方法需要一个上下文(当您在Activity中使用它时,该上下文是隐式的,因为Activity是一个上下文)。但是,在您的适配器中并非如此。
您已在接受适配器构造函数中的上下文,因此请保留对它的引用。有一个班级变量:
Context my_context;
在构造函数中设置它:
public VideosAdapter(Context context, List<Video> videos) {
this.videos = videos;
this.mInflater = LayoutInflater.from(context);
my_context = context;
}
然后你可以使用
在你的适配器中调用getResourcesmy_context.getResources()
答案 1 :(得分:0)
您应该使用convertView
代替v
:
fav_up_btn1 = (Button) convertView.findViewById(R.id.fav_up_btn1);