2

我试图FragmentActivitybindView. CursorAdapter实际上,我正在尝试在我的 中单击DialogFragmentan 时创建一个,与.ImageViewListViewSimpleCursorAdapter

@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 的构造函数,并在我的匿名内部类中使用它。这就是我的对话框片段显示方法。现在它工作正常。我在下面更新了我的代码。

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,因此您可以在您的内部保存final对您的引用,并将其传递给您的构造函数。FragmentActivitySimpleCursorAdapterSimpleCursorAdapter

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. 如果您需要在匿名内部类中使用它,而不仅仅是让它成为最终的。在方法调用或方法内的辅助变量上。例如:

@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 回答