javaandroidlistviewandroid-listviewandroid-alertdialog

How to get onItemclick from listview in a AlertDialog


I have an Activity with three Identical Buttons and three Identical TextViews. Each button onClick invokes a AlertDialog by following code:

    PickerDialog = new PickerDialogFragment();
    PickerDialog.show(getSupportFragmentManager(), "PickerDialog");

My AlertDialog has an EditText with addTextChangedListener

searchText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            adapter.getFilter().filter(s);
            adapter.notifyDataSetChanged();
        }
        @Override
        public void afterTextChanged(Editable s) {
        }
    });

and the listview adapter extends cursoradapter and implements filterable

listView.setTextFilterEnabled(true);
    adapter.setFilterQueryProvider(new FilterQueryProvider() {
        @Override
        public Cursor runQuery(CharSequence constraint) {
            return dbHelper.filterCodes(db, constraint); //returns a cursor
        }
    });

What I am trying to achieve is to directly change button text on user selected value from Listview inside AlertDialog.

Apart from this If possible I would like to set the value of edittext with user selected value from filtered list and then on clicking of positive button I would the values of each button

So my question is what goes inside listview.onItemClickListener and builder.setPositiveButton and builder.setNegativeButton

I am also not able to identify which button invoked the AlertDialog and making different alertdialog for each button will only increase lines of code


Solution

  • Add an 'int resid' parameter to the constructor of your class. Instantiate your dialog with 'new PickerDialogFragment(R.id.button1);'. Use resid in findViewById to get the button instance and set its text.