2014-12-18 61 views
1

我有一個popupWindow,用於修改在父窗口中加載微調器的SQLight表。我想讓父窗口中的微調框在刷新PopupWindow時用新值刷新。下面的代碼顯示了我的進度創建一個偵聽器,可以檢測PopupWindow的解僱。我迄今爲止的聽衆不工作。我認爲我已經錯過了構建聽衆的東西。我已經包含ShowPopup類以及作爲此窗口的父級的片段(Tab3Fragment)。在PopupWindow.dismiss上刷新父級片段

showPopup.java

public class showPopup extends PopupWindow{ 
Context m_context; 
Button btnDismiss; 
PopupWindow popup; 
Tab3Fragment Tab3Fragment; 
OnDismissListener listener; 

public showPopup(Context context){ 
    super(context); 
    m_context = context;//was commented out 

    setContentView(LayoutInflater.from(context).inflate(R.layout.popup_layout, null)); 
    setHeight(LayoutParams.WRAP_CONTENT); 
    setWidth(LayoutParams.WRAP_CONTENT); 
} 
public void init(View v){ 
    LayoutInflater inflater = (LayoutInflater)m_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View popUpView = inflater.inflate(R.layout.popup_layout, null, false); 
    final PopupWindow popup = new PopupWindow(popUpView, 600, 400, true); 

    popup.setContentView(popUpView); 
    popup.showAtLocation(v, Gravity.CENTER_HORIZONTAL, 0, 0); 

    btnDismiss = (Button) popUpView.findViewById(R.id.btndismissxml); 
    btnDismiss.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View anchor) { 
      popup.dismiss(); 
     } 
    }); 
} 
@Override 
public void setOnDismissListener(OnDismissListener listener){ 
    this.listener = listener; 
    } 
} 

Tab3Fragment.java

public class Tab3Fragment extends Fragment implements OnClickListener{ 
Context context; 
Button btnPopup, btnSpinnerRefresh; 
Spinner spinnerSpecies; 
public static int iSpeciesPosition; 
showPopup showPopup; 
ArrayAdapter<String> arrayAdapterSpecies; 
OnDismissListener dismissListener; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup containerGroup, Bundle savedInstanceState) {  
    View v = inflater.inflate(R.layout.tab3_fragment, containerGroup, false);  

    btnPopup = (Button)v.findViewById(R.id.btnPopupxml); 
    btnPopup.setOnClickListener(this); 

    btnSpinnerRefresh = (Button)v.findViewById(R.id.btnSpinnerRefreshxml); 
    btnSpinnerRefresh.setOnClickListener(this); 

    spinnerSpecies = (Spinner) v.findViewById(R.id.spinnerSpeciesxml); 
    spinnerSpecies.setAdapter(arrayAdapterSpecies); 

    if(savedInstanceState != null){ 
     iSpeciesPosition = savedInstanceState.getInt("speciesPosition_key"); 
     populateTab3Fragment(v); 
    }else if(savedInstanceState == null){ 
     populateTab3Fragment(v); 
    } 
    return v;  
} 
//@Override 
public void onViewCreated(View v) { 
    populateTab3Fragment(v); 
    /****************************************************************************************************** 
    Can setOnDismissListener be used outside of showPopup class to indicate that showPopup has dismissed? 
    ******************************************************************************************************/ 
    showPopup popup = new showPopup(context); 
    popup.setOnDismissListener(new OnDismissListener(){ 

     @Override 
     public void onDismiss(){ 
      Toast.makeText(getActivity().getApplicationContext(), "onDismiss() works.", Toast.LENGTH_LONG).show(); 
      loadSpinnerData(); 
     } 
    });   
} 
@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.btnPopupxml: 
     new showPopup(getActivity().getApplicationContext()).init(v); 
     break; 
    case R.id.btnSpinnerRefreshxml: 
     loadSpinnerData();//temporary workaround to refresh spinner... 
     break; 
    } 
} 
/** 
* Function to load the spinner data from SQLite database 
* */ 
public void loadSpinnerData() { 
    //omitted 
} 
public void populateTab3Fragment(View v){ 
    loadSpinnerData();    
    } 
} 
+0

好的 - 我已經廣泛修改showPopup類並刪除了無關的語言。是否有可能在正在被解散的popupWindow類之外實現onDismissListener()?我在這裏錯過了什麼? TIA顯示耐心並提供有用的反饋。 – portsample 2015-01-12 04:16:33

回答

2

從來沒有達到可以直接在子PopupWindow關閉時刷新父窗口的程度。最終的解決方案(解決方法)是僅當已設置指示已修改的SQL快速查找表的靜態標誌(iSpeciesRefresh)時,才從微調控件中的onTouch事件之後使用fragmentmanager replace()。

public class dataCapture extends Fragment implements OnClickListener { 
String szSpecies; 
static public int iSpeciesRefresh = 1; 
Spinner spinnerSpecies; 
... 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.data_capture, container, false); 
... 
    spinnerSpecies = (Spinner) v.findViewById(R.id.spinnerSpeciesxml); 

    spinnerSpecies.setOnTouchListener(new View.OnTouchListener() {//refreshes fragment as needed... 
     @Override 
     public boolean onTouch(View view, MotionEvent motionEvent) { 
      if (iSpeciesRefresh == 1) {//this is application default 
       //do nothing 
      } else if (iSpeciesRefresh == 0) {//value is reset to "0" at SAVE, UPDATE, or DELETE in speciesPopupWindow. 
       refreshDataCapture(); 
       iSpeciesRefresh = 1; 
      } 
      return false; 
     } 
    }); 
... 
}  
public void refreshDataCapture() { 
    Fragment currentFragment = (dataCapture) getFragmentManager().findFragmentByTag("data_capture"); 
    if (currentFragment == null) { 
     currentFragment = new dataCapture(); 
    } else if (currentFragment != null) { 
     getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); 
     getFragmentManager().beginTransaction().replace(R.id.fragment_placeholder, new dataCapture(), "data_capture").addToBackStack(null).commit();  
    } 
} 
1

在你onCreateViewonViewCreated方法,你做btnPopup.setOnClickListener(this);,但隨後在showPopup類init()方法,你覆蓋你的片段作爲監聽器,而是分配一個新的匿名監聽器。我的猜測是,你需要重新分配你的監聽器,並確保你不會像這樣覆蓋它。

P.S.出於可維護性的考慮(以及查看代碼的其他開發人員的理智),約定以上駱駝大小寫來命名類,而且它們不應包含動詞。像MyCustomPopup會更好。