I am making an app that contains a ListView with contacts in it. The list elements originally start off with just names of contacts in them. There is a hidden ImageView in "choice_inflater" that contains a yellow star that will show up to the left of the name when it is saved as a favorite (held down). The user can hold down the ListView items to save them as favorites, at which point the star should be set to visible for the item they held down.
The problem I am having is that the star is showing up for every six items whenever I hold down the first item and I have no idea why. Can anyone figure out my issue here?
This is my custom ListViewAdapter:
public class ListViewAdapter extends BaseAdapter {
private ArrayList<String> allContacts;
private Context context;
private LayoutInflater inflater;
public ListViewAdapter(Context context, ArrayList<String> allContacts) {
inflater = LayoutInflater.from(context);
this.context = context;
this.allContacts = allContacts;
}
@Override
public int getCount() {
return allContacts.size();
}
@Override
public Object getItem(int position) {
return allContacts.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ContactHolder holder;
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.choice_inflater, parent, false);
holder = new ContactHolder();
assert view != null;
holder.tvContactName = (TextView) view.findViewById(R.id.tvContactListName);
holder.img = (ImageView)view.findViewById(R.id.imageView);
holder.tag = getItemId(position);
view.setTag(holder);
} else {
holder = (ContactHolder) view.getTag();
}
holder.tvContactName.setText(allContacts.get(position));
if(holder.hasStar == 1){
holder.img.setVisibility(View.VISIBLE);
}
else{
holder.img.setVisibility(View.GONE);
}
return view;
}
}
class ContactHolder {
TextView tvContactName;
long hasStar = 0;
ImageView img;
}
This is where I am updating my ListView. The user holds down a list view item, and I want the item they held down to show a Yellow star next to it, to indicate it is a favorite.
listView.setOnItemLongClickListener (new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {
Toast.makeText(getContext(), "Saved as Favorite", Toast.LENGTH_SHORT).show();
int c = listView.getChildCount();
for (int i = 0; i < c; i++)
{
View view2 = listView.getChildAt(i);
ContactHolder hld = (ContactHolder)view2.getTag();
if (hld.tag == id)
{
// update view
hld.hasStar = 1;
hld.img.setVisibility(View.VISIBLE);
break;
}
}
return true;
}
});
}
And finally, this is the XML file I am using to represent each list item in my listview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="0dp"
android:id="@+id/choicelayout"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_gravity="center"
android:layout_weight="1"
android:visibility="gone"
app:srcCompat="@android:drawable/btn_star"/>
<TextView
android:id="@+id/tvContactListName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:gravity="center"
android:layout_gravity="center"
android:layout_marginRight="8dp"
android:layout_weight="1"
android:layout_marginLeft="8dp"
android:textColor="@color/color_black"
android:textSize="20sp"
android:text="MEHUL RUGHANI"/>
</LinearLayout>
</LinearLayout>