Android 点击按钮弹出ListView对话框

在主Activity中

public class ZHGLActivity extends AppCompatActivity {

private List list = new ArrayList();
private AlertDialog.Builder builder;
private AlertDialog alertDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.zhglactivity);
    init();
    list = initData();
 btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                ShowDialog();//弹框操作
            }
        });
    }
    public void init(){
        return1=(ImageButton) findViewById(R.id.return1);
        nc=(Button) findViewById(R.id.nc);
        btn = (Button) findViewById(R.id.btn_list);
    }
    //初始化数据
    private ArrayList initData() {
        ArrayList list = new ArrayList();
        String name1="修改头像";
        String name2="拍照上传";
        String name3="本地上传";
        list.add(name1);
        list.add(name2);
        list.add(name3);
        return list;
    }
    public void ShowDialog() {
        Context context = ZHGLActivity.this;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.list_tx_picture, null);
        ListView myListView = (ListView) layout.findViewById(R.id.formcustomspinner_list);
        MyAdapter adapter = new MyAdapter(context, list);
        myListView.setAdapter(adapter);
        myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView arg0, View arg1, int positon, long id) {
                //在这里面就是执行点击后要进行的操作,这里只是做一个显示
                if (alertDialog != null) {
                    alertDialog.dismiss();
                }
            }
        });
        builder = new AlertDialog.Builder(context);
        builder.setView(layout);
        alertDialog = builder.create();
        alertDialog.show();
    }
    //自定义的适配器
    class MyAdapter extends BaseAdapter {
        private List mlist;
        private Context mContext;

        public MyAdapter(Context context, List list) {
            this.mContext = context;
            mlist = new ArrayList();
            this.mlist = list;
        }

        @Override
        public int getCount() {
            return mlist.size();
        }

        @Override
        public Object getItem(int position) {
            return mlist.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Person person = null;
            if (convertView == null) {
                LayoutInflater inflater = LayoutInflater.from(mContext);
                convertView = inflater.inflate(R.layout.list_tx_item,null);
                person = new Person();
                person.name = (TextView)convertView.findViewById(R.id.txtext);
                convertView.setTag(person);
            }else{
                person = (Person)convertView.getTag();
            }
            person.name.setText(list.get(position).toString());
            return convertView;
        }
        class Person{
            TextView name;
        }
    }
}
 
  
在list_tx_picture布局文件中,特别注意最外面这个容器的id,android:id="@+id/layout_myview"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_myview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <ListView
        android:id="@+id/formcustomspinner_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:cacheColorHint="@null"
        android:divider="@android:drawable/divider_horizontal_bright"
        android:scrollbars="none" /> 

LinearLayout>

在list_tx_item布局文件中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/txtext"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:textSize="20dp"
        android:gravity="center"
        android:layout_marginLeft="10dp"
        />
LinearLayout>
效果图:

Android 点击按钮弹出ListView对话框_第1张图片

你可能感兴趣的:(Android 点击按钮弹出ListView对话框)