2013-05-09 75 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