2

AlertDialogには が含まれていListViewます。が閉じAlertDialogられ、戻ってくると、ListViewが 2 回表示されます。なぜこれが起こるのかわかりません。これが私のコードです:

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 に答える