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!
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