【问题标题】:Show list view in a popup window - Android在弹出窗口中显示列表视图 - Android
【发布时间】:2012-02-27 13:05:44
【问题描述】:

我正在开发一个 Android 项目,但遇到了问题。 我有一个活动,其中包括三个按钮、编辑文本和一个列表视图。

我想更改该实现并仅在用户按下全选按钮时在新的弹出窗口上显示列表视图。 我已经添加了我的代码,谢谢。

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);
    }

}

【问题讨论】:

    标签: android listview popupwindow


    【解决方案1】:

    显示一个带有列表的简单警报对话框:

    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();
    

    【讨论】:

      【解决方案2】:

      是的,你可以做到。

      第一种方式: 在 AndroidManifest.xml 文件中使用以下属性将 Activity 定义为 Dialog:

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

      第二种方式: 您可以在对话框中扩展 XML 布局,如下所示:

      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();
      

      例如:

      编辑:链接已修复

      Android Dialog with ListView.

      【讨论】:

      • 感谢您的回答。另一个问题,我是否需要将它分成两个活动,一个用于按钮、编辑文本等,一个用于对话框?
      • 是的,如果您想使用 Activity 作为对话框,那么您可以定义 2 个单独的 Activity,如果您只想使用上面提到的第二种方式,则不需要另一个 Activity。
      • @OfiRAN 检查上面提到的示例链接。
      • 它的作品,但它也显示一些空间,如标题栏。所以我怎样才能删除这个空间
      【解决方案3】:

      如果使用自定义 ArrayAdapter,请使用 setAdapter():

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

      【讨论】:

        猜你喜欢
        • 2013-01-06
        • 1970-01-01
        • 1970-01-01
        • 2015-07-16
        • 1970-01-01
        • 2019-11-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多