2

我的AlertDialog包含一个ListView. 当AlertDialog被解雇时,它又回来了ListView被呈现两次。我不知道为什么会这样。这是我的代码:

protected Dialog onCreateDialog(int id) {
        AlertDialog.Builder builder = new AlertDialog.Builder(LegendTest.this);
        final LayoutInflater inflater = (LayoutInflater) Legend`enter code here`Test.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.selectfeaturetype, null);
        ListView listView =  (ListView) view.findViewById(R.id.listView1);
        Dialog dialog;

        switch (id) {
        case LEGEND_DIALOG:

            builder.setTitle("Set legend");
            builder.setView(view);


            listView.setAdapter(new BaseAdapter() {

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

                    if(convertView == null)
                    {
                        convertView = inflater.inflate(R.layout.itemwithimage, null);
                    }
                    MyLegend legendItem = data.get(position);
                    if(legendItem != null)
                    {
                        ImageView img = (ImageView) convertView.findViewById(R.id.listimageView);
                        img.setImageBitmap(legendItem.legendImage);
                        TextView txt = (TextView) convertView.findViewById(R.id.listtextView);
                        txt.setText(legendItem.legendname);
                    }

                    return convertView;
                }

                @Override
                public long getItemId(int position) {

                    return position;
                }

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

                @Override
                public int getCount() {

                    if (data == null)
                        return 0;

                    return data.size();
                }
            });

            dialog = builder.create();
            break;

        default:

            dialog = null;
            break;
        }
        return dialog.
4

1 回答 1

0

试试下面的代码

package at.bartinger.list;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class PracticeClas extends Activity {

    ArrayList<String> data = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        data.add("Hello");
        data.add("Hello One");
        data.add("Hello Two");
        data.add("Hello Three");
        data.add("Hello Four");
        data.add("Hello Five");
        data.add("Hello Six");

        showDialog(0);
    }

    protected Dialog onCreateDialog(int id) {           
        final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.main, null);
        ListView listView = (ListView) view.findViewById(R.id.LsOne);
        Dialog dialog = null;

        dialog = new Dialog(PracticeClas.this);
        dialog.setContentView(view);
        dialog.setTitle("Material & Labor:");

        // dialog.create();
        switch (id) {
        case 0:
            listView.setAdapter(new BaseAdapter() {
                @Override
                public View getView(int position, View convertView,
                        ViewGroup parent) {

                    if (convertView == null) {
                        convertView = inflater.inflate(
                                R.layout.list_item_section, null);
                    }

                    TextView txt = (TextView) convertView
                            .findViewById(R.id.list_item_section_text);
                    txt.setText(data.get(position));
                    return convertView;
                }

                @Override
                public long getItemId(int position) {

                    return position;
                }

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

                @Override
                public int getCount() {

                    if (data == null)
                        return 0;

                    return data.size();
                }
            });

            dialog.show();
            break;

        default:
            dialog = null;
            break;
        }
        return dialog;
    }

}
于 2012-05-05T06:20:04.823 回答