我是Android
的新手,我正在开展一个项目,我需要自定义一些视图。我需要从一些类imageview
类中夸大的布局中获得一些BaseAdapter
。当我尝试:
ImageView imageView = (ImageView) getView(R.layout.user_list_item).findViewById(R.id.iv_avatar);
我收到错误,因为在BaseAdapter
课程中有
@Override
public View getView(final int position, View convertView,
final ViewGroup parent) {
if (convertView == null)
convertView = initialize(inflater.inflate(layout, null));
try {
update(position, convertView, getItem(position));
} catch (IOException e) {
e.printStackTrace();
}
return convertView;
}
如何获得此ImageView
?我也尝试了View view = getLayoutInflater().inflate(R.layout.user_list_item)
,但它无法正常工作
以下是视图的初始化方式
public class UserListAdapter extends SingleTypeAdapter<User> {
public UserListAdapter(LayoutInflater inflater, List<User> items) {
super(inflater, R.layout.user_list_item);
setItems(items);
}
和这个方法
@Override
protected int[] getChildViewIds() {
return new int[] { R.id.iv_avatar, R.id.tv_name };
}
至于SingleTypeAdapter
,就像这样
public abstract class SingleTypeAdapter<V> extends TypeAdapter {
private static final Object[] EMPTY = new Object[0];
private final LayoutInflater inflater;
private final int layout;
private final int[] children;
private Object[] items;
/**
* Create adapter
*
* @param activity
* @param layoutResourceId
*/
public SingleTypeAdapter(final Activity activity, final int layoutResourceId) {
this(activity.getLayoutInflater(), layoutResourceId);
}
/**
* Create adapter
*
* @param context
* @param layoutResourceId
*/
public SingleTypeAdapter(final Context context, final int layoutResourceId) {
this(LayoutInflater.from(context), layoutResourceId);
}
/**
* Create adapter
*
* @param inflater
* @param layoutResourceId
*/
public SingleTypeAdapter(final LayoutInflater inflater,
final int layoutResourceId) {
this.inflater = inflater;
this.layout = layoutResourceId;
items = EMPTY;
int[] childIds = getChildViewIds();
if (childIds == null)
childIds = new int[0];
children = childIds;
}
答案 0 :(得分:1)
试试这个:
ImageView imageView = (ImageView) convertView.findViewById(R.id.iv_avatar);
答案 1 :(得分:0)
我不是Android
的专业人士,但你可以尝试以下方法:
添加SingleAdpaterClass
protected Activity activist = null;
并在SingleTypeAdapter
适配器方法中添加:
public SingleTypeAdapter(final Activity activity, final int layoutResourceId) {
this(activity.getLayoutInflater(), layoutResourceId);
activist = activity;
}
这允许您在Activity
类中获取UserListAdapter
实例,然后您可以获得一些视图
View view = activist.getLayoutInflater().inflate(R.layout.user_list_item);
答案 2 :(得分:-1)
convertView = inflater.inflate(R.layout.user_list_item, null);
ImageView imageView = (ImageView) convertView.findViewById(R.id.iv_avatar);