0

我有一个 PopupWindow,我想从操作栏中的菜单项中调用它。PopupWindow 类是,

public class speciesPopupWindow {
Context ctx;
Button btnDismiss, ...;

public speciesPopupWindow(Context ctx){
    this.ctx = ctx;
}
public void init(View v){ 
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService...
View popUpView = inflater.inflate(R.layout.popup_layout,...
final PopupWindow popup = new PopupWindow(popUpView,...
popup.setContentView(popUpView);
popup.showAtLocation(v, Gravity.CENTER_HORIZONTAL, -10, 100);
...
btnSaveRecord = (Button) popUpView.findViewById(R.id.btnSaveRecordxml);
btnSaveRecord.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){saveRecord(v);
        }});
...
    }
public void saveRecord(View v){
    String szSpecies = edSpeciesLookup.getText().toString();            
    if(szSpecies.matches("")){          
    }else{
        db.execSQL("INSERT INTO speciesLookupDb (species) VALUES ('"+szSpecies+"')");
        clearForm(v);
    }
  }
//...more delete, update, first, previous, next, and last sql calls.
}

MainActivity 类是,

public class MainActivity extends Activity{
    public static Context appContext;
...
public speciesPopupWindow speciesPopupWindow;
    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
...
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.corax, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case R.id.menuitem_editSpeciesTable:
            editSpeciesTable(null);
            return true;
    }
    return false;
}
//2/13- Adjusted editSpeciesTable below per suggested answer.
public void editSpeciesTable(View v){
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    PopupWindow speciesPopupWindow = new PopupWindow(inflater.inflate(R.layout.popup_layout, null, false),500,500,true);
    speciesPopupWindow.showAtLocation(this.findViewById(R.id.fragment_placeholder), Gravity.CENTER, 0, 0);
//shows popup_layout.xml layout fine...but how to call speciesPopupWindow.init() for sql logic?
}
}

物种弹出窗口截图:

在此处输入图像描述

当前的问题与在speciesPopupWindow中实现SQL和其他方法有关......如何从MainActivity中onOptionsItemSelected中的case-switch语句调用这些?

提前致谢。

4

1 回答 1

0

将 MainActivity 中的 editSpeciesTable 更改为...

public void editSpeciesTable(){
    new speciesPopupWindow(MainActivity.this).init();
}

导致 speciesPopupWindow.class 正常工作。谢谢。

于 2015-02-16T07:00:49.117 回答