从具有多个项的ArrayList中检索String,并在TextView

时间:2018-05-03 17:53:04

标签: java android arraylist

我是android / xml / java的新手。 作为课程的一部分,我将制作一个音乐库/播放器应用程序(没有音乐播放,只是应该看起来和行为一样)。

我有一个存储了三个项目的ArrayList:ArtistNameOrGroup,TrackName,Genre。

ArrayList <musics> music = new ArrayList<>(); music.add(new musics("AC/DC", "Thunderstruck", "Hard Rock"));&#34;

然后我需要在相应的列中获取String的值,并将其设置为TextView的文本。

TextView artistOrGroupPlayingTrack = findViewById(R.id.artistOrGroupPlaying);

/ 代码在此 /

之后

我知道我应该在ArrayList中获取该项的id,然后使用&#34;来获取&#34;循环遍历每个并显​​示它。

但是,对于我的生活,我无法找到正确的方法。

我不确定如何使用GitHub,所以如果您需要查看应用程序来解决这个问题,我可以提供google.drive的链接。

活动类别: &#39;

import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import java.util.ArrayList;

public class nowPlaying_activity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nowplaying);

        // Create a list of artist and group names, track name, genres
        ArrayList <musics> music = new ArrayList<>();
        music.add(new musics("AC/DC", "Thunderstruck", "Hard Rock"));
        music.add(new musics("AC/DC", "Thunderstruck", "Hard Rock"));
        music.add(new musics("AC/DC", "Thunderstruck", "Hard Rock"));
        music.add(new musics("AC/DC", "Thunderstruck", "Hard Rock"));
        music.add(new musics("AC/DC", "Thunderstruck", "Hard Rock"));
        music.add(new musics("AC/DC", "Thunderstruck", "Hard Rock"));
        music.add(new musics("AC/DC", "Thunderstruck", "Hard Rock"));
        music.add(new musics("AC/DC", "Thunderstruck", "Hard Rock"));
        music.add(new musics("AC/DC", "Thunderstruck", "Hard Rock"));
        music.add(new musics("AC/DC", "Thunderstruck", "Hard Rock"));
        music.add(new musics("AC/DC", "Thunderstruck", "Hard Rock"));
        music.add(new musics("AC/DC", "Thunderstruck", "Hard Rock"));
        music.add(new musics("AC/DC", "Thunderstruck", "Hard Rock"));
        music.add(new musics("AC/DC", "Thunderstruck", "Hard Rock"));
        music.add(new musics("AC/DC", "Thunderstruck", "Hard Rock"));
        music.add(new musics("AC/DC", "Thunderstruck", "Hard Rock"));


}
    TextView artistOrGroupPlayingTrack = findViewById(R.id.artistOrGroupPlaying);

    /*here goes the code to get a value of String from ArtistNameOrGroup list and cast it on TextView*
   then go through each item with "for" loop and display them
     */


    TextView trackPlayingNow = findViewById(R.id.trackPlaying);
    /*here goes the code to get a value of String from TrackName list and cast it on TextView*
   then go through each item with "for" loop and display them
     */


    TextView genrePlaying = findViewById(R.id.genreOfTrack);
    /*here goes the code to get a value of String from Genre list and cast it on TextView*
   then go through each item with "for" loop and display them
     */

}

&#39;

音乐是对此的参考:

'    
class musics {
    //stores artist or group name
    private final String mArtistGroup;
    //stores name of the track
    private final String mTrackName;
    //stores musical genres
    private final String mGenre;

    public musics(String ArtistNameOrGroup, String TrackName, String Genre){
        mArtistGroup = ArtistNameOrGroup;
        mTrackName = TrackName;
        mGenre = Genre;
    }

    public String getArtistNameOrGroup(){
        return mArtistGroup;
    }

    public String getTrackName(){
        return mTrackName;
    }

    public String getGenre(){
        return mGenre;
    }
}'

我为ListView实现了这个: 我有ListView布局:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp" />

用于此ListView的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/listLinear"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <TextView
        android:id="@+id/artistOrGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textColor="#000000"
        android:textSize="18sp"
        android:background="#FFD54F"
        tools:text="Bethoven" />

    <TextView
        android:id="@+id/trackName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textColor="#000000"
        android:background="#FFD54F"
        android:textSize="20sp"
        tools:text="Symphony №1" />

    <TextView
        android:id="@+id/genre"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textColor="#000000"
        android:background="#FFD54F"
        android:textSize="16sp"
        tools:text="Classical" />

</LinearLayout>

这个适配器:

package com.lordicodesolutions.wheelofmusicapp;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;

class musicsAdapter extends ArrayAdapter <musics> {

   public musicsAdapter(Context context, ArrayList<musics> music){
        super(context, 0, music);
    }
    @NonNull
    @Override
    public View getView(int position, View convertView, @NonNull ViewGroup parent) {
        // Check if an existing view is being reused, otherwise inflate the view
        View listItemView = convertView;
        if (listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(
                    R.layout.list_item, parent, false);
        }

        // Get the {@link musics} object located at this position in the list
        musics currentTrack = getItem(position);

        // Find the TextView in the list_item.xml layout with the ID
        TextView artistOrGroup = listItemView.findViewById(R.id.artistOrGroup);

        assert currentTrack != null;
        //sets artist or group text from array list
        artistOrGroup.setText(currentTrack.getArtistNameOrGroup());

        // Find the TextView in the list_item.xml layout with the ID
        TextView trackName = listItemView.findViewById(R.id.trackName);
        //sets track name text from array list
        trackName.setText(currentTrack.getTrackName());

        // Find the TextView in the list_item.xml layout with the ID
        TextView genre = listItemView.findViewById(R.id.genre);
        //sets genre text from array list
        genre.setText(currentTrack.getGenre());
        //creates list item with 3 text views with set text and displays it
        return listItemView;
    }
}
'

我是否必须再次编写此内容来创建新类和其他布局?所以有没有更简单的方法来做,我必须使用ListView?

如何从包含多个项目的ArrayList中检索字符串?

1 个答案:

答案 0 :(得分:0)

这对我有用:

'public class nowPlaying_activity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nowplaying);

        ArrayList <musics> music = new ArrayList<>();
        music.add(new musics("AC/DC", "Highway To Hell", "Hard Rock"));
        music.add(new musics("AC/DC", "Hells Bells", "Hard Rock"));
        music.add(new musics("U2", "Beautiful Day", "Rock"));
        music.add(new musics("U2", "Pride", "Rock"));
        music.add(new musics("Beethoven", "Symphony No. 5 in C minor", "Classical"));
        music.add(new musics("Beethoven", "Symphony No. 7 in A major", "Classical"));
        music.add(new musics("Mozart", "Oboe Concerto in C major", "Classical"));
        music.add(new musics("Mozart", "Symphony No. 41 ('Jupiter') in C major", "Classical"));
        music.add(new musics("Elvis Presley", "Suspicious Minds", "Rock"));
        music.add(new musics("Elvis Presley", "If I Can Dream", "Rock"));
        music.add(new musics("Rihanna", "We Found Love", "R&B"));
        music.add(new musics("Rihanna", "Love on the Brain", "R&B"));
        music.add(new musics("Eminem", "Lose Yourself", "Rap"));
        music.add(new musics("Eminem", "Love The Way You Lie", "Rap"));
        music.add(new musics("Rammstein", "Du Hast", "Heavy Metal"));
        music.add(new musics("Rammstein", "Mutter", "Heavy Metal"));

        for (int counter = 0; counter < music.size(); counter++){
            musics musicItem = music.get(counter);
            TextView artistOrGroupPlayingTrack = findViewById(R.id.artistOrGroupPlaying);
            artistOrGroupPlayingTrack.setText(musicItem.getArtistNameOrGroup());

            musics trackItem = music.get(counter);
            TextView trackNamePlaying = findViewById(R.id.trackPlaying);
            trackNamePlaying.setText(trackItem.getTrackName());

            musics genreItem = music.get(counter);
            TextView genrePlaying = findViewById(R.id.genreOfTrack);
            genrePlaying.setText(genreItem.getGenre());
        }

}

}'