0
我有這樣的代碼爲我的音板,「添加到收藏夾」 選項,在音板的應用程序
公共類soundboardActivity延伸活動{
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代碼:
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);
}
}
問題是,我如何實現「添加到收藏夾」選項(我的最愛是我ñ另一項活動)的上下文菜單?
非常感謝!
感謝您的回答!我現在的問題是,每次用戶點擊「添加到收藏夾」選項時,如何在收藏夾活動表格行中創建一個新按鈕? 再次感謝。 – Varki 2012-03-19 19:31:18
我沒有得到實際的流程如何「添加到收藏夾」和主屏幕links.If你有任何屏幕請參閱這裏 – Manju 2012-03-20 05:33:31
在主屏幕上我有兩個按鈕,一個讓你進入音板活動,另一個讓你進入你的「最愛」活動。每當用戶在音板活動中點擊「添加到收藏夾」時,都必須創建一個「收藏夾」中的新按鈕。 我不知道這是你問的。 – Varki 2012-03-20 06:20:29