Show list view in a popup window - Android

泄露秘密 提交于 2019-12-05 04:23:32

问题


I'm working on a project in Android and I have a problem. I have an activity which includes three buttons, edit text and a list view.

I want to change that implementation and to show the list view on a new popup window only when the user press the select all button. I've added my code, thanks.

public class Notepadv1 extends ListActivity implements OnClickListener {

    private WordsDbAdapter mDbHelper;
    private Button selectAllButton;
        private PopupWindow mPopup;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        selectAllButton = (Button)findViewById(R.id.selectAll);
        selectAllButton.setOnClickListener(this);

        mDbHelper = new WordsDbAdapter(this);
        mDbHelper.open();
        fillData();
    }

    public void onClick(View v) {
        switch(v.getId()){
            case(R.id.selectAll):
                selectAll();
                break;
        }
    }

    private void selectAll(){
    }

    private void fillData() {

        Cursor c = mDbHelper.fetchAllNotes();
        startManagingCursor(c);

        String[] from = new String[] { WordsDbAdapter.KEY_WORD };
        int[] to = new int[] { R.id.text1 };


        SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.words_row, c, from, to);
        setListAdapter(notes);
    }

}

回答1:


Yes you can do it.

1st way: define an activity as Dialog with the below attribute in AndroidManifest.xml file:

<activity android:theme="@android:style/Theme.Dialog" />

2nd way: You can inflate the XML layout inside the dialog as below:

Dialog dialog = new Dialog(context);
LayoutInflater li = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.my_layout, null, false);
dialog.setContentView(v);
dialog.show();

for example:

edit: link fixed

Android Dialog with ListView.




回答2:


Show a simple Alert Dialog with a list:

final CharSequence[] items = {"Red", "Green", "Blue"};

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {

   public void onClick(DialogInterface dialog, int item) {
        Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
   }

});

AlertDialog alert = builder.create();

alert.show();



回答3:


If using a custom ArrayAdapter, use setAdapter():

AlertDialog.Builder builder = new Builder(this)
    .setTitle("Dialog Title")
    .setAdapter(new CustomAdapter(context, items, ...), (dialog, itemPosition) -> {
      // Handle item click
    });
builder.show();


来源:https://stackoverflow.com/questions/9465506/show-list-view-in-a-popup-window-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!