How can I pass data from container activity to custom adapter - android

I am in the following situation:
I have the default implementation of the navigation drawer from Android Studio with a navigationDrawerFragment.
I have a custom listView for this navigationDrawerFragment with a custom adapter.
Since I have different type of item in my listView, i use the classic holder pattern.
My problem is the following: I'd like to send data from my container activity (facebook profile picture and name) to the custom adapter to set the holder with these data.
how can I do that ? See the following code:
if (view == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
drawerHolder = new DrawerItemHolder();
view = inflater.inflate(layoutResID, parent, false);
drawerHolder.ItemName = (TextView) view
.findViewById(R.id.drawer_profileName);
drawerHolder.icon = (ProfilePictureView) view.findViewById(R.id.drawer_profileIcon);
drawerHolder.title = (TextView) view.findViewById(R.id.drawer_titleName);
drawerHolder.item = (TextView) view.findViewById(R.id.drawer_itemName);
drawerHolder.logOut = (TextView) view.findViewById(R.id.drawer_logOut);
drawerHolder.logOut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onLogoutButtonClicked();
}
});
drawerHolder.titleLayout = (LinearLayout) view.findViewById(R.id.titleLayout);
drawerHolder.itemLayout = (LinearLayout) view.findViewById(R.id.itemLayout);
drawerHolder.profileLayout = (LinearLayout) view.findViewById(R.id.profileLayout);
view.setTag(drawerHolder);
} else {
drawerHolder = (DrawerItemHolder) view.getTag();
}
DrawerItem dItem = (DrawerItem) this.drawerItemList.get(position);
if (dItem.isTitle()){
drawerHolder.titleLayout.setVisibility(LinearLayout.VISIBLE);
drawerHolder.itemLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.profileLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.title.setText(dItem.getTitle());
} else if (dItem.getImgResID() == 0) {
drawerHolder.titleLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.itemLayout.setVisibility(LinearLayout.VISIBLE);
drawerHolder.profileLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.item.setText(dItem.getItem());
} else {
drawerHolder.titleLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.itemLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.profileLayout.setVisibility(LinearLayout.VISIBLE);
}
return view;
}
I need in the else to set the data I passed to the custom adapter to the facebookProfilPicture.

Pass the data in the constructor form your activity to the adapter.
public class YourAdapter {
private final String facebookProfilePictureUrl;
private final String facebookProfileName;
public YourAdapter(String facebookProfilePictureUrl, String facebookProfileName) {
this.facebookProfilePictureUrl = facebookProfilePictureUrl;
this.facebookProfileName = facebookProfileName;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (view == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
drawerHolder = new DrawerItemHolder();
view = inflater.inflate(layoutResID, parent, false);
drawerHolder.ItemName = (TextView) view
.findViewById(R.id.drawer_profileName);
drawerHolder.icon = (ProfilePictureView) view.findViewById(R.id.drawer_profileIcon);
drawerHolder.title = (TextView) view.findViewById(R.id.drawer_titleName);
drawerHolder.item = (TextView) view.findViewById(R.id.drawer_itemName);
drawerHolder.logOut = (TextView) view.findViewById(R.id.drawer_logOut);
drawerHolder.logOut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onLogoutButtonClicked();
}
});
drawerHolder.titleLayout = (LinearLayout) view.findViewById(R.id.titleLayout);
drawerHolder.itemLayout = (LinearLayout) view.findViewById(R.id.itemLayout);
drawerHolder.profileLayout = (LinearLayout) view.findViewById(R.id.profileLayout);
view.setTag(drawerHolder);
} else {
drawerHolder = (DrawerItemHolder) view.getTag();
}
DrawerItem dItem = (DrawerItem) this.drawerItemList.get(position);
if (dItem.isTitle()){
drawerHolder.titleLayout.setVisibility(LinearLayout.VISIBLE);
drawerHolder.itemLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.profileLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.title.setText(dItem.getTitle());
} else if (dItem.getImgResID() == 0) {
drawerHolder.titleLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.itemLayout.setVisibility(LinearLayout.VISIBLE);
drawerHolder.profileLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.item.setText(dItem.getItem());
} else {
drawerHolder.titleLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.itemLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.profileLayout.setVisibility(LinearLayout.VISIBLE);
}
drawerHolder.title.setText(facebookProfileName);
// Load the profile image here.
return view;
}
}

Related

Android Problems with onClickListener in Adapter

I've spent hours searching for a solution but everything I've tried doesn't work, it simply yields the same results as the code below does.
I am trying to have an onClickListener change the visibility of a TextView but this code always sets the one I click and then another one below.
class EventiAdapter extends ArrayAdapter<CustomEventi> {
List<CustomEventi> customEventi;
LayoutInflater inflater;
EventiAdapter(#NonNull Context context, #LayoutRes int resource, List<CustomEventi> objects) {
super(context, resource, objects);
customEventi = objects;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#NonNull
#Override
public View getView(final int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View view = convertView;
final EventiViewHolder eventiViewHolder;
if(view == null) {
eventiViewHolder = new EventiViewHolder();
view = inflater.inflate(R.layout.eventi_list, parent, false);
eventiViewHolder.imageTV = (ImageView) view.findViewById(R.id.image);
eventiViewHolder.nameTV = (TextView) view.findViewById(R.id.name);
eventiViewHolder.categoryTV = (TextView) view.findViewById(R.id.category);
eventiViewHolder.latLngTV = (TextView) view.findViewById(R.id.latLng);
eventiViewHolder.placeTV = (TextView) view.findViewById(R.id.place);
eventiViewHolder.dateTV = (TextView) view.findViewById(R.id.date);
eventiViewHolder.timeTV = (TextView) view.findViewById(R.id.time);;
eventiViewHolder.viewInfo = (Button) view.findViewById(R.id.viewInfo);
eventiViewHolder.informationTV = (TextView) view.findViewById(R.id.information);
eventiViewHolder.mapImage = (ImageView) view.findViewById(R.id.mapImage);
view.setTag(eventiViewHolder);
} else {
eventiViewHolder = (EventiViewHolder) view.getTag();
}
eventiViewHolder.viewInfo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(eventiViewHolder.informationTV.getVisibility() == View.VISIBLE) {
eventiViewHolder.informationTV.setVisibility(View.GONE);
eventiViewHolder.mapImage.setVisibility(View.GONE);
eventiViewHolder.viewInfo.setText("VISUALIZZA INFORMAZIONI");
}
else {
eventiViewHolder.informationTV.setVisibility(View.VISIBLE);
eventiViewHolder.mapImage.setVisibility(View.VISIBLE);
eventiViewHolder.viewInfo.setText("NASCONDI INFORMAZIONI");
}
}
});
System.out.println("POSITION: "+position);
CustomEventi cstEventi = customEventi.get(position);
eventiViewHolder.setDataIntoViewHolder(cstEventi);
return view;
}
}
Thank you in advance to anyone who'll try to help me.
Try
1. Add a boolean field visible to CustomEventi class
private boolean visible;
public boolean isVisible { return visible; }
public void setVisible(boolean visible) { this.visible = visible; }
2. Replace your adapter code with this one:
class EventiAdapter extends ArrayAdapter<CustomEventi> {
List<CustomEventi> customEventi;
LayoutInflater inflater;
EventiAdapter(#NonNull Context context, #LayoutRes int resource, List<CustomEventi> objects) {
super(context, resource, objects);
customEventi = objects;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#NonNull
#Override
public View getView(final int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View view = convertView;
final EventiViewHolder eventiViewHolder;
System.out.println("POSITION: "+position);
CustomEventi cstEventi = customEventi.get(position);
if(view == null) {
eventiViewHolder = new EventiViewHolder();
view = inflater.inflate(R.layout.eventi_list, parent, false);
eventiViewHolder.imageTV = (ImageView) view.findViewById(R.id.image);
eventiViewHolder.nameTV = (TextView) view.findViewById(R.id.name);
eventiViewHolder.categoryTV = (TextView) view.findViewById(R.id.category);
eventiViewHolder.latLngTV = (TextView) view.findViewById(R.id.latLng);
eventiViewHolder.placeTV = (TextView) view.findViewById(R.id.place);
eventiViewHolder.dateTV = (TextView) view.findViewById(R.id.date);
eventiViewHolder.timeTV = (TextView) view.findViewById(R.id.time);;
eventiViewHolder.viewInfo = (Button) view.findViewById(R.id.viewInfo);
eventiViewHolder.informationTV = (TextView) view.findViewById(R.id.information);
eventiViewHolder.mapImage = (ImageView) view.findViewById(R.id.mapImage);
view.setTag(eventiViewHolder);
} else {
eventiViewHolder = (EventiViewHolder) view.getTag();
}
eventiViewHolder.informationTV.setVisibility(cstEventi.isVisible() ? View.VISIBLE : GONE);
eventiViewHolder.mapImage.setVisibility(cstEventi.isVisible() ? View.VISIBLE : View.GONE);
eventiViewHolder.viewInfo.setText(cstEventi.isVisible() ? "NASCONDI INFORMAZIONI": "VISUALIZZA INFORMAZIONI");
eventiViewHolder.viewInfo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean isVisible = customEventi.isVisible();
customEventi.setVisible(!isVisible);
notifyDataSetChanged();
}
});
eventiViewHolder.setDataIntoViewHolder(cstEventi);
return view;
}
}
Try this one....
public View getView(final int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View view = convertView;
if(view == null) {
view = inflater.inflate(R.layout.eventi_list, parent,false);
imageTV = (ImageView) view.findViewById(R.id.image);
nameTV = (TextView) view.findViewById(R.id.name);
categoryTV = (TextView) view.findViewById(R.id.category);
latLngTV = (TextView) view.findViewById(R.id.latLng);
placeTV = (TextView) view.findViewById(R.id.place);
dateTV = (TextView) view.findViewById(R.id.date);
timeTV = (TextView) view.findViewById(R.id.time);;
viewInfo = (Button) view.findViewById(R.id.viewInfo);
informationTV = (TextView) view.findViewById(R.id.information);
mapImage = (ImageView) view.findViewById(R.id.mapImage);
viewInfo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(informationTV.getVisibility() == View.VISIBLE) {
informationTV.setVisibility(View.GONE);
mapImage.setVisibility(View.GONE);
viewInfo.setText("VISUALIZZA INFORMAZIONI");
}
else {
informationTV.setVisibility(View.VISIBLE);
mapImage.setVisibility(View.VISIBLE);
viewInfo.setText("NASCONDI INFORMAZIONI");
}
}
});
System.out.println("POSITION: "+position);
CustomEventi cstEventi = customEventi.get(position);
setDataIntoViewHolder(cstEventi);
return view;
}

Confusion with getview android imagebuttons

I have a listview which has two image buttons in the row. Tapping image button change the background color. When I tap on the first list item, image button background change and the view is saved but scrolling bottom of the listview, background color of image button of another list item changes as well. Below is the getView of Custom Adapter. How can I avoid this problem?
public View getView(final int i, View view, ViewGroup viewGroup) {
ViewHolder holder = new ViewHolder();
holder = null;
//view=null;
if (inflater == null) {
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if (view == null) {
view = inflater.inflate(R.layout.student_list, null);
holder = new ViewHolder();
holder.presentButton = (ImageButton) view.findViewById(R.id.imageView);
holder.absentButton = (ImageButton) view.findViewById(R.id.imageView2);
holder.presentButton.setBackgroundColor(0);
holder.absentButton.setBackgroundColor(0);
view.setTag(holder);
}
else {
holder = (ViewHolder) view.getTag();
}
final SQLiteStudents db1 = new SQLiteStudents(activity.getApplicationContext());
final TextView tvName = (TextView) view.findViewById(R.id.tv_name);
final TextView tvRoll = (TextView) view.findViewById(R.id.tv_roll);
final studentInfo s = students.get(i);
tvRoll.setText(s.getRoll() + ".");
tvName.setText(s.getName());
final Integer roll = Integer.parseInt(s.getRoll());
//ivpresent.setBackgroundColor(0);
final ViewHolder finalHolder1 = holder;
//final ViewHolder finalHolder = holder;
holder.presentButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
db1.updateUser(roll,"present");
finalHolder1.presentButton.setBackgroundColor(GREEN);
finalHolder1.absentButton.setBackgroundColor(0);
//v1.setTag(v.getTag());
//Log.d("present","Roll No: "+String.valueOf(roll));
finalHolder1.presentButton.setTag(Integer.toString(i));
notifyDataSetChanged();
}
});
holder.absentButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
db1.updateUser(roll,"absent");
finalHolder1.presentButton.setBackgroundColor(0);
finalHolder1.absentButton.setBackgroundColor(RED);
//view=null;
//Log.d("absent","Roll No: "+String.valueOf(roll));
finalHolder1.presentButton.setTag(Integer.toString(i));
notifyDataSetChanged();
}
});
return view;
}
public static class ViewHolder {
public ImageButton presentButton;
public ImageButton absentButton;
}
public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<studentInfo> students;
private boolean presentButton = false;
private boolean absentButton = false;
You need to add an else accordingly in your code, see below:
public View getView(final int i, View view, ViewGroup viewGroup) {
ViewHolder holder = new ViewHolder();
holder = null;
//view=null;
if (inflater == null) {
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if (view == null) {
view = inflater.inflate(R.layout.student_list, null);
holder = new ViewHolder();
holder.presentButton = (ImageButton) view.findViewById(R.id.imageView);
holder.absentButton = (ImageButton) view.findViewById(R.id.imageView2);
holder.presentButton.setBackgroundColor(0);
holder.absentButton.setBackgroundColor(0);
view.setTag(holder);
}
else {
holder = (ViewHolder) view.getTag();
}
final SQLiteStudents db1 = new SQLiteStudents(activity.getApplicationContext());
final TextView tvName = (TextView) view.findViewById(R.id.tv_name);
final TextView tvRoll = (TextView) view.findViewById(R.id.tv_roll);
final studentInfo s = students.get(i);
tvRoll.setText(s.getRoll() + ".");
tvName.setText(s.getName());
final Integer roll = Integer.parseInt(s.getRoll());
//ivpresent.setBackgroundColor(0);
final ViewHolder finalHolder1 = holder;
//final ViewHolder finalHolder = holder;
holder.presentButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
db1.updateUser(roll,"present");
presentButton = true;
}
});
holder.absentButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
db1.updateUser(roll,"absent");
absentButton = true;
}
});
if(presentButton){
finalHolder1.presentButton.setBackgroundColor(GREEN);
finalHolder1.absentButton.setBackgroundColor(0);
//v1.setTag(v.getTag());
//Log.d("present","Roll No: "+String.valueOf(roll));
finalHolder1.presentButton.setTag(Integer.toString(i));
notifyDataSetChanged();
} else {
//set button to some default button like black
}
if(absentButton){
finalHolder1.presentButton.setBackgroundColor(0);
finalHolder1.absentButton.setBackgroundColor(RED);
//view=null;
//Log.d("absent","Roll No: "+String.valueOf(roll));
finalHolder1.presentButton.setTag(Integer.toString(i));
notifyDataSetChanged();
} else {
//set button to some default button like black
}
return view;
}

When i was click one button of listview another button also click?how can i stop it

I have a listview with so much button.When i was click one button of listview another button also click?how can i stop it.
please solve this problem.
Give me any trick how can i stop another button in listview.
this is my code
private class MenuItemsAdapter extends ArrayAdapter<MenuItem> {
private static final String TAG = "MenuItemsAdapter";
public MenuItemsAdapter(Context context, List<MenuItem> menuItems) {
super(context, 0, menuItems);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final MenuItem menuItem = getItem(position);
View view = convertView;
final ViewHolder viewHolder;
LayoutInflater inflater;
inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.menu_item, parent, false);
viewHolder = new ViewHolder();
// viewHolder.half = (TextView) view.findViewById(R.id.half);
viewHolder.name = (TextView) view.findViewById(R.id.name);
viewHolder.description = (TextView) view.findViewById(R.id.description);
viewHolder.price = (TextView) view.findViewById(R.id.price);
viewHolder.add = (Button) view.findViewById(R.id.add);
viewHolder.selectedView = view.findViewById(R.id.selectedView);
viewHolder.remove = (Button) view.findViewById(R.id.remove);
viewHolder.total = (TextView) view.findViewById(R.id.itemTotal);
viewHolder.quantity = (TextView) view.findViewById(R.id.quantity);
view.setTag(viewHolder);
try
{
viewHolder.name.setText(menuItem.name);
viewHolder.description.setText(menuItem.description);
viewHolder.price.setText(String.valueOf(menuItem.price));
viewHolder.add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mApplication.createNewCartIfPossibleAndAskIfNot(
mActivity, mRestaurant,
new MainApplication.OnCreateCartListener() {
#Override
public void onCreateCart(Cart cart) {
cart.addOne(menuItem);
updateItemFromCart(menuItem, viewHolder);
updateCart();
}
});
}
});
viewHolder.remove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!mApplication.isCartCreated()) {
return;
}
mApplication.getCart().removeOne(menuItem);
updateItemFromCart(menuItem, viewHolder);
updateCart();
}
});
}catch(NullPointerException e){e.printStackTrace();}
return view;
}
private void updateItemFromCart(MenuItem menuItem, ViewHolder viewHolder) {
if (!mApplication.isCartCreated()) {
return;
}
int quantity = mApplication.getCart().getNOfItemsOfType(menuItem);
if (quantity > 0) {
viewHolder.selectedView.setVisibility(View.VISIBLE);
} else {
viewHolder.selectedView.setVisibility(View.GONE);
}
viewHolder.quantity.setText(String.valueOf(quantity));
viewHolder.total.setText(String.valueOf(quantity
* menuItem.price));
}
class ViewHolder {
TextView name;
TextView description;
TextView price;
Button add;
View selectedView;
Button remove;
TextView total;
TextView quantity;
TextView half;
}
}
I can think of you are missing recycle adapter implementation with static view holder inside adapter. Again, this is hypothetical answer based on text and my past experience. More detailed investigation requires code study.
You can keep null check for your view
final MenuItem menuItem = getItem(position);
View view = convertView;
final ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater;
inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.menu_item, parent, false);
viewHolder = new ViewHolder();
// viewHolder.half = (TextView) view.findViewById(R.id.half);
viewHolder.name = (TextView) view.findViewById(R.id.name);
viewHolder.description = (TextView) view.findViewById(R.id.description);
viewHolder.price = (TextView) view.findViewById(R.id.price);
viewHolder.add = (Button) view.findViewById(R.id.add);
viewHolder.selectedView = view.findViewById(R.id.selectedView);
viewHolder.remove = (Button) view.findViewById(R.id.remove);
viewHolder.total = (TextView) view.findViewById(R.id.itemTotal);
viewHolder.quantity = (TextView) view.findViewById(R.id.quantity);
view.setTag(viewHolder);
}else{
viewHolder= (ViewHolder)convertView.getTag();
}

How to fix repeating items when scrolling in android listview?

I have a custom adapter class which extended by ArrayAdapter. I set the adapter to a listview in my activity. In my objects list I have 8 objects. In the listview shows 5 objects when it's loading(when i'm scrolling the listview it has 3 more data to show). After I scroll the listview last 3 objects are showing same as 1st 3 objects. Here's the code what I tried.
Adapter
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
final MenuItem listItem = objects.get(position);
holder = new Holder();
LayoutInflater inflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.menu_list_item, parent);
holder.textViewItemName = (TextView) convertView.findViewById(R.id.textViewItemName);
holder.textViewItemName.setText(listItem.getItemName());
holder.textViewPrice = (TextView) convertView.findViewById(R.id.textViewPrice);
holder.textViewPrice.setText("$ ".concat(String.valueOf(listItem.getItemPrice())));
holder.imageView = (ImageView) convertView.findViewById(R.id.imageViewItem);
holder.buttonPlus = (ButtonRectangle) convertView.findViewById(R.id.buttonPlus);
holder.cartQtyTextView = (TextView) convertView.findViewById(R.id.textViewCartQty);
// Check & Set
if (holder.buttonPlus != null) {
holder.buttonPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int i = 0;
if (holder.cartQtyTextView != null) {
holder.cartQtyTextView.setText("" + ++i);
}
}
});
}
holder.buttonPlus.setBackgroundColor(Color.WHITE);
holder.buttonPlus.setTextColor(Color.parseColor("#333333"));
holder.buttonMinus = (ButtonRectangle) convertView.findViewById(R.id.buttonMinus);
// Check & Set
if (holder.buttonMinus != null) {
holder.buttonMinus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int i = 0;
if (holder.cartQtyTextView != null) {
holder.cartQtyTextView.setText("" + --i);
}
}
});
}
holder.buttonMinus.setBackgroundColor(Color.WHITE);
holder.buttonMinus.setTextColor(Color.parseColor("#333333"));
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
return convertView;
}
Activity
listViweMenu.destroyDrawingCache();
listViweMenu.setVisibility(ListView.INVISIBLE);
listViweMenu.setVisibility(ListView.VISIBLE);
menuListAdapter.notifyDataSetChanged();
List<uk.co.bapos.android.baposwaiter.data.models.menu.MenuItem> itemsList
= MenuItemController.fetchAllCategoryItems(this, String.valueOf(new ArrayList<>(menuItemData.entrySet()).get(position).getValue()));
listDataMenu.clear();
listDataMenu.addAll(itemsList);
How may I fix this?
You are calling getTag(), setTag() and setting the data in wrong place. Try this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new Holder();
LayoutInflater inflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.menu_list_item, parent);
holder.textViewItemName = (TextView) convertView.findViewById(R.id.textViewItemName);
holder.textViewPrice = (TextView) convertView.findViewById(R.id.textViewPrice);
holder.imageView = (ImageView) convertView.findViewById(R.id.imageViewItem);
holder.buttonPlus = (ButtonRectangle) convertView.findViewById(R.id.buttonPlus);
holder.cartQtyTextView = (TextView) convertView.findViewById(R.id.textViewCartQty);
holder.buttonMinus = (ButtonRectangle) convertView.findViewById(R.id.buttonMinus);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
final MenuItem listItem = objects.get(position);
holder.textViewItemName.setText(listItem.getItemName());
holder.textViewPrice.setText("$ ".concat(String.valueOf(listItem.getItemPrice())));
// Check & Set
if (holder.buttonPlus != null) {
holder.buttonPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int i = 0;
if (holder.cartQtyTextView != null) {
holder.cartQtyTextView.setText("" + ++i);
}
}
});
}
holder.buttonPlus.setBackgroundColor(Color.WHITE);
holder.buttonPlus.setTextColor(Color.parseColor("#333333"));
// Check & Set
if (holder.buttonMinus != null) {
holder.buttonMinus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int i = 0;
if (holder.cartQtyTextView != null) {
holder.cartQtyTextView.setText("" + --i);
}
}
});
}
holder.buttonMinus.setBackgroundColor(Color.WHITE);
holder.buttonMinus.setTextColor(Color.parseColor("#333333"));
}
return convertView;
}
After getting all the id's of views use setTag() and if view is not null use getTag() to get all id's back. Only after that set data to the views.
In Activity :
ArrayAdapter adapter = myAdapter();//pass arraylist or data to adapter here
listview.setAdapter(adapter);
Try this.
LayoutInflater inflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.menu_list_item, parent);
//initializing your view elements
convertView.setTag(holder);
}
else{
holder = (Holder) convertView.getTag();
}
// do other stuff after if-else, setting the values to view elements
You need to understand why holder is used and set as tag.
If convertView is not null means you will be reusing the view (this improves performance and saves memory)
Now what you are doing when it is not null is
holder = (Holder) convertView.getTag();
and returning sameConvertView so you get the same view
What you need to do after getting holder tag is change the view information using holder
Eg holder.textViewItemName.setText(listItem.getItemName());//Needs to get the current list item based on position.
#Override public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
final MenuItem listItem = objects.get(position);
holder = new Holder(); LayoutInflater inflater = (LayoutInflater) this.context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.menu_list_item, parent);
holder.textViewItemName = (TextView)convertView.findViewById(R.id.textViewItemName);
holder.textViewPrice = (TextView) convertView.findViewById(R.id.textViewPrice);
holder.imageView = (ImageView) convertView.findViewById(R.id.imageViewItem);
holder.buttonPlus = (ButtonRectangle) convertView.findViewById(R.id.buttonPlus);
holder.cartQtyTextView = (TextView) convertView.findViewById(R.id.textViewCartQty);
holder.buttonMinus = (ButtonRectangle) convertView.findViewById(R.id.buttonMinus);
convertView.setTag(holder); }
else
{
holder = (Holder) convertView.getTag();
}
holder.textViewItemName.setText(listItem.getItemName());
holder.textViewPrice.setText("$ ".concat(String.valueOf(listItem.getItemPrice()))); // Check & Set if (holder.buttonPlus != null) {
holder.buttonPlus.setOnClickListener(new View.OnClickListener()
{
#Override public void onClick(View v)
{
int i = 0; if (holder.cartQtyTextView != null)
{
holder.cartQtyTextView.setText("" + ++i);
}
}
});
}
holder.buttonPlus.setBackgroundColor(Color.WHITE);
holder.buttonPlus.setTextColor(Color.parseColor("#333333"));
// Check & Set if (holder.buttonMinus != null)
{
holder.buttonMinus.setOnClickListener(new View.OnClickListener()
{
#Override public void onClick(View v)
{
int i = 0; if (holder.cartQtyTextView != null)
{
holder.cartQtyTextView.setText("" + --i);
}
} }); }
holder.buttonMinus.setBackgroundColor(Color.WHITE);
holder.buttonMinus.setTextColor(Color.parseColor("#333333"));
return convertView;
}

Android: OnClick ImageView not change correct Image

I am creating an app where there are images in a custom Listview. The database put a picture or another. Clicking the image has to change. But when I click the image is not changed at the correct ImageView. It changes other ImageView.
I need any help. thanks
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
final MonumentosHolder holder = new MonumentosHolder();
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.listview_row, null);
TextView tv = (TextView) v.findViewById(R.id.textoListviewNombre);
ImageView img3 = (ImageView) v.findViewById(R.id.imagenVisto);
holder.texto = tv;
holder.imagenVisto = img3;
v.setTag(holder);
}
else {
holder = (MonumentosHolder) v.getTag();
}
holder.position = position;
final MonumentosObj p = monumentosLista.get(position);
holder.texto.setText(p.getNombre());
if (p.getVisto().equals("n")) {
holder.imagenVisto.setImageResource(R.drawable.vistorojo);
} else {
holder.imagenVisto.setImageResource(R.drawable.vistoverde);
}
holder.imagenVisto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MonumentosObj p1 = monumentosLista.get(holder.position);
if (p1.getVisto().equals("s")) {
holder.imagenVisto.setImageResource(R.drawable.vistorojo);
p1.setVisto("n");
} else {
holder.imagenVisto.setImageResource(R.drawable.vistoverde);
p1.setVisto("s");
}
MonumentosAdapter.updateMonumentos(p1);
MonumentosObj p = monumentosLista.get(holder.position);
}
});
imageLoader.displayImage(imageUrls[position], holder.imagen, options, animateFirstListener);
return v;
}
EDIT
Onclick not change the correct image. But when I scroll the listview to below and and return to top, the correct image has changed...
I think that bit of code maybe wrong :
holder.imagenVisto.setOnClickListener(new View.OnClickListener() {
private int pos = position;
#Override
public void onClick(View v) {
MonumentosObj p1 = monumentosLista.get(pos);
Try to store position inside holder object and when assigning it in onClick Listener instead
private int pos = position;
do
private int pos = holder.getPosition()
Remember that android ListViews are not loading all items at one time it just loads thats what is visible and few items beyond that,
Also remember that positions of your items in your Arrays doesn't change you may use it somehow :)
try to change
holder.imagenVisto.setOnClickListener(new View.OnClickListener()
by
holder.imagenVisto.setOnClickListener(new OnClickListener()
I think your click listener affect early items in your list!
do this:
put a long item in your viewHolder:
class MonumentosHolder {
.
.
.
long position;
}
and then in your getView, set the holder positon:
View v = convertView;
--EDITED--
final MonumentosHolder holder = new MonumentosHolder();
--END EDITED--
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.listview_row, null);
TextView tv = (TextView) v.findViewById(R.id.textoListviewNombre);
ImageView img3 = (ImageView) v.findViewById(R.id.imagenVisto);
holder.texto = tv;
holder.imagenVisto = img3;
v.setTag(holder);
}
else {
holder = (MonumentosHolder) v.getTag();
}
**holder.postion = position;**
and then click listener would be like this:
EDITED
holder.imagenVisto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MonumentosObj p1 = monumentosLista.get(**holder.position**);
if (p1.getVisto().equals("s")) {
holder.imagenVisto.setImageResource(R.drawable.vistorojo);
p1.setVisto("n");
} else {
holder.imagenVisto.setImageResource(R.drawable.vistoverde);
p1.setVisto("s");
}
MonumentosAdapter.updateMonumentos(p1);
MonumentosObj p = monumentosLista.get(holder.position);
}
});
EDIT 4
remove the final modifier from holder, define another MonumentosHolder named finalHolder like this:
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
MonumentosHolder holder = new MonumentosHolder();
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.listview_row, null);
TextView tv = (TextView) v.findViewById(R.id.textoListviewNombre);
ImageView img3 = (ImageView) v.findViewById(R.id.imagenVisto);
holder.texto = tv;
holder.imagenVisto = img3;
v.setTag(holder);
}
else {
holder = (MonumentosHolder) v.getTag();
}
holder.position = position;
final MonumentosObj p = monumentosLista.get(position);
holder.texto.setText(p.getNombre());
if (p.getVisto().equals("n")) {
holder.imagenVisto.setImageResource(R.drawable.vistorojo);
} else {
holder.imagenVisto.setImageResource(R.drawable.vistoverde);
}
final MonumentosHolder finalHolder = holder;
holder.imagenVisto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MonumentosObj p1 = monumentosLista.get(finalHolder.position);
if (p1.getVisto().equals("s")) {
holder.imagenVisto.setImageResource(R.drawable.vistorojo);
p1.setVisto("n");
} else {
holder.imagenVisto.setImageResource(R.drawable.vistoverde);
p1.setVisto("s");
}
MonumentosAdapter.updateMonumentos(p1);
MonumentosObj p = monumentosLista.get(finalHolder.position);
}
});
imageLoader.displayImage(imageUrls[position], holder.imagen, options, animateFirstListener);
return v;
}

Categories

Resources