Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7675983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:05:42+00:00 2026-05-31T17:05:42+00:00

I have this code for my soundboard, public class soundboardActivity extends Activity { View

  • 0

I have this code for my soundboard,

public class soundboardActivity extends Activity {

View lastClickedView;

    public static void setIntArrayPref(Context context, String key, ArrayList<Integer>       values) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = prefs.edit();
    JSONArray a = new JSONArray();
    for (int i = 0; i < values.size(); i++) {
        a.put(values.get(i));
    }
    if (!values.isEmpty()) {

        editor.putString(key, a.toString());
    } else {
        editor.putString(key, null);
    }
    editor.commit();
}



   public boolean onCreateOptionsMenu(Menu menu) {
       MenuInflater inflater = getMenuInflater();
       inflater.inflate(R.menu.menu, menu);
       return true;
   }
   // Contextual Menu
    public void onCreateContextMenu(ContextMenu menu, View v,  ContextMenuInfo menuInfo)
    {
        super.onCreateContextMenu(menu, v, menuInfo);

            menu.setHeaderTitle("OPTIONS");
            MenuInflater inflater = getMenuInflater();
                inflater.inflate(R.menu.menu_contex_apm, menu);

                lastClickedView = v;
    }
    public boolean onContextItemSelected(MenuItem item) {

            switch (item.getItemId()) {
                case R.id.ctxtapm_opcio1:
                    //favorites

                    switch (lastClickedView.getId()){
                                 case R.id.a1: favorites.add(R.raw.sound1);
                                return true;
                                    }
                                 case R.id.a2: favorites.add(R.raw.sound2);
                                return true;
                                    }
                                 case R.id.a3: favorites.add(R.raw.sound3);
                                return true;
                                    }
                  setIntArrayPref(this, "favoritos", favorites);


                    return true;
                case R.id.ctxtapm_opcio2:
                   //share

                    return true;

                default:
                    return super.onContextItemSelected(item);
            }
        }

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.apm);

    Map<Integer, Integer> map = new HashMap<Integer, Integer>();
    map.put(R.id.a1, R.raw.sound1);
    map.put(R.id.a2, R.raw.sound2);
    map.put(R.id.a3, R.raw.sound3);



    for (Map.Entry<Integer, Integer> entry : map.entrySet()) {


        final MediaPlayer sound = MediaPlayer.create(this,entry.getValue());
        Button button = (Button) findViewById(entry.getKey());

        registerForContextMenu(button);

        button.setOnClickListener(new View.OnClickListener() {

           public void onClick(View v) {
                sound.start();
            }

        });

    }

FavoritesActivity code:

   public class FavoritosActivity extends Activity {

ArrayList<Integer> favorites=new ArrayList<Integer>();

public View getView (int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = this.getLayoutInflater();
        View item = inflater.inflate(R.layout.buttonsample, null);

    return item;  
}

public static ArrayList<Integer> getIntArrayPref(Context context, String key) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    String json = prefs.getString(key, null);
    ArrayList<Integer> favoritos = new ArrayList<Integer>();
    if (json != null) {
        try {
            JSONArray a = new JSONArray(json);
            for (int i = 0; i < a.length(); i++) {
                int musicaid = a.optInt(i);
                favoritos.add(musicaid);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    return favoritos;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.proba);

    favorites = getIntArrayPref(this, "favoritos");

    ListView lv = (ListView) findViewById(R.id.listView1);


    ArrayAdapter<Integer> arrayAdapter =  new ArrayAdapter<Integer>(this,android.R.layout.simple_list_item_1, favorites);
    lv.setAdapter(arrayAdapter); 


}

}

question is, how can I implement an “add to favorites” option ( favorites are in another activity) for the contextual menu?

thanks a lot!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:05:43+00:00Added an answer on May 31, 2026 at 5:05 pm

    Over ride onCreateOptionsMenu with in that create one more ArrayList for favorites .

    Store the song id in arraylist for each time.
    Store this array list in SharedPreferences object
    When user select favourites play navigate to next activity get SharedPreferences.
    According to position from the list of favourites play the song.

    I hope the above solution is clear for you

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My main class look like this: public class Soundboard extends Activity { SharedPreferences preferences;
In my soundboard app I have created a context menu using this code. public
I have this code but it doesn't work! public class Trial { public static
I have this code in a single file : public class genIntro { public
I have this code: public void replay() { long previous = DateTime.Now.Ticks; for (int
I have this code for an event handler: rbM.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton
I have this code: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { mouseSwiped = NO; UITouch
I have this code: <div class = content-dir-item> <p>Text input</p> <img src=./images/email.png class =
I have this code for converting a byte[] to float[] . public float[] ConvertByteToFloat(byte[]
I have this code, that draws an image. private void timer1_Tick(object sender, EventArgs e)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.