我有一个带有歌曲和播放器的ListView。在我的ListView
中,我想向button
添加一个函数来保存ListView
行,并将其添加到不同的ListView中的不同活动(收藏夹活动)中。我怎样才能做到这一点?
这是我的适配器代码:
public class Adapter extends ArrayAdapter<String> {
Context context;
int[] song_icon;
boolean pausedSamePos = false;
String[] song_name;
String[] song_duration;
String[] song_duration_sb;
private final int mLcdWidth = 0;
private final float mDensity = 0;
public static MediaPlayer mp = new MediaPlayer();
public static Boolean isPlaying = Boolean.valueOf(false);
public static int pos = 55;
Ids holder;
private final Handler handler = new Handler();
Runnable updateTimeProgressBar;
public Adapter(Context c, String[] song_titles, int song_Icons[],
String[] song_durations) {
super(c, R.layout.item, R.id.textview_song_duration,
song_titles);
this.context = c;
this.song_icon = song_Icons;
this.song_name = song_titles;
this.song_duration = song_durations;
this.song_duration_sb = song_durations;
}
final int[] songPos = { R.raw.song1, R.raw.song2, R.raw.song3,
R.raw.song4, R.raw.song5, R.raw.song6, R.raw.song7,
R.raw.song8, R.raw.song9, R.raw.song10,
R.raw.song11, R.raw.song12, R.raw.song13,
R.raw.song14, R.raw.song15, R.raw.song16,
R.raw.song17, R.raw.song18, R.raw.song19,
R.raw.song20, R.raw.song21, R.raw.song22,
R.raw.song23, R.raw.song24, R.raw.song25,
R.raw.song26, R.raw.song27, R.raw.song28,
R.raw.song29, R.raw.song30, };
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getAdapterViewType(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
holder = null;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.item, parent, false);
holder = new Ids(row);
row.setTag(holder);
} else {
holder = (Ids) row.getTag();
}
if (Adapter.isPlaying && Adapter.pos == position) {
if (pausedSamePos == true) {
holder.pauseed_play.setVisibility(View.VISIBLE);
holder.playing_pause.setVisibility(View.GONE);
} else {
holder.pauseed_play.setVisibility(View.GONE);
holder.playing_pause.setVisibility(View.VISIBLE);
}
holder.song_currenttime_sb.setVisibility(View.VISIBLE);
holder.song_duration.setVisibility(View.INVISIBLE);
holder.song_duration_sb.setVisibility(View.VISIBLE);
holder.seekbar.setVisibility(View.VISIBLE);
} else {
holder.seekbar.setVisibility(View.GONE);
holder.song_currenttime_sb.setVisibility(View.GONE);
holder.song_icon.setImageResource(song_icon[position]);
holder.song_duration_sb.setVisibility(View.INVISIBLE);
holder.song_duration.setVisibility(View.VISIBLE);
holder.pauseed_play.setVisibility(View.GONE);
holder.playing_pause.setVisibility(View.GONE);
}
holder.song_name.setText(song_name[position]);
holder.song_duration.setText(song_duration[position]);
holder.song_duration_sb.setText(song_duration_sb[position]);
final Ids finalHolder = holder;
holder.favorite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "The favorite", Toast.LENGTH_SHORT)
.show();
}
holder.clickRegister
.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
stopPlaying();
return true;
}
});
finalHolder.song_currenttime_sb.setTag(position);
holder.seekbar.setFocusable(true);
holder.seekbar.setTag(position);
holder.clickRegister.setTag(position);
holder.song_icon.setTag(position);
holder.song_name.setTag(position);
holder.song_duration.setTag(position);
holder.song_duration_sb.setTag(position);
int widthSpec = MeasureSpec.makeMeasureSpec(
(int) (mLcdWidth - 10 * mDensity), MeasureSpec.EXACTLY);
holder.menu_options.measure(widthSpec, 0);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) holder.menu_options
.getLayoutParams();
params.bottomMargin = -holder.menu_options.getMeasuredHeight();
holder.menu_options.setVisibility(View.GONE);
return row;
}
我想要什么TLDR:要在Clicked Row上获取song_name,song_duration,songPos,然后保存它,然后使用该数据在“收藏夹活动”中填充ListView。
如果需要更多细节,我很乐意提供。
答案 0 :(得分:0)
基本上正确的方法是:
1)创建一个Song
模型类,并在此类中声明所有必需的属性
2)扩展BaseAdapter
并使用ArrayList<Song>
作为其数据
使用此方法,您可以轻松地从单击的行中检索Song
对象,并将其传递到任何您想要的位置。如果你不知道我在说什么,那么你就缺少一些关于ListView
的基本知识,而且只是向你抛出代码是没有意义的。有大量的教程,请查看this one或that one。
答案 1 :(得分:0)
有很多方法可以解决这个问题,这是我会使用的一种方式 将您的数据(song_name,song_duration,songPos)保存在共享首选项
中的对象中当您转到fav活动时,请阅读您保存的共享偏好
@Override
public void onClick(View v) {
Toast.makeText(context, "The favorite", Toast.LENGTH_SHORT)
.show();
//for example get the fav here and save it in object
YoursongInfoObject yourSongInfoObject=new YoursongInfoObject();
yourSongInfoObject.name=holder.song_name.getText();
.
.
.
.
SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);
Editor editor = prefs.edit();
try {
editor.putString("YourSongInfoID", ObjectSerializer.serialize(yourSongInfoObject));
} catch (IOException e) {
e.printStackTrace();
}
editor.commit();
}
和您的收藏活动
public void onCreate() {
super.onCreate();
if (null == currentFavSong) {
currentFavSong = new YourSongInfoObject();
}
// load tasks from preference
SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);
try {
currentFavSong = (YourSongInfoObject) ObjectSerializer.deserialize(prefs.getString("YourSongInfoObject", ObjectSerializer.serialize(new YourSongInfoObject())));
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}