0

我使用 ArrayAdapter 创建了带有单选按钮的单选 AlertDialog。选择比率时是否可以关闭该警报对话框?我在 ArrayAdapter 类 holder.name.setOnClickListener 中有 OnClick 侦听器,但我不知道该怎么做。

警报对话框代码:

Builder builder = new Builder(serveris, useris, paswordas, BuildBuildingsViewActivity.this, USER_AGENT);

                        ArrayList<AvailableBuildings> availableBuildings = builder.checkForPossibleBuildings(pastatas.getBuildingLink());

                        ArrayAdapter<AvailableBuildings> adapter = new AvailableBuildingsAdapter(BuildBuildingsViewActivity.this, R.layout.choice_main, availableBuildings, host, curdid, pastatas.getBuildingLink());

                        new AlertDialog.Builder(BuildBuildingsViewActivity.this)
                        .setSingleChoiceItems(adapter, 0, null)
                        .setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                dialog.dismiss();
                            }
                        })
                        .show();

阵列适配器代码:

public class AvailableBuildingsAdapter extends ArrayAdapter<AvailableBuildings> {

    public DBAdapterBuild db_build;

        public AvailableBuildingsAdapter(Context context, int textViewResourceId,ArrayList<AvailableBuildings> availableBuildings, String host, String curdid, String aiksteles_link) {
            super(context, textViewResourceId, availableBuildings);
            this.context = context;
            this.availableBuildings = new ArrayList<AvailableBuildings>();
            this.availableBuildings.addAll(availableBuildings);
            this.host = host;
            this.curdid = curdid;
            this.aiksteles_link = aiksteles_link;
            db_build = new DBAdapterBuild(context, host);
        }

        private String host;
        private Context context;
        private String curdid;
        private String aiksteles_link;
        private ArrayList<AvailableBuildings> availableBuildings;

        static class ViewHolder {
           RadioButton name;
        }

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

            ViewHolder holder;


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

                holder = new ViewHolder();
                holder.name = (RadioButton) convertView.findViewById(R.id.building_name);
                convertView.setTag( holder );
                holder.name.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        db_build.open();
                        RadioButton name = (RadioButton) v;
                        AvailableBuildings building = (AvailableBuildings) name.getTag();
                        Log.e( "Available: ", ""+building.getAvailable_title()+building.getAvailable_code());
                        Log.e( "Available: ", ""+db_build.insertTitle(1, building.getAvailable_title(), aiksteles_link, curdid, 0, 1, building.getAvailable_type(), "3", building.getAvailable_code()));
                        db_build.close();


                    }
                });
            }  else {
                // view already defined, retrieve view holder
                holder = (ViewHolder) convertView.getTag();
            }

            AvailableBuildings building = availableBuildings.get(position);

            if ( building == null ) {

            }
            holder.name.setText(building.getAvailable_title());
            holder.name.setTag(building);

            return convertView;
        }
    }
4

1 回答 1

0

我解决了传递给 ArrayAdapter 构造函数 Dialog 对象的这个问题,接下来这样做:

holder.name.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        db_build.open();
                        RadioButton name = (RadioButton) v;
                        AvailableBuildings building = (AvailableBuildings) name.getTag();
                        Log.e( "Available: ", ""+building.getAvailable_title()+building.getAvailable_code());
                        Log.e( "Available: ", ""+db_build.insertTitle(1, building.getAvailable_title(), aiksteles_link, curdid, 0, 1, building.getAvailable_type(), "3", building.getAvailable_code()));
                        db_build.close();
                        dia.dismiss();
                    }
                });
于 2013-02-10T14:08:09.247 回答