如何在bindView方法的匿名内部类中获取FragmentActivity引用?

时间:2013-02-22 15:36:39

标签: android android-cursoradapter

我想在FragmentActivity中的bindView方法的匿名内部类中获取CursorAdapter引用。实际上,我想在DialogFragment点击ImageView并与ListView相关联时尝试创建SimpleCursorAdapter

@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的构造函数,并在我的匿名内部类中使用它。这是我的Dialog片段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");
            }
        });
    }

3 个答案:

答案 0 :(得分:3)

由于您尝试获取FragmentManager的参考号,因此您可以对final内的FragmentActivity进行SimpleCursorAdapter引用,并将其传递给SimpleCursorAdapter 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");

{{1}}

答案 1 :(得分:1)

您可以在构造函数中获取Activity上下文。只需将其保存在元素类中:

Context context;
public myCursorAdapter(Context context, Cursor c) {
this.context=context;
...
}

答案 2 :(得分:0)

您可以使用传递给您的上下文。传递给bindView方法的上下文与您创建SimpleCursorAdapter时传递的上下文相同。如果你需要在一个匿名的内部类中使用它,而不是让它成为最终的。方法调用或方法内的辅助变量。例如:

@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);
      ...
    }
  });
}