我想要在CursorAdapter
中的bindView
方法的匿名內部類中獲得FragmentActivity
引用。其實我試圖創建一個DialogFragment
當ImageView
被點擊在我的ListView
,與SimpleCursorAdapter
連接。如何在bindView方法的匿名內部類中獲取FragmentActivity引用?
@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的構造,並用它在我的匿名內部class.That是我的對話片段顯示方法。現在它工作正常。我在下面更新了我的代碼。
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");
}
});
}
不要在列表項上設置單獨的'onClickListener's。爲ListView設置一個'OnItemClickListener'。您的活動可以實現OnItemClickListener。 – Karakuri 2013-02-22 15:46:10
他正試圖獲取點擊只是圖像,我假設有不同的邏輯,如果列表行被點擊。 – 2013-02-22 15:52:10