弹出窗口中的列表视图

8

我正在开发一个应用程序,使用自定义适配器来管理 ListView。该应用在活动界面中运行。

public class Adapter extends ArrayAdapter {
    Context mContext;
    int resourceID;
    ArrayList<String> names;
    public Adapter(Context context, int resource, ArrayList<String> objects) {
        super(context, resource, objects);
        this.mContext = context;
        this.resourceID=resource;
        this.names= objects;
    }

    @Override
    public String getItem(int position) {
        return names.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        LayoutInflater inflater = LayoutInflater.from(mContext);
        row = inflater.inflate(resourceID, parent, false);

        TextView text = (TextView) row.findViewById(R.id.text);

        text.setText(names.get(position));
        return row;
    }

}

我使用了这段代码来让它们在一个活动中显示。
    myNames= (ListView) findViewById(R.id.List);
    adapter = new Adapter(this,R.layout.names_view, Current.Names);
    myNames.setAdapter(adapter);

现在我想点击一个按钮,使得相同的列表出现在弹出窗口中,请帮忙吗?


弹出窗口是AlertDialog,你是这个意思吗? - the_dani
我是指这个 PopupWindow 类尝试了多种方式,但都没有成功。 - Esraa
4个回答

15

以下是您可以采取的步骤:

1) 单击按钮时创建自定义对话框:

Button clickButton = (Button) findViewById(R.id.clickButton);
clickButton.setOnClickListener( new OnClickListener() {

        @Override
        public void onClick(View v) {

            final Dialog dialog = new Dialog(YourActivity.this);
            dialog.setContentView(R.layout.custom_dialog);
            dialog.setTitle("Title...");
            myNames= (ListView) dialog.findViewById(R.id.List);
            adapter = new Adapter(YourActivity.this,R.layout.names_view, Current.Names);
            myNames.setAdapter(adapter);
            dialog.show();

        }
    });

2)在对话框布局(custom_dialog.xml)中添加列表视图(listview):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical">

   <ListView
      android:id="@+id/List"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" >
   </ListView>

</LinearLayout>

非常感谢,它起作用了,但我使用了“Activity.this”而不是“this”,并在最后调用了dialog.show()。 - Esraa
抱歉之前的回答有误,我已经进行了更正。如果对您有帮助,请标记为已接受的答案。 - tahsinRupam
完成 :),但请更改其他的东西 - Esraa

4

custom.xml

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

</ListView> 

活动

String names[] ={"A","B","C","D"};
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
                LayoutInflater inflater = getLayoutInflater();
                View convertView = (View) inflater.inflate(R.layout.custom, null);
                alertDialog.setView(convertView);
                alertDialog.setTitle("List");
                ListView lv = (ListView) convertView.findViewById(R.id.List);
                Adapter<String> adapter = new Adapter(this,R.layout.names_view, Current.Names);
                lv.setAdapter(adapter);
                alertDialog.show();

谢谢,它完美地运行了。但是为了在单击按钮时使用它,我将“Adapter<String> adapter = new Adapter(this,R.layout.names_view, Current.Names);”中的“this”替换为“MainActivity.this”。 - Esraa
欢迎...很高兴能帮助@E.Khaled - mahiraj2709

2
您可以使用自定义对话框。
自定义对话框需要使用mydialog.xml文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView
    android:id="@+id/lv"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"/>
</LinearLayout>

在你的活动中
Dialog dialog = new Dialog(Activity.this);
   dialog.setContentView(R.layout.mydialog)
ListView lv = (ListView ) dialog.findViewById(R.id.lv);
dialog.setCancelable(true);
dialog.setTitle("ListView");
dialog.show();

dialog.setCancelable(true);是什么意思? - Esraa
用户只能通过按对话框按钮使其消失,而不能通过按返回键或轻触屏幕来关闭对话框。 - Shahbaz Ansari

1

我在谷歌上输入了“PopupWindow”,找到了以下内容:

https://www.youtube.com/watch?v=fn5OlqQuOCk

直到现在我才知道PopupWindow,我建议使用对话框,例如带有自定义视图的AlertDialog。

希望我的回答能够帮到你!


非常感谢您的帮助尝试,我使用了Dialog,它真的很有效。 - Esraa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接