我正在为我学校的项目创建一个元素的列表视图,当我运行该应用程序时,它会向我显示该元素的详细信息。我希望它显示一个特定的文本,但我找不到如何去做。 这是我的 Java 代码:
private ArrayList<Playlist>playlists=new ArrayList<Playlist>();
ArrayAdapter<Playlist> adapter;
lvPlaylists=(ListView)findViewById(R.id.lvPlaylists);
adapter =new ArrayAdapter<Playlist>(Playlists.this, android.R.layout.simple_list_item_1, playlists);
lvPlaylists.setAdapter(adapter);
我将项目从 firebase cloud firestore 获取到我的 arraylist 的函数
CollectionReference PlaylistsRef = db.collection("Users").document(Mail).collection("Playlists");
PlaylistsRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()){
for (QueryDocumentSnapshot documentSnapshot : task.getResult())
{
if(!task.getResult().getDocuments().isEmpty())
{
String PlaylistName= (String)documentSnapshot.get("Name");
PlaylistsRef.document(PlaylistName).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
if (documentSnapshot != null){
Playlist playlist = new Playlist((String)documentSnapshot.get("Name"), (String)documentSnapshot.get("Genre"));
playlists.add(playlist);
lvPlaylists.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent=new Intent(Playlists.this,PlaylistView.class);
intent.putExtra("Name", adapter.getItem(position).getName());
intent.putExtra("Genre", adapter.getItem(position).getGenre());
intent.putExtra("PN", PN);
startActivity(intent);
}
});
adapter.notifyDataSetChanged();
}
}
});
}
}
}
}
});`
播放列表:
public class Playlist implements Serializable {
private String name;
private String genre;
private ArrayList<Song> songs;
public Playlist(String name, String genre, ArrayList<Song> songs)
{
this.name=name;
this.genre=genre;
this.songs=songs;
}
public Playlist(String name, String genre)
{
this.name=name;
this.genre=genre;
this.songs=null;
}
public Playlist()
{
this.name=null;
this.genre=null;
this.songs=null;
}
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return this.name;
}
public void setGenre(String genre)
{
this.genre=genre;
}
public String getGenre()
{
return this.genre;
}
public ArrayList<Song> getSongs ()
{
return this.songs;
}
public void addSong (Song song)
{
songs.add(song);
}
public String ToString()
{
return this.name+" "+this.genre;
}
public void removeSong(Song song)
{
for(int i=0;i<this.songs.size();i++)
{
if(this.songs.get(i).getName()==song.getName())
{
this.songs.remove(i);
}
}
}
现在,当我运行应用程序时,它向我显示了这一点: shows me the item's details and I want text