【问题标题】:how to show listview in a dialoge in android?如何在android的对话框中显示listview?
【发布时间】:2014-07-08 10:30:23
【问题描述】:

我有一个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();
    }

下面是这个列表视图的适配器。

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;

        }

    }

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

【问题讨论】:

  • 请向我们展示您的 logcat
  • 如果tablelistmergetablepopup 布局中,那么findViewById() 将为ListView 返回null。

标签: java android listview android-listview dialog


【解决方案1】:

试试这个:

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

【讨论】:

    【解决方案2】:

    如果您想在对话框中显示列表视图,请创建您自己的自定义对话框。

    This is my Gist you can check here.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-13
      • 1970-01-01
      相关资源
      最近更新 更多