首页 > 解决方案 > 从具有多个项目的 ArrayList 中检索字符串,并在 TextView 上使用 setText 和检索到的字符串值

问题描述

我是 android/xml/java 的新手。作为课程的一部分,我将制作一个音乐库/播放器应用程序(不播放音乐,只是看起来和表现得像一个)。

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

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

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

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

/代码在此之后/

我知道我可能应该在 ArrayList 中获取项目的 id,然后使用“for”循环遍历每个并显​​示它。

但是,对于我的生活,我无法找出正确的方法来做到这一点。

我不知道如何使用 GitHub,所以如果您需要查看应用程序来弄清楚我可以提供指向 google.drive 的链接。

活动类别:'

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
     */

}

'

音乐是对此的参考:

'    
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 中检索字符串?

标签: javaandroidarraylist

解决方案


这对我有用:

'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());
        }

}

}'

推荐阅读