2013-05-09 74 views
0

獲得的圖像視圖我有一個ListFragment其佈局A的名單上有它的佈局,以及被稱爲B.內部BI在每個項目上有ID爲「imgVi」的形象圖:獲得來自ListFragment

<ImageView 
    android:id="@+id/imgVi" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 

從我的ListFragment方法onCreateView我想獲得訪問這個ImageView來更改圖像src。我怎樣才能做到這一點?。由於這ImageView的是不是在ListFragment佈局,我不能這樣做:

ImageView imgEmp = (ImageView) view.findViewById(R.id.imageViewEmp); 
imgEmp.setBackgroundResource(R.drawable.ic_tab_emp_selected); 

但佈局B是在佈局答:因爲它是一個與它的行列表。

任何幫助將被折衷。我是Android新手。

編輯:我得到了它的工作,只需按照本教程http://thinkandroid.wordpress.com/2010/01/11/custom-cursoradapters/

public class ListClientsCursorAdapter extends SimpleCursorAdapter{ 

private Context context; 
private int layout; 

public ListClientsCursorAdapter(Context context, int layout, Cursor c, 
     String[] from, int[] to, int flags) { 

    super(context, layout, c, from, to, flags); 

    this.context = context; 
    this.layout = layout; 
} 

@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 

    Cursor c = getCursor(); 

    final LayoutInflater inflater = LayoutInflater.from(context); 
    View v = inflater.inflate(layout, parent, false); 

    int nameCol = c.getColumnIndex("name"); 
    String name = c.getString(nombreCol); 

    int telfCol = c.getColumnIndex("telf"); 
    String telf = c.getString(telfCol); 

    /** 
    * Next set the name of the entry. 
    */  

    TextView name_text = (TextView) v.findViewById(R.id.textViewNombEmp); 
    if (name_text != null) { 
     name_text .setText(name); 
    } 

    TextView telf_text = (TextView) v.findViewById(R.id.textViewTelfEmp); 
    if (telf_text != null) { 
     telf_text.setText(telf); 
    } 

    ImageView imgEmp = (ImageView) v.findViewById(R.id.imageViewEmp); 
    if (imgEmp != null) { 
     imgEmp.setBackgroundResource(R.drawable.ic_tab_emp_selected); 
    } 

    return v; 
} 


} 

然後在我的ListFragment我打電話的onCreateView:

ListClientCursorAdapter notes = new ListClientCursorAdapter(context,R.layout.activity_fil_client, mCursor, from, to, 0); 
setListAdapter(notes); 

相反SimpleCursorAdapter的。

+0

我不太明白。所以B是你列表的每一行的佈局。對?這意味着這些圖像視圖中有0個或更多(取決於列表中的項目數量)?如果是這種情況,你應該訪問適配器內的圖像視圖,而不是在你的onCreateView方法! – 2013-05-09 03:08:18

+0

是的,這是正確的。我會爲每一行的圖像視圖。你能給我提供一些例子嗎? – kiduxa 2013-05-09 13:44:42

回答

1

如果您有ListFragment,則必須爲該列表設置適配器。在該適配器內部,您可以覆蓋方法getView(),並且您可以在那裏訪問ImageView,您無法從片段內的onCreateView方法訪問它。

+0

這是一個自定義適配器嗎?我應該在哪裏定義它?我可以重寫ListFragment類中的getView方法嗎? – kiduxa 2013-05-09 13:46:12

+0

您可以覆蓋自定義適配器中的getView()方法,而不是在片段中。 – bogdan 2013-05-13 07:29:18