Я новичок в программировании Android. Я пишу простое музыкальное приложение, используя ListView
и пользовательский макет. Каждая строка в списке содержит два TextView's
для названия песни и имени исполнителя.
У меня есть два действия, MainActivity и nowPlaying Activity, в nowPlaying Activity есть список, который также имеет два TextView's
. Чего я хочу достичь, так это когда я нажимаю на элемент в ListView
(в разделе «Основные действия»), я хочу получить название песни и имя исполнителя и установить его на TextView's
в игровой деятельности nowPlaying.
MainActivity
public class MainActivity extends AppCompatActivity {
ListView listView;
ArrayList<Songs> song;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.songs_list);
song = new ArrayList<Songs>();
song.add(new Songs("Shape of you", "ed sheeran", "4:59"));
song.add(new Songs("Energy", "avelino", "3:15"));
song.add(new Songs("Wages", "bad sounds", "2:56"));
song.add(new Songs("L'Hiver Indien", "baloji", "3:28"));
song.add(new Songs("Faded Heart", "Borns", "4:59"));
song.add(new Songs("King Ruby", "ider", "4:59"));
song.add(new Songs("Drown", "kovic", "4:59"));
song.add(new Songs("Supercut", "lorde", "4:59"));
song.add(new Songs("&Run", "sir sly", "4:59"));
song.add(new Songs("Live in the moment", "portugal. the man", "4:59"));
SongAdapter adapter = new SongAdapter(this, song);
listView = findViewById(R.id.list_view);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
TextView songName = findViewById(R.id.song_name);
TextView artistName = findViewById(R.id.artist);
Intent intent = new Intent(MainActivity.this, nowPlaying.class);
intent.putExtra("name", listView.getItemAtPosition(i).toString());
startActivity(intent);
}
});
listView.setAdapter(adapter);
}
}
SongAdapter
public class SongAdapter extends ArrayAdapter<Songs> {
public SongAdapter(Activity context, ArrayList<Songs> song) {
// Here, we initialize the ArrayAdapter's internal storage for the context and the list.
// the second argument is used when the ArrayAdapter is populating a single TextView.
// Because this is a custom adapter for two TextViews and an ImageView, the adapter is not
// going to use this second argument, so it can be any value. Here, we used 0.
super(context, 0, song);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
//return super.getView(position, convertView, parent);
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.song_item, parent, false);
}
Songs currentSong = getItem(position);
TextView songName = listItemView.findViewById(R.id.song_name);
songName.setText(currentSong.getSongName());
TextView artistName = listItemView.findViewById(R.id.artist);
artistName.setText(currentSong.getArtistName());
TextView time = listItemView.findViewById(R.id.time);
time.setText(currentSong.geTime());
return listItemView;
}
}
Скриншот
На скриншоте (указанном выше) каждая строка имеет имя для песни, а другое - для исполнителя. При щелчке по строке я хочу получить имя, задать его для другого действия и сделать то же самое для имени исполнителя.
Я хочу, чтобы это происходило с каждым элементом в ListView
.
SongsClass
public class Songs {
private String mSongName;
private String mArtistName;
private String mTime;
public Songs(String songName, String artistName, String Time) {
mSongName = songName;
mArtistName = artistName;
mTime = Time;
}
public String getSongName() {
return mSongName;
}
public String getArtistName() {
return mArtistName;
}
public String geTime() {
return mTime;
}
}