0

Hey there I want to add pictures to favorite activity when a user tap on a picture. So far I'm able to get the data but don't know how to pass it to my favorite activity. How should I approach this problem, perhaps using intent and broadcast? Any help would be appreciated. Thanks

Here's my MyRecyclerAdapter class

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyViewHolder>{

    Context c;
    ArrayList<Album> albums;

    public MyRecyclerAdapter(Context c, ArrayList<Album> albums){
        this.c=c;
        this.albums=albums;
    }
    // Initialize holder
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

       View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.model,null);
        MyViewHolder holder = new MyViewHolder(v);
       return holder;
    }

    //Bind data to views
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

      holder.nameTxt.setText(albums.get(position).getName());
      holder.img.setImageResource(albums.get(position).getImage());

     //listener
        holder.setItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClick(View v, int pos) {
                Toast.makeText(c,albums.get(pos).getName() + " ,added to favorite ",Toast.LENGTH_SHORT).show();


             //  albums.get(pos).getName();

            }
        });
    }
    @Override
    public int getItemCount() {
        return albums.size();
    }
}

Here's my MyViewHolder class

public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    ImageView img;
    TextView nameTxt;
    ItemClickListener itemClickListener;

    public MyViewHolder(View itemView) {

        super(itemView);

        nameTxt=(TextView) itemView.findViewById(R.id.nameTxt);
        img = (ImageView) itemView.findViewById(R.id.movieImage);

        itemView.setOnClickListener(this);



    }

    public void setItemClickListener(ItemClickListener ic)
    {
        this.itemClickListener=ic;

    }


    @Override
    public void onClick(View v) {

        this.itemClickListener.onItemClick(v,getLayoutPosition());


    }
}
user7062312
  • 115
  • 2
  • 10
  • Do you mean you want to display selected image in the activity where this RecyclerViewAdapter get called? – Ajay Shrestha Jan 11 '17 at 20:56
  • If you find the correct Image you want to transfer to your activity, try doing so, that you start a new Intent to switch to FavouriteActivity and passing Extra the image. You have to convert Bitmap into ByteArray to do so. [Check here](http://stackoverflow.com/questions/11010386/send-bitmap-using-intent-android) if it helps. – David Kasabji Jan 11 '17 at 21:05
  • @AjayShrestha (ah testai vanam). I was thinking to pass just a string value but may be I should pass an bitmap image . – user7062312 Jan 11 '17 at 21:14

1 Answers1

0

Assuming that your image is located in your drawable folder and can be obtained by this line: albums.get(position).getImage()

then just pass the int value to your FavoritesActivity like this:

Intent intent = new Intent(c, FavoritesActivity.class);
intent.putExtra(Intent.EXTRA_TEXT, albums.get(pos).getImage());
c.startActivity(intent); // put context infront

Note that albums.get(pos).getImage() should return and int value. Then in your FavoritesActivity retrieve by calling the following:

myImageView = (ImageView) findViewById(R.id.my_image_view);

Intent intent = getIntent();
if (intent.hasExtra(Intent.EXTRA_TEXT)){
    int image = intent.getIntExtra(Intent.EXTRA_TEXT);
    myImageView.setImageResource(image);
}
user7062312
  • 115
  • 2
  • 10
MS2099
  • 369
  • 2
  • 11
  • Okay I was able to pass the data, but how do I save the data? Thanks – user7062312 Jan 12 '17 at 00:04
  • @user7062312 where do you want to save the data? – MS2099 Jan 12 '17 at 00:19
  • I want to save data on my favorite activity. I want to display the images that I passed on using intent. Right now, It is only passing the data. – user7062312 Jan 12 '17 at 00:27
  • @user7062312 ok, if you need to display it in a ImageView then do something similar what you did to display the image in your RecyclerView. I updated the answer. I hope it's what you are looking for. – MS2099 Jan 12 '17 at 00:39
  • I tried doing this ImageView displayImage = (ImageView) findViewById(R.id.favImage); int favImage = intent.getIntExtra(Intent.EXTRA_TEXT,image); displayImage.setImageResource(favImage); But this doesn't not save the image, only displays. – user7062312 Jan 12 '17 at 00:40
  • Okay I got it now – user7062312 Jan 12 '17 at 00:41
  • Okay I just added my recycler view layout but whenever I tap on the image it will only display the image but doesn't save it in the favorite activity. Since, I'm using a button( button is in MainActivty) to view favorited images, whenever I clicked the button it shows nothing. Do I need to pass data from recycler to MainActivity and MainActivity to Favorite? – user7062312 Jan 12 '17 at 01:32
  • And by the way this line int image = intent.getIntExtra(Intent.EXTRA_TEXT); requires a second argument. I was using image from my other class. May be that's the reason – user7062312 Jan 12 '17 at 01:39
  • @user7062312 you can't save permanent values to an Activity. When you exit your FavoritesActivity, its values are destroyed. If you want to keep track of favorite images then use a database, that way you can see your favorite images even after you close your app. – MS2099 Jan 12 '17 at 03:56