Requirement: Clicking an ImageButton
in a ListView
displays a popup window. Based on popup selection, change the ImageButton
in a Listview
to a different ImageButton
.
The issue is, only the last ImageButton
is getting changed when I click the other ImageButton
. The issue is with position of the ImageButton
in the ListView
is not taken into consideration.
How do I change the selected ImageButton
by getting the position of the selected ImageButton
?
Here's the code:
public class FavouritesCellAdapter extends ArrayAdapter<String> {
private int layout;
private List<String> mObjects;
private RelativeLayout mRelativeLayout;
final ViewHolder viewHolder = new ViewHolder();
public FavouritesCellAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
mObjects = objects;
layout = resource;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder mainViewHolder = null;
if(convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
mRelativeLayout = (RelativeLayout) convertView.findViewById(R.id.favorites_listview_small);
viewHolder.title = (TextView) convertView.findViewById(R.id.text1);
viewHolder.button = (ImageButton) convertView.findViewById(R.id.subscribe);
viewHolder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPopupDialog( viewHolder);
Log.v("Message", viewHolder.button + " was selected");
}
});
viewHolder.title.setText(getItem(position));
convertView.setTag(viewHolder);
}
else {
mainViewHolder = (ViewHolder) convertView.getTag();
}
return convertView;
}
public class ViewHolder {
TextView title;
ImageButton button;
Button ok_button;
Button cancel_button;
CheckBox escalatedChkBx;
CheckBox priorityChkBx;
}
private void showPopupDialog(final ViewHolder viewHolder){
try{
LayoutInflater li = LayoutInflater.from(getContext());
final View prompt = li.inflate(R.layout.popup_dialog, null);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getContext());
viewHolder.ok_button = (Button)prompt.findViewById(R.id.ok_button);
viewHolder.cancel_button = (Button)prompt.findViewById(R.id.cancel_button);
viewHolder.escalatedChkBx = ( CheckBox ) prompt.findViewById( R.id.checkBox );
viewHolder.priorityChkBx = ( CheckBox ) prompt.findViewById( R.id.checkBox2 );
alertDialogBuilder.setView(prompt);
final AlertDialog show = alertDialogBuilder.show();
viewHolder.ok_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (viewHolder.escalatedChkBx.isChecked() && viewHolder.priorityChkBx.isChecked()) {
viewHolder.button.setImageResource(R.drawable.ic_email_inactive);
show.dismiss();
}
}
});
viewHolder.cancel_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
show.dismiss();
}
});
alertDialogBuilder.show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Do I have to pass the position of the ImageButton
here in order to set the different ImageButton
if this condition succeeds?
if (viewHolder.escalatedChkBx.isChecked() && viewHolder.priorityChkBx.isChecked()) {
**viewHolder.button.setImageResource(R.drawable.ic_email_inactive);**
show.dismiss();
}
Only the last ImageButton
is changing when I have clicked the other.
you must make an interface like this :
public interface RecyclerViewOnClick {
void itemOnClick (View view, int position);
}
then in your adapter viewholder
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mRecyclerViewOnClick.itemOnClick(v, position);
}
});
then in your activity implement the recyclerviewonclick
@Override
public void itemOnClick(View view, final int position) {
//you will got the item clicked position here.
//then you can validate to differentiate your image based on position right here
}
Hope it helps!
将int position属性添加到show popup方法中,并通过在getView中传递的位置获取viewholder
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.