0

如何在我的列表视图项上设置从警报对话框(单选)中选择的值?...我的场景是:-我有带有按钮和文本视图的列表视图项-单击我的列表视图项中的按钮,打开对话框-选择对话框中单个选择列表中的值 - 想要在我的列表视图项内的文本视图中的对话框中显示此选定值

这是我的代码: 问题是:通过在对话框中设置更改了 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;
    }
4

1 回答 1

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

}
于 2021-07-30T11:43:16.620 回答