0

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

4

2 回答 2

2

你真的把它从适配器的数据集中删除了吗?因为我在 OnClickListener 的代码中看不到对 ArrayAdapter的删除调用。

适配器本身只关心它的数据集 - 而不是您如何绘制视图(这是您对收藏夹所做的事情。

于 2013-05-28T13:58:41.000 回答
0

根据您的问题,只是从列表视图中删除一行,

当您在 ListItemsAdapter 的构造函数中分配 ListItems 时,

您只需从 ListItems 中删除该项目,并将适配器重新分配给 listview 或为适配器调用 notifyDataSetChanged,代码如下,

items.remove(location);
adapter = new ListItemsAdapter(this,1, items);//not sure about integer parameter so I just put 1
list.setAdapter(adapter); 

或者

items.remove(location);
adapter.notifyDataSetChanged();

以上两个选项中的任何一个都应该起作用。

于 2013-05-28T14:28:49.333 回答