2013-02-22 61 views
2

我想要在CursorAdapter中的bindView方法的匿名内部类中获得FragmentActivity引用。其实我试图创建一个DialogFragmentImageView被点击在我的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"); 
      } 
     }); 
    } 
+0

不要在列表项上设置单独的'onClickListener's。为ListView设置一个'OnItemClickListener'。您的活动可以实现OnItemClickListener。 – Karakuri 2013-02-22 15:46:10

+1

他正试图获取点击只是图像,我假设有不同的逻辑,如果列表行被点击。 – 2013-02-22 15:52:10

回答

3

既然你试图让一个裁判你FragmentManager,您能不能把一个final引用您的FragmentActivitySimpleCursorAdapter内,并通过它在你的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

我认为这会起作用,你可以直接传递FragmentManager而不是FragmentActivity。我在其他情况下做了类似的事情。 – 2013-02-22 16:25:17

+0

谢谢...请参阅我的更新。它工作正常。 – Ramprasad 2013-02-23 03:37:45

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

他在问show()方法。 – 2013-02-22 15:48:41

+0

噢,对此很抱歉...您可以将您的上下文作为show方法的活动,但听起来有点脏。另一种选择是让你的ViewBinder类需要一个活动上下文,如:public MyBinder(Context activity){this.mActivity = activity; }'这可能是正确的做法。 – ebarrenechea 2013-02-22 15:53:26

1

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

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

他正试图展示一个'DialogFragment',它需要一个'FragmentManager',你可以从使用'CursorAdapter'的'FragmentActivity'获得。基本上,他没有像上述那样寻找上下文。问题的措辞不清楚。 – 2013-02-22 16:12:20