2

のメソッドFragmentActivityの匿名内部クラス内で参照を取得しようとしています。実際、私は で がクリックされ、に接続されたときにを作成しようとしています。bindViewCursorAdapterDialogFragmentImageViewListViewSimpleCursorAdapter

@Override
    public void bindView(View view, Context context, Cursor c) {
        super.bindView(view, context, c);

        ImageView geoEditIcon = (ImageView)view.findViewById(R.id.li_cdf_icon_geoedit);
        geoEditIcon.setImageResource(R.drawable.geolist_edit);
        geoEditIcon.setTag(c.getString(c.getColumnIndex(DBConstants.ID)));

        geoEditIcon.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
              Log.i("geolist", "geoEditIcon clicked");
              String selectedGeoID = v.getTag().toString();
              Log.i("geolist", "geoEditIcon selected Id->"+selectedGeoID);

              EditGeofenceFragment editGeofenceFragment = new EditGeofenceFragment(v.getContext(),selectedGeoID);
              //what context i want to use in Show method
              editGeofenceFragment.show(getActivity().getSupportFragmentManager(), "editGeofenceFragment");
            }
        });
    }

アップデート :

getSupportFragmentManager 参照を MySimpleCursorAdapter のコンストラクターに渡し、それを匿名の内部クラスで使用します。これが、ダイアログ フラグメントの show メソッドです。以下のコードを更新しました。

public MySimpleCursorAdapter(Context context, FragmentManager fragmentManager, int layout, Cursor c,String[] from, int[] to, int flags) {
        super(context, layout, c, from, to, flags);
        this.context=context;
        this.fragmentManager=fragmentManager;
    }


    @Override
    public void bindView(View view, Context context, Cursor c) {
        super.bindView(view, context, c);

        ImageView geoEditIcon = (ImageView)view.findViewById(R.id.li_cdf_icon_geoedit);
        geoEditIcon.setImageResource(R.drawable.geolist_edit);
        geoEditIcon.setTag(c.getString(c.getColumnIndex(DBConstants.ID)));

        geoEditIcon.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
              Log.i("geolist", "geoEditIcon clicked");
              String selectedGeoID = v.getTag().toString();
              Log.i("geolist", "geoEditIcon selected Id->"+selectedGeoID);

              EditGeofenceFragment editGeofenceFragment = new EditGeofenceFragment(v.getContext(),selectedGeoID);
              // Put fragmentManager in first parameter to show method.
              editGeofenceFragment.show(fragmentManager, "editGeofenceFragment");
            }
        });
    }
4

3 に答える 3

3

への参照を取得しようとしているので、 の内部への参照をFragmentManager保持し、それを のコンストラクターに渡すことができます。finalFragmentActivitySimpleCursorAdapterSimpleCursorAdapter

private final FragmentActivity mFragmentActivity;

public YourSimpleCursorAdapter(Context context, FragmentActivity fragmentActivity) {
    // Deprecated in API 11, needed on < API 11 devices
    super(context, null);

    mFragmentActivity = fragmentActivity;
}

次に、匿名の内部クラスでその参照を使用して、FragmentManager.

editGeofenceFragment.show(mFragmentActivity.getSupportFragmentManager(), "editGeofenceFragment");
于 2013-02-22T15:50:21.257 に答える
1

コンストラクターで Activity コンテキストを取得できます。要素クラスに保存するだけです:

Context context;
public myCursorAdapter(Context context, Cursor c) {
this.context=context;
...
}
于 2013-02-22T15:53:16.857 に答える
0

渡されたコンテキストを使用できます。メソッドに渡されるbindViewコンテキストは、を作成するために渡したコンテキストと同じですSimpleCursorAdapter。単にfinalにするのではなく、匿名の内部クラス内で使用する必要がある場合。メソッド呼び出しまたはメソッド内の2次変数のいずれか。例えば:

@Override
public void bindView(View view, final Context context, Cursor c) {
  ...
  geoEditIcon.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {
      ...
      EditGeofenceFragment editGeofenceFragment = new EditGeofenceFragment(context,selectedGeoID);
      ...
    }
  });
}
于 2013-02-22T15:48:11.847 に答える