当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Android-Songs List

问题描述:

I'm new to Android and currently working on a project where I list the title of 100 songs. When the user clicks on a song, the window display changes to display the lyrics of the song. I've done a similar project,for 2 buttons, where I used intent to start a new activity(Java class to read text file). Now that I have a 100 text views, is there a way that I can achieve the same result (display content of text files) without creating 100 java classes to read the 100 text files ?

网友答案:

So, a few suggestions:

  • Definitely try to go Android Developer Training Section to get comfortable with use-cases of the most common scenarios in android.

  • You are stating that you want to display 100 titles, and so your first thought was to create 100 TextView items in your layout. Well, let me tell you that whenever you plan on displaying the same item (by item I mean the same layout) multiple times in one same ContentView, it is a better idea to use a View that can handle reuse for you, so your code is simpler to manager. You can either use a ListView or a GridView.

Basically the ListView uses an Adapter that will fill as many items as you need it to, using a specified LayoutResource. Read on how to use the ListView here.

  • Once you have created ListView on Activity A, set an OnItemClickListener to the list, and whenever an Item is clicked, start an Intent to launch Activity B; in the Intent you can include extras, and therefore you can pass the name of the Song. On Activity B, receive the extra by calling mIntent.getExtras(), and then display the lyrics for the supplied song using a TextView

I could give you the some sample code, but it seems to me like you are on a very early stage and I don't want to confuse you. Take some time to read through the official docs, they are very helpful! Good luck.

网友答案:

why do you need 100 textview or 100 classes??!!! as far i understand from your question, that you are trying to show the lyrics of a song after clicking on it. If so then you can do this.

in second activity only place 1 textview. when user clicks on a song of first activity then pass a data to second activity to identify the song ( may be song name). Then from the second activity read the lyrics using the song name and show it in textview.

Again why do you need 100 textviews+100 classes. Am I missing something?

分享给朋友:
您可能感兴趣的文章:
随机阅读: