2013-11-23 58 views
0

我是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; 
    } 

回答

1

試試這個:

ImageView imageView = (ImageView) convertView.findViewById(R.id.iv_avatar); 
+0

'convertView'不被識別 – user1611830

+0

提供代碼部分,你初始化你的意見 – yurezcv

+0

我剛剛編輯我的文章 – user1611830

-1
convertView = inflater.inflate(R.layout.user_list_item, null); 

ImageView imageView = (ImageView) convertView.findViewById(R.id.iv_avatar); 
+1

它不是很清楚它是如何解決問題? –

0

我不是Android親,但你可以嘗試以下方法:

添加在SingleAdpaterClass

保護活動積極分子= NULL;

SingleTypeAdapter適配器方法,補充一點:

public SingleTypeAdapter(final Activity activity, final int layoutResourceId) { 
    this(activity.getLayoutInflater(), layoutResourceId); 
    activist = activity; 
    } 

這可以讓你在UserListAdapter類獲得一個Activity實例,那麼你可以得到一些視圖,所以

View view = activist.getLayoutInflater().inflate(R.layout.user_list_item); 
相關問題