ArrayAdapter

ArrayAdapter数组适配器

  • 第一种
ListView lv = (ListView) findViewById(R.id.lv);
String[] object = new String[]{
                "小子",
                "孟子",
                "孔子"
        };
//参数1:上下文  this  参数2:布局文件     参数3:显示文字要显示的id  参数4:模型数组
lv.setAdapter(new ArrayAdapter(this, R.layout.listview,R.id.tv, object));
  • 第二种
        //集合中每个元素都包含ListView条目需要的所有数据,该案例中每个条目需要一个字符串和一个整形,所以使用一个map来封装这两种数据
        List> data = new ArrayList>();
        Mapmap1 = new HashMap();
        map1.put("photo", R.drawable.ic_launcher);
        map1.put("name", "孟子");
        data.add(map1);
        
        Mapmap2 = new HashMap();
        map2.put("photo", R.drawable.ic_launcher);
        map2.put("name", "孔子");
        data.add(map2);
        
        Mapmap3 = new HashMap();
        map3.put("photo", R.drawable.ic_launcher);
        map3.put("name", "小子");
        data.add(map3);
        
        lv.setAdapter(new SimpleAdapter(this, data, R.layout.listview, 
                new String[]{"photo","name"}, new int[]{R.id.imageView,R.id.tv}));

对话框

  • xml文件创建Button,绑定方法
public void click1(View v){
        AlertDialog.Builder builder = new Builder(this);
        builder.setIcon(android.R.drawable.alert_dark_frame);
        builder.setTitle("阿斯顿撒");
        builder.setMessage("asdasd");
        //设置确定按钮
        builder.setPositiveButton("确定", new OnClickListener() {
            
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                Toast.makeText(MainActivity.this, "练不成", 0).show();
            }
        });
        //设置取消按钮
        builder.setNegativeButton("取消", new OnClickListener() {
            
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                Toast.makeText(MainActivity.this, "练不成", 0).show();
            }
        });
        AlertDialog ad =  builder.create();
        ad.show();
    }

单选框

 public void click2(View v){
        AlertDialog.Builder builder = new Builder(this);
        builder.setTitle("请选择性别");
        final String[] items = new String[]{
            "女",
                "男"
        };
        builder.setSingleChoiceItems(items, -1, new OnClickListener() {            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                Toast.makeText(MainActivity.this, "您选择的是:"+ items[arg1], 0).show();
            }
        });
        builder.show();
    }

多选框

 public void click3(View v) {
        AlertDialog.Builder builder = new Builder(this);
        builder.setTitle("喀喀喀");
        String[] items = new String[]{
            "小明",
            "小刘",
            "小黄",
            "asd"
        };
        final boolean[] checkedItems = new boolean[]{
                true,
                true,
                false,
                false
        };
        builder.setMultiChoiceItems(items, checkedItems, new OnMultiChoiceClickListener() {
            //arg1:用户点击的条目的下标
            //arg2:用户是选中该条目还是取消该条目
            @Override
            public void onClick(DialogInterface arg0, int arg1, boolean arg2) {
                checkedItems[arg1] = arg2;
            }
        });
        builder.show();

你可能感兴趣的:(ArrayAdapter)