2014-07-25 52 views
-1

我想在popup菜單中顯示listview? 我建立initializeVars()的列表視圖 如何在我的showPopupMenu(視圖V)的iCall這種方法方法 告訴我這個計劃如何在彈出菜單中調用我自己的方法

public void initializeVars(){ 
    lv_sample = (ListView)findViewById(R.id.lv_sample); 
    adapter = new ArrayAdapter<String>(this, R.layout.setlanguage, R.id.tv_content, values); 
    if(adapter==null) 
     return; 
    lv_sample.setAdapter(adapter); 
} 

private void showPopupMenu(View v){ 
    PopupMenu popupMenu = new PopupMenu(MainActivity.this, v); 
    popupMenu.getMenuInflater().inflate(R.menu.main, popupMenu.getMenu()); 

    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 

    public boolean onMenuItemClick(MenuItem item) { 
     Toast.makeText(MainActivity.this, item.toString(), Toast.LENGTH_LONG).show(); 
     return true; 
    } 
}); 
popupMenu.show(); 

回答

0
<menu xmlns:android="http://schemas.android.com/apk/res/android" > 

<item 
    android:id="@+id/pop1" 
    android:orderInCategory="100" 
    android:showAsAction="never" 
    android:title="@string/hello_world"/> 
<item 
    android:id="@+id/pop2" 
    android:orderInCategory="100" 
    android:showAsAction="never" 
    android:title="@string/hello_world"/> 
<item 
    android:id="@+id/pop3" 
    android:orderInCategory="100" 
    android:showAsAction="never" 
    android:title="@string/hello_world"/> 

默認顯示與PopupMenu的膨脹將解該項目列表....

但是,如果你還想要顯示一個列表然後:

可以延長DialogFragment ...

public class DefaultViewDialog extends DialogFragment{ 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 

    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity()); 
    //following commented code produces check box list in the dialog 
    //dialogBuilder.setMultiChoiceItems(R.array.DefaultView_array, null, null); 

    /* setItems(id) creates a simple list */ 
    dialogBuider.setItems(R.array.DefaultView_array, null, null); 
    return dialogBuilder.create(); 

} 

}

希望我幫助:)

相關問題