我是Android
的新手,我正在開發一個項目,我需要自定義一些視圖。我需要從類似BaseAdapter
的膨脹佈局中獲得一些imageview
。當我嘗試:如何在活動類別之外獲取視圖
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;
}
'convertView'不被識別 – user1611830
提供代碼部分,你初始化你的意見 – yurezcv
我剛剛編輯我的文章 – user1611830