【问题标题】:How to get FragmentActivity reference inside an anonymous inner class of bindView method?如何在 bindView 方法的匿名内部类中获取 FragmentActivity 引用?
【发布时间】:2013-02-22 15:36:39
【问题描述】:

我试图在CursorAdapterbindView 方法的匿名内部类中获取FragmentActivity 引用。实际上,我正在尝试在我的ListView 中单击ImageView 时创建一个DialogFragment,并与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 的构造函数,并在我的匿名内部类中使用它。这就是我的对话框片段显示方法。现在它工作正常。我在下面更新了我的代码。

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");
            }
        });
    }

【问题讨论】:

  • 不要在列表项上设置单独的onClickListeners。为ListView 设置OnItemClickListener。您的 Activity 可以改为实现 OnItemClickListener。
  • 他试图只为图像获得点击,我假设如果点击列表行会有不同的逻辑。

标签: android android-cursoradapter


【解决方案1】:

由于您尝试获取FragmentManager 的引用,因此您可以在SimpleCursorAdapter 中保存对FragmentActivityfinal 引用,并将其传递给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");

【讨论】:

  • 我认为这会起作用,您可以直接传递 FragmentManager 而不是 FragmentActivity。我在其他场景中也做过类似的事情。
【解决方案2】:

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

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

【讨论】:

  • 他正在尝试显示一个DialogFragment,这需要一个FragmentManager,您可以从使用CursorAdapterFragmentActivity 获得它。基本上,他不是在寻找所说的上下文。问题的措辞不清楚。
【解决方案3】:

您可以只使用传递给您的上下文。传递给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);
      ...
    }
  });
}

【讨论】:

  • 哦,很抱歉...您可以将上下文作为 show 方法的活动,但这听起来有点脏。另一种选择是让您的 ViewBinder 类需要一个活动上下文,例如:public MyBinder(Context activity) { this.mActivity = activity; },这可能是正确的做法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-05
  • 2018-10-30
  • 2012-02-21
相关资源
最近更新 更多