2013-02-22 63 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