I am trying to implement a "favorite item" feature on my listview. When user touches the "favorite" imageview of any row, if item was not a favorite, it becomes a favorite, and if item was a favorite, it becomes a non favorite AND the row disappears from the view. (There is a separate view in my app where user can set back the favorite to true if he wants to)
For each list item I am using Sharedpreferences to store if it is a favorite or not.
I am handling the clicklistener in my listadapter, not in my list activity.
Code of my ListAdapter:
public class ListItemsAdapter extends ArrayAdapter<ListItems> {
int resource;
String response;
Context context;
//Initialize adapter
public ListItemsAdapter(Context context, int resource, List<ListItems> items) {
super(context, resource, items);
this.resource=resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LinearLayout ll;
//Get the current alert object
final ListItems i = getItem(position);
//Inflate the view
if(convertView==null)
{
ll = new LinearLayout(getContext());
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li;
li = (LayoutInflater)getContext().getSystemService(inflater);
li.inflate(resource, ll, true);
}
else
{
ll = (LinearLayout) convertView;
}
// to display favorite icon on each row
ImageView favo = (ImageView)ll.findViewById(R.id.favView);
SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(getContext());
if (sPrefs.getBoolean("fav"+i.id, false)==false)
favo.setBackgroundResource(R.drawable.icon_favoriteno);
if (sPrefs.getBoolean("fav"+i.id, false)==true)
favo.setBackgroundResource(R.drawable.icon_favoriteyes);
// listener of the imageview to handle the user's touch
final ImageView fav = (ImageView)ll.findViewById(R.id.favView);
fav.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(getContext());
if (sPrefs.getBoolean("fav"+i.id, false)==false) {
Toast.makeText(getContext(), R.string.addedToFavorite, Toast.LENGTH_SHORT).show();
fav.setBackgroundResource(R.drawable.icon_favoriteyes);
SharedPreferences.Editor editor = sPrefs.edit();
editor.putBoolean("fav"+i.id, true);
editor.commit();
}
else if (sPrefs.getBoolean("fav"+i.id, false)==true) {
Toast.makeText(getContext(), R.string.removedFromFavorite, Toast.LENGTH_SHORT).show();
fav.setBackgroundResource(R.drawable.icon_favoriteno);
SharedPreferences.Editor editor = sPrefs.edit();
editor.putBoolean("fav"+i.id, false);
editor.commit();
}
}
});
return ll;
}
The display of the icon wether it is set as a favorite or not works well. What I don't manage to do is to make the row disappear as soon as the user sets the favorite icon as no.
I have tried to add notifyDataSetChanged(); in the onclicklistener but it does nothing. It is possible that I don't get what I want because I am trying to do this in my ListAdapter class and not my Activity class. The problem is that each row has several icons each with an onclicklistener, so I think I can't use the activity class to handle the click, but maybe I am wrong