0

レイアウト A を持つ ListFragment があり、リストの各項目に対して、B と呼ばれるレイアウトもあります。BI 内には、ID "imgVi" を持つイメージ ビューがあります。

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

メソッド onCreateView の ListFragment から、この ImageView にアクセスして、画像の src を変更したいと考えています。これどうやってするの?。このimageViewはListFragmentレイアウトにないため、これを行うことはできません:

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

ただし、レイアウト B はレイアウト A にあります。行を含むリストであるためです。

どんな助けも高く評価されます。私はAndroidが初めてです。

EDITED:私はそれがうまくいった、ちょうどこのチュートリアルに従ってください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 の代わりに。

4

1 に答える 1

1

がある場合はListFragment、リストのアダプターを設定する必要があります。そのアダプター内では、 method をオーバーライドgetView()でき、そこにアクセスできますが、フラグメント内のメソッドImageViewからアクセスすることはできません。onCreateView

于 2013-05-09T07:46:19.137 に答える