0

How can i set selected value from alert dialog (single selection) on my list view item?... My scenario is: - I have listview item with button and textview - Clicking on button in my listview item , which opens dialog box - Selecting value from single selection list in dialog box - Want to show this selected value from dialog in textview inside my listview item

here is my code: Problem is: the value changed inside listitem view by setting in dialog box, doesnt persist on scroll up and down.

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 Answers1

0

I have achieved the same thing with this code

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

}
Ali Hassan
  • 188
  • 4
  • 8