I have a ListView populated by a custom ArrayAdapter. The structure of each row is composed by
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.
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!
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();
}
});