java - 如何在android中的对话框中显示 ListView ?

标签 java android listview android-listview dialog

我有一个activitymain.xml,当我单击显示按钮时,它应该在弹出窗口中显示列表。按钮就是调用这个方法。

public void mergecheck(View v)
    {
        Dialog dialog = new Dialog(Table_layoutnew.this);
        dialog.setContentView(R.layout.mergetablepopup);

        ListView listView = (ListView) findViewById(R.id.tablelist);



        ArrayList<Mergetablelist> countryList = new ArrayList<Mergetablelist>();
        Mergetablelist country = new Mergetablelist("AFG","Table 100",false);
        countryList.add(country);
        country = new Mergetablelist("ALB","Tabel 101",true);
        countryList.add(country);
        country = new Mergetablelist("DZA","Tabel 102",false);
        countryList.add(country);
        country = new Mergetablelist("ASM","Tabel 103",true);
        countryList.add(country);
        country = new Mergetablelist("AND","Tabel 104",true);
        countryList.add(country);
        country = new Mergetablelist("AGO","Tabel 105",false);
        countryList.add(country);
        country = new Mergetablelist("AIA","Tabel 106",false);
        countryList.add(country);
        country = new Mergetablelist("AIA","Tabel 107",false);
        countryList.add(country);
        country = new Mergetablelist("AIA","Tabel 108",false);
        countryList.add(country);
        country = new Mergetablelist("AIA","Tabel 109",false);
        countryList.add(country);
        country = new Mergetablelist("AIA","Tabel 110",false);
        countryList.add(country);

        //create an ArrayAdaptar from the String Array


        dataAdapter = new MyCustomAdapter(this,
                R.layout.mergetablelist_elements, countryList);
        //ListView listView = (ListView) findViewById(R.id.tablelist);
        // Assign adapter to ListView
        listView.setAdapter(dataAdapter);


        dialog.setCancelable(true);
        dialog.show();
    }

此 ListView 的适配器如下。

private class MyCustomAdapter extends ArrayAdapter<Mergetablelist> {

        private ArrayList<Mergetablelist> countryList;

        public MyCustomAdapter(Context context, int textViewResourceId, 
                ArrayList<Mergetablelist> countryList) {
            super(context, textViewResourceId, countryList);
            this.countryList = new ArrayList<Mergetablelist>();
            this.countryList.addAll(countryList);
        }

        private class ViewHolder {
            TextView code;
            CheckBox name;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder = null;
            Log.v("ConvertView", String.valueOf(position));

            if (convertView == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(
                        Context.LAYOUT_INFLATER_SERVICE);
                convertView = vi.inflate(R.layout.mergetablelist_elements, null);

                holder = new ViewHolder();
                holder.code = (TextView) convertView.findViewById(R.id.code);
                holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
                convertView.setTag(holder);

                holder.name.setOnClickListener( new View.OnClickListener() {  
                    public void onClick(View v) {  
                        CheckBox cb = (CheckBox) v ;  
                        Mergetablelist country = (Mergetablelist) cb.getTag();  
                        Toast.makeText(getApplicationContext(),
                                "Clicked on Checkbox: " + cb.getText() +
                                " is " + cb.isChecked(), 
                                Toast.LENGTH_LONG).show();
                        country.setSelected(cb.isChecked());
                    }  
                });  
            } 
            else {
                holder = (ViewHolder) convertView.getTag();
            }

            Mergetablelist country = countryList.get(position);
            holder.code.setText(" (" +  country.getCode() + ")");
            holder.name.setText(country.getName());
            holder.name.setChecked(country.isSelected());
            holder.name.setTag(country);

            return convertView;

        }

    }

此代码在设置适配器元素时显示错误。请解决这个问题。我想在对话框中显示带有复选框的 ListView ,并从 ListView 中获取所有选中的值。

最佳答案

试试这个:

public void mergecheck(View v){
    View dialog_view = View.inflate(this, R.layout.mergetablepopup, null);

    ListView listView = (ListView)dialog_view.findViewById(R.id.tablelist);

    ArrayList<Mergetablelist> countryList = new ArrayList<Mergetablelist>();
    Mergetablelist country = new Mergetablelist("AFG","Table 100",false);
    countryList.add(country);
    country = new Mergetablelist("ALB","Tabel 101",true);
    countryList.add(country);
    country = new Mergetablelist("DZA","Tabel 102",false);
    countryList.add(country);
    country = new Mergetablelist("ASM","Tabel 103",true);
    countryList.add(country);
    country = new Mergetablelist("AND","Tabel 104",true);
    countryList.add(country);
    country = new Mergetablelist("AGO","Tabel 105",false);
    countryList.add(country);
    country = new Mergetablelist("AIA","Tabel 106",false);
    countryList.add(country);
    country = new Mergetablelist("AIA","Tabel 107",false);
    countryList.add(country);
    country = new Mergetablelist("AIA","Tabel 108",false);
    countryList.add(country);
    country = new Mergetablelist("AIA","Tabel 109",false);
    countryList.add(country);
    country = new Mergetablelist("AIA","Tabel 110",false);
    countryList.add(country);

    //create an ArrayAdaptar from the String Array


    dataAdapter = new MyCustomAdapter(this,
            R.layout.mergetablelist_elements, countryList);
    //ListView listView = (ListView) findViewById(R.id.tablelist);
    // Assign adapter to ListView
    listView.setAdapter(dataAdapter);

    new AlertDialog.Builder(this)
            .setView(dialog_view)
            .show();
}

关于java - 如何在android中的对话框中显示 ListView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24629508/

相关文章:

java - 如何在运行时创建 json 数组并向特定数组添加元素?

java - JBoss 服务器上的 Cobertura 运行时覆盖范围

java - 反射调用空 arg 构造函数的编译器警告

android - StrictMode 策略违规 : android. os.StrictMode$StrictModeDiskReadViolation : policy=327711 violation=2 at Application super. onCreate

listview - 如何在 Flutter 中使用 ListView 修复 'Bottom Overflowed by XXX pixels'

listview - 执行notifyDataSetChanged时ListView出现ClassCastException

java - 向现有 PDF 添加页脚

android - 如何从服务转到主屏幕?

android - 给定一个 tensorflow 模型图,如何找到输入节点和输出节点名称

java - Android:无法获得可点击的 ListView 页眉/页脚