gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 08:05:24 26 4
gpt4 key购买 nike

我试图在 CursorAdapter 中的 bindView 方法的匿名内部类中获取 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 的构造函数,并在我的匿名内部类中使用它。这是我的 Dialog fragment 显示方法。现在它工作正常。我在下面更新了我的代码。

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

最佳答案

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

关于android - 如何在 bindView 方法的匿名内部类中获取 FragmentActivity 引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15027709/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com