弹出窗口中的ListView

时间:2017-02-25 16:18:48

标签: android listview popup

我正在创建一个应用程序并为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);

现在我想点击一个按钮,弹出一个相同的列表,任何帮助吗?

4 个答案:

答案 0 :(得分:11)

您可以执行以下操作:

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>

答案 1 :(得分:2)

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

答案 2 :(得分:1)

您可以使用自定义对话框

自定义对话框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();

答案 3 :(得分:0)

我在google中键入“PopupWindow”并找到了:

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

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

我希望我能帮忙!