3
votes

I have a listview . When clicked on listitem it opens up a new activity which contains a button(add to fav) which adds the opened list item to my favorites activity(using shared preference) and should change the color of the heart image in listitem from grey to red indicating its in favorites. Adding the listitem to favorites work fine but the heart image is not changing from grey to red unless it is scrolled out of the user's view from the screen and scrolled back to it again. To better understand my problem look at this video

I want the changing of the image instantaneously

By the way to convert the listitem object to jsonString and pass it via intent i used jacksons library

The code i use

onitemclicklistener of my list fragment

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
                        long id) {

                            ObjectMapper mapper = new ObjectMapper();
                            Product pro = productListAdapter.getItem(position);


    try
    {


        String jsonInString = mapper.writeValueAsString(pro);

        Intent intent = new Intent(activity.getApplicationContext(), SingleItemView.class);
        intent.putExtra("selected item", jsonInString);


        startActivity(intent);
    }
    catch (JsonProcessingException e)
    {}  



}

my adapter for the list

public class ProductListAdapter extends ArrayAdapter<Product> {

private Context context;
List<Product> products;
SharedPreference sharedPreference;

public ProductListAdapter(Context context, List<Product> products) {
    super(context, R.layout.product_list_item, products);
    this.context = context;
    this.products = products;
    sharedPreference = new SharedPreference();
}

private class ViewHolder {
    TextView productNameTxt;
    TextView productDescTxt;
    TextView productPriceTxt;
    ImageView favoriteImg;
}

@Override
public int getCount() {
    return products.size();
}

@Override
public Product getItem(int position) {
    return products.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.product_list_item, null);
        holder = new ViewHolder();
        holder.productNameTxt = (TextView) convertView
            .findViewById(R.id.txt_pdt_name);
        holder.productDescTxt = (TextView) convertView
            .findViewById(R.id.txt_pdt_desc);
        holder.productPriceTxt = (TextView) convertView
            .findViewById(R.id.txt_pdt_price);
        holder.favoriteImg = (ImageView) convertView
            .findViewById(R.id.imgbtn_favorite);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    Product product = (Product) getItem(position);
    holder.productNameTxt.setText(product.getName());
    holder.productDescTxt.setText(product.getDescription());
    holder.productPriceTxt.setText(product.getPrice() + "");

    /*If a product exists in shared preferences then set heart_red drawable
     * and set a tag*/
    if (checkFavoriteItem(product)) {
        holder.favoriteImg.setImageResource(R.drawable.heart_red);
        holder.favoriteImg.setTag("red");
    } else {
        holder.favoriteImg.setImageResource(R.drawable.heart_grey);
        holder.favoriteImg.setTag("grey");
    }

    return convertView;

}

/*Checks whether a particular product exists in SharedPreferences*/
public boolean checkFavoriteItem(Product checkProduct) {
    boolean check = false;
    List<Product> favorites = sharedPreference.getFavorites(context);
    if (favorites != null) {
        for (Product product : favorites) {
            if (product.equals(checkProduct)) {
                check = true;
                break;
            }
        }
    }
    return check;
}

@Override
public void add(Product product) {
    super.add(product);
    products.add(product);
    notifyDataSetChanged();
}

@Override
public void remove(Product product) {
    super.remove(product);
    products.remove(product);
    notifyDataSetChanged();
}   
}

singleitem activity

public class SingleItemView extends Activity
{
ProductListAdapter padaptr;
SharedPreference sharedPreference;

List<Product> products = null;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    // TODO: Implement this method
    super.onCreate(savedInstanceState);
    setContentView(R.layout.singleitem);
    sharedPreference = new SharedPreference();
    padaptr = new ProductListAdapter(SingleItemView.this, products);





    Button btn = (Button) findViewById(R.id.singleitemButton1);
    btn.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v){
            products=new ArrayList<Product>();
            Bundle extras = getIntent().getExtras();

            String jsonObj = extras.getString("selected item");


            ObjectMapper mapper = new ObjectMapper();

            try
            {
                Product pro = mapper.readValue(jsonObj, Product.class);

                if (checkFavoriteItem(pro)) {

                    sharedPreference.removeFavorite(SingleItemView.this, pro);

                    Toast.makeText(SingleItemView.this,
                                   SingleItemView.this.getResources().getString(R.string.remove_favr),
                                   Toast.LENGTH_SHORT).show();
                                   padaptr.notifyDataSetChanged();
                } else {
                    sharedPreference.addFavorite(SingleItemView.this, pro);
                    Toast.makeText(SingleItemView.this,
                                   SingleItemView.this.getResources().getString(R.string.add_favr),
                                   Toast.LENGTH_SHORT).show();
                                   padaptr.notifyDataSetChanged();


                }
            }
            catch (IOException e)
            {};





        }



            private boolean checkFavoriteItem(Product checkProduct) {
                boolean check = false;
                List<Product> favorites = sharedPreference.getFavorites(getApplicationContext());
                if (favorites != null) {
                    for (Product product : favorites) {
                        if (product.equals(checkProduct)) {
                            check = true;
                            break;
                        }
                    }
                }
                return check;
            }
    });
    }


}
2

2 Answers

0
votes

You need to notify your adapter about the changes, when you came back to the fragment/activity.

Use startActivityForResult() to start your second activity:

Intent intent = new Intent(activity.getApplicationContext(), SingleItemView.class);
intent.putExtra("selected item", jsonInString);
startActivityForResult(intent, 1);

Then override onActivityResult() method to handle the call back, and notify your adapter with notifyDataSetChanged():

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
        productListAdapter.notifyDataSetChanged();
    }
}
0
votes

I'd like to achieve the behaviour you've attached in your video with adding a toggleSelection with my List Adapter. Here's I'm attaching an working adapter which will toggle the list item background when clicked. This might be modified to serve your purpose.

public class ToggleSelectionListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private Cursor mCursor;
        private SparseBooleanArray selectedItems;

        public ToggleSelectionListAdapter(Cursor cursor) {
            mCursor = cursor;
            selectedItems = new SparseBooleanArray();
        }

        public void toggleSelection(int pos) {
            if (selectedItems.get(pos, false)) {
                selectedItems.delete(pos);
            } else {
                selectedItems.put(pos, true);
            }
            notifyItemChanged(pos);
        }

        public int getSelectedItemCount() {
            return selectedItems.size();
        }

        public void clearSelections() {
            selectedItems.clear();
            notifyDataSetChanged();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {

            public ViewHolder(final View itemView) {
                super(itemView);

                // Initialize your items of each row here
            }

            public void bindView(int pos) {
                try {
                    if (mCursor.isClosed())
                        return;

                    mCursor.moveToPosition(pos);

                    // Maintain a checked item list so that you can have a track if the item is clicked or not
                    if (checkedItems.contains(number) itemView.setBackgroundResource(R.drawable.background_selected);
                    else itemView.setBackgroundResource(R.drawable.background_normal);

                    itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                                    if (checkedItems.contains(number)) {
                                        checkedItems.remove(number);
                                    } else {
                                        checkedItems.add(number);
                                    }

                                // Get the index of which item should toggle the background
                                int idx = mRecyclerView.getChildAdapterPosition(v);
                                toggleSelection(idx);
                            }
                        }
                    });
            }
        }


        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            View v;

            v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_row, parent, false);

            ViewHolder vh = new ViewHolder(v);

            return vh;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

            if (holder instanceof ViewHolder) {
                ViewHolder vh = (ViewHolder) holder;

                vh.bindView(position);

            }
        }

        @Override
        public int getItemCount() {

            if (mCursor == null) {
                return 0;
            }

            int n = mCursor.getCount();
            return n;
        }

        @Override
        public int getItemViewType(int position) {
            return super.getItemViewType(position);
        }

        synchronized public void swapCursor(Cursor cursor) {
            mCursor = cursor;
            notifyDataSetChanged();
        }
    }