1

I have a ListView populated by a custom ArrayAdapter. The structure of each row is composed by

  • ImageView
  • TextView
  • NumberPicker
  • ImageButton

Clicking on the ImageButton should show a popup window which contains a color slider and a "ACCEPT" button. Here is an image that should clarify the layout. enter image description here

What I would like to do is : by clicking the "ACCEPT" button contained in the popup window, I should retrieve the selected color, set it as background color of the ImageButton and dismiss the popupwindow. Here is the code :

        public View getView(final int position, View convertView, ViewGroup parent) {
                _row_view = convertView;
                db = new SofosDbDAO(this._ctx);
                if(_row_view==null){
                    // 1. Create inflater
                    _inflater = (LayoutInflater) _ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    _row_view =  _inflater.inflate(R.layout.riga_app,parent,false);
                }

                // 2. Inflate xml layout
                _row_view = _inflater.inflate(R.layout.riga_app, parent, false);

                // 3. Initialize child views
                _iconaapp = (ImageView)_row_view.findViewById(R.id.riga_app_iv);
                _nomeapp = (TextView)_row_view.findViewById(R.id.riga_app_tv);
                _numerovibrazioni = (NumberPicker)_row_view.findViewById(R.id.riga_app_np);
                _colorenotifica = (ImageButton)_row_view.findViewById(R.id.riga_app_ib);

                // 4. Set Values
                int iconid = _ctx.getResources().getIdentifier(_sofosapps.get(position).get_app_icon(), "drawable", _ctx.getPackageName());
                Drawable icon = _ctx.getResources().getDrawable(iconid);
                _iconaapp.setImageDrawable(icon);
                String appname = _sofosapps.get(position).get_app_name();
                _nomeapp.setText(appname);
                _numerovibrazioni.setMinValue(0);
                _numerovibrazioni.setMaxValue(5);
                _numerovibrazioni.setValue(_sofosapps.get(position).get_vibrations());

    //Update DB when number picker value gets changed
                _numerovibrazioni.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
                    @Override
                    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                        SofosApp app = _sofosapps.get(position);
                        app.set_vibrations(newVal);
                        db.openDb();
                        db.updateAppVibrations(app);
                        db.closeDb();
                        Log.d("DEBUG", "Updated nr of vibrations");
                    }
                });

                //Set initial ImageButton background color
    _colorenotifica.setBackgroundColor(_sofosapps.get(position).get_color());

    //Show popup window on click of ImageButton
                _colorenotifica.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        _popupcontainer = (ViewGroup)_inflater.inflate(R.layout.color_picker_popup,null);
                        _puw = new PopupWindow(_popupcontainer,800,600,true);//view,dimensioni e focusable
                        _btn_applica = (Button) _popupcontainer.findViewById(R.id.color_picker_btn_applica);
                        _tv_applica_colore = (TextView) _popupcontainer.findViewById(R.id.color_picker_tv);
                        _tv_applica_colore.setText(_sofosapps.get(position).get_app_name());
                        _lss = (LobsterShadeSlider)_popupcontainer.findViewById(R.id.color_picker_ls);
                        _puw.showAtLocation(_popupcontainer, Gravity.CENTER, 20, 50);
                        Log.d("DEBUG","I clicked on imagebutton and opened my popupwindow");
                    }
                });

//**********************************************************
*********************** CRUCIAL POINT **********************
************************************************************
//Click of accept button inside popupwindow
                _btn_applica.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        newcolor = _lss.getColor();
                        String dbg = "color = " + String.valueOf(newcolor);
                        Log.d("DEBUG", dbg);
                        _colorenotifica.setBackgroundColor(newcolor);
                        _puw.dismiss();
                        _colorenotifica.invalidate();
                        Log.d("DEBUG", "Cliked accept");
                    }
                });
                // 5. retrn rowView
                return _row_view;
            }

I realize that this approach is not correct as the background image of the imagebutton does not change. Also , for some reason, I fail to create a new popup window for each row : when I click on any image button , the same popup window appears with the textview text set to "Twitter" , the last row. What is the right way of doing this? Thank you for the help!

Aaron Ullal
  • 4,855
  • 8
  • 35
  • 63
  • please can you specify which part is not working? does it crash? just does nothing? – Carlos Borau Jan 12 '16 at 09:38
  • I think you are doing good , Are you facing an hang or crash in application using this approach ! ? – Kapil Rajput Jan 12 '16 at 09:43
  • Thanks for your comment! Anyway the button is not changing background. – Aaron Ullal Jan 12 '16 at 10:03
  • @CarlosBorau Please see edited question :) Thank you! – Aaron Ullal Jan 12 '16 at 10:23
  • you can pass a OnClickListner in your custom adaper and then call your onClick in your custom adapter in constructor. `_btn_applica.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { listener.onClick(); } });` – vijaypalod Jan 12 '16 at 11:08
  • what if you put your OnClickListener directly in your listview? there you can retrieve the corresponding child ID and make whatever change you need: yourlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { //Do stuff } – Carlos Borau Jan 12 '16 at 14:21
  • @CarlosBorau Thanks for the suggestion! But how would I access the individual child views of each row (image button,number picker etc) ? – Aaron Ullal Jan 13 '16 at 18:49
  • inside onItemClick you have a reference to the view (the row) so you can find the childs via view.findViewById – Carlos Borau Jan 15 '16 at 10:03

2 Answers2

1

You should change value of color in your array _sofosapps and then call notifydatasetchanged on adapter.

_colorenotifica.setTag(position);
 _colorenotifica.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                     int position = (int)v.getTag();
                    _popupcontainer = (ViewGroup)_inflater.inflate(R.layout.color_picker_popup,null);
                    _puw = new PopupWindow(_popupcontainer,800,600,true);//view,dimensioni e focusable
                    _btn_applica = (Button) _popupcontainer.findViewById(R.id.color_picker_btn_applica);
                    _tv_applica_colore = (TextView) _popupcontainer.findViewById(R.id.color_picker_tv);
                    _tv_applica_colore.setText(_sofosapps.get(position).get_app_name());
                    _lss = (LobsterShadeSlider)_popupcontainer.findViewById(R.id.color_picker_ls);
                    _puw.showAtLocation(_popupcontainer, Gravity.CENTER, 20, 50);
                    _btn_applica.setTag(position);
                    Log.d("DEBUG","I clicked on imagebutton and opened my popupwindow");
                }
            });
 _btn_applica.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    newcolor = _lss.getColor();
                    String dbg = "color = " + String.valueOf(newcolor);
                    Log.d("DEBUG", dbg);
                    int position = (int)v.getTag();
                    _sofosapps.get(position).setColor(dbg);
                    notifyDataSetChanged();
                    _puw.dismiss();
                }
            });
sherlock
  • 97
  • 5
0

invoke custom dialog on onClick event of _colorenotifica and listen to it's button click events, then get the appropriate item object based on position and apply changes...

.
.
_colorenotifica.setOnClickListener(new View.OnClickListener() {
     int mPosition = position;

     @Override
     public void onClick(View v) {
         /**
          * invoke custom dialog, add button.. 
          */
          dlg.setButton(AlertDialog.BUTTON_POSITIVE, "Accept", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialogInterface, int i) {
                   getItem(mPosition); // apply changes to the item at position `mPosition`
               }
          });
     }
 });
.
.
Rupesh
  • 3,415
  • 2
  • 26
  • 30