如何在列表视图项目的警报对话框(单选)中设置选定值?

时间:2014-05-29 16:52:43

标签: android listview dialog android-viewholder

如何在列表视图项目的警告对话框(单选)中设置所选值?... 我的情况是: - 我有listview项目,包含按钮和textview - 单击列表视图项中的按钮,打开对话框 - 从对话框中的单个选择列表中选择值 - 想要在listview项目

中的textview中的对话框中显示此选定值

这是我的代码: 问题是:通过在对话框中设置,在listitem视图中更改了值,在向上和向下滚动时不会持续存在。

public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        db = new DatabaseHandler(getContext());
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.activity_event_item, parent, false);
            holder = new ViewHolder();
            holder.x = (TextView) convertView.findViewById(R.id.x);
            holder.y = (TextView) convertView.findViewById(R.id.y);
            holder.b = (Button) convertView.findViewById(R.id.b);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String s = x[position];
        if (s != null){
            holder.x.setText("setting text");
        }
        holder.b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                selected = 0;

                builder.setTitle("Select ").setSingleChoiceItems(array1, selected, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        selected = which;
                    }
                })
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        value = array1[selected];
                        holder.y.setText(value + " ");
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                    }
                });
                builder.create();
                builder.show();
            }

        });

        return convertView;
    }

1 个答案:

答案 0 :(得分:0)

我用这段代码实现了同样的目的

private void showDialoge(Context context, int postion) {
    String[] items = {"Unpaid", " Paid "};

    AlertDialog.Builder builder = new AlertDialog.Builder(context)
            .setSingleChoiceItems(items, postion, null)
            .setPositiveButton(R.string.ok_button_label, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.dismiss();
                    int selectedPosition = ((AlertDialog) dialog).getListView().getCheckedItemPosition();
                    // Do something useful withe the position of the selected radio button
                }
            });

    AlertDialog dialog = builder.create();
    dialog.show();

}