3

I want to add pictures to my favorite activity when a user tap on a picture. So far I'm able to get the data and display it but for some reason whenever I tap on an image it displays the favorited image, however, when I recheck the favorite activity by clicking on it, it shows empty. Here's the little flow chart.

imageOnTap is implemented on RecyclerAdapter class. I have my Favorite activity and MainActivity.Any help would be appreciated. Thanks

Here's my MyRecyclerAdapter class

     @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();



SharedPreferences settings = c.getSharedPreferences(PREFS_NAME,0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putInt("favorite",albums.get(pos).getImage());
            editor.commit();

            Toast.makeText(c,albums.get(pos).getName() + " ,added to favorite ",Toast.LENGTH_SHORT).show();

            Intent intent = new Intent(c, favorite.class);
           // intent.putExtra(Intent.EXTRA_TEXT, albums.get(pos).getImage());
            c.startActivity(intent);




                }
            });
        }

Here's my favorite activity

  public class favorite extends AppCompatActivity {
        int favImage;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_favorite);

                ImageView displayImage = (ImageView) findViewById(R.id.movieImage);




         SharedPreferences settings = getSharedPreferences(PREFS_NAME,0);

            displayImage.setImageResource(settings.getInt("Favorite", 0));


    //            Intent intent = getIntent();
    //            if (intent.hasExtra(Intent.EXTRA_TEXT)) {
    //                favImage = intent.getIntExtra(Intent.EXTRA_TEXT,image);
    //                displayImage.setImageResource(favImage);
    //
    //            }
             }
        }

Here's my MainActivity

public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();


        if (id == R.id.nav_favorite) {

            Intent intent = new Intent(this,favorite.class);
            startActivity(intent);

        }
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
user7062312
  • 115
  • 2
  • 10

2 Answers2

1

If you are checking favourite activity from nav menu then it will not display anything afterall you are not passing any intent extras in it. Is it being display when you click the image? Are you getting intent params null here?

Ayush P Gupta
  • 1,459
  • 1
  • 17
  • 24
  • when I tap on on the image it displays the image in favorite activity – user7062312 Jan 12 '17 at 04:34
  • you must save this intent value in shared pref or assign it to a static constant if you want to use it in future. Pass this value in intent of favourite activity, when opening from menu. – Ayush P Gupta Jan 12 '17 at 06:08
  • so should I save the intent value in my RecyclerAdapter class by using sharedPref and pass that value to favorite activity? – user7062312 Jan 12 '17 at 06:41
  • yes, or save it to a static variable and pass it in nav item click intent – Ayush P Gupta Jan 12 '17 at 06:45
  • I have edited my code using sharedPref. Can you please take a look at it? I don't see anything when i tap on it – user7062312 Jan 12 '17 at 07:19
  • More better way is to make a static array containing Drawable ids of various drawables that u are using in your adapter. Now you should save the item position in onclick method to shared pref. Now in favourite activity get this position from shared pref, get the drawable from the list with this position as index and then set the image – Ayush P Gupta Jan 12 '17 at 07:46
  • the best way is to make your adapter list as static. Now save the position in shared pref on onclick, and retrieve the position in fav activity and subsequently get the the image by getting item from list with the position as index `editor.putInt("favorite",position);` `displayImage.setImageResource(albums.get(settings.getInt("Favorite", 0).getimage());` – Ayush P Gupta Jan 12 '17 at 08:00
1

Use those preferences:

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(ctx.getApplicationContext());

In your case it might be different activities observe different areas of settings.

THAT IS:

use:

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(ctx.getApplicationContext());
            SharedPreferences.Editor editor = settings.edit();
            editor.putInt("favorite",albums.get(pos).getImage());
            editor.commit();

instead of:

SharedPreferences settings = c.getSharedPreferences(PREFS_NAME,0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putInt("favorite",albums.get(pos).getImage());
            editor.commit();

AND this:

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(ctx.getApplicationContext());

            displayImage.setImageResource(settings.getInt("Favorite", 0));

instead of:

  SharedPreferences settings = getSharedPreferences(PREFS_NAME,0);

            displayImage.setImageResource(settings.getInt("Favorite", 0));
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194