访问和编辑自定义适配器的子视图
Access and edit child view of custom adapter
我有一个由自定义 ArrayAdapter 填充的 ListView。
每行的结构由
组成
- ImageView
- 文本视图
- 数字选择器
- 图片按钮
单击 ImageButton 应该会显示一个弹出窗口 window,其中包含一个 color slider 和一个 "ACCEPT" 按钮。
这是一张应该阐明布局的图像。
我想做的是:通过单击包含在弹出窗口 window 中的 "ACCEPT" 按钮,我应该检索选定的颜色,将其设置为 ImageButton 的背景颜色并关闭弹出window。
这是代码:
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;
}
我意识到这种方法是不正确的,因为 imagebutton 的背景图像不会改变。此外,出于某种原因,我无法为每一行创建一个新的弹出窗口 window:当我单击任何图像按钮时,会出现相同的弹出窗口 window,其中文本视图文本设置为 "Twitter" , 最后一行。
这样做的正确方法是什么?
感谢您的帮助!
在 _colorenotifica
的 onClick
事件上调用自定义对话框并监听它的按钮单击事件,然后根据位置获取适当的项目对象并应用更改...
.
.
_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`
}
});
}
});
.
.
您应该更改数组 _sofosapps 中的颜色值,然后在适配器上调用 notifydatasetchanged。
_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();
}
});
我有一个由自定义 ArrayAdapter 填充的 ListView。 每行的结构由
组成- ImageView
- 文本视图
- 数字选择器
- 图片按钮
单击 ImageButton 应该会显示一个弹出窗口 window,其中包含一个 color slider 和一个 "ACCEPT" 按钮。
这是一张应该阐明布局的图像。
我想做的是:通过单击包含在弹出窗口 window 中的 "ACCEPT" 按钮,我应该检索选定的颜色,将其设置为 ImageButton 的背景颜色并关闭弹出window。 这是代码:
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;
}
我意识到这种方法是不正确的,因为 imagebutton 的背景图像不会改变。此外,出于某种原因,我无法为每一行创建一个新的弹出窗口 window:当我单击任何图像按钮时,会出现相同的弹出窗口 window,其中文本视图文本设置为 "Twitter" , 最后一行。 这样做的正确方法是什么? 感谢您的帮助!
在 _colorenotifica
的 onClick
事件上调用自定义对话框并监听它的按钮单击事件,然后根据位置获取适当的项目对象并应用更改...
.
.
_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`
}
});
}
});
.
.
您应该更改数组 _sofosapps 中的颜色值,然后在适配器上调用 notifydatasetchanged。
_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();
}
});