Java 如何使用ListView工具在活动中列出Eclipse(Android)中的活动类?
我正在制作一个显示篝火歌曲列表的应用程序,目前我在应用程序上只能看到所有歌曲的列表,当点击时,它们会打开另一个活动。打开的下一个活动看起来是我想要的,但是第一个活动菜单上的歌曲列表只显示一个没有格式的列表,或者我在XML中指定的任何图像或按钮。-我没有在代码中链接这些,因为它们阻止了列表的出现 这是我的Menu.Java的Java文件:Java 如何使用ListView工具在活动中列出Eclipse(Android)中的活动类?,java,android,xml,eclipse,listview,Java,Android,Xml,Eclipse,Listview,我正在制作一个显示篝火歌曲列表的应用程序,目前我在应用程序上只能看到所有歌曲的列表,当点击时,它们会打开另一个活动。打开的下一个活动看起来是我想要的,但是第一个活动菜单上的歌曲列表只显示一个没有格式的列表,或者我在XML中指定的任何图像或按钮。-我没有在代码中链接这些,因为它们阻止了列表的出现 这是我的Menu.Java的Java文件: package com.lmarshall1995.scoutsongs; import android.app.ListActivity; import a
package com.lmarshall1995.scoutsongs;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Menu extends ListActivity{
String classes[] = {"......"};
String items[] = {"......"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, items));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String song_activity = classes[position];
try{
Class<?> ourClass = Class.forName("com.lmarshall1995.scoutsongs." + song_activity);
Intent ourIntent = new Intent(Menu.this, ourClass);
startActivity(ourIntent);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
}
这是我的menu.XML的XML文件,我希望列表放在@+id/list中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" xmlns:app="http://schemas.android.com/apk/lib/com.google.ads">
<TextView
android:id="@+id/SelectSong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@android:id/list"
android:layout_alignTop="@+id/ic_launcher"
android:text="@string/SelectSong"
android:textSize="20sp" />
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="320dp"
android:layout_below="@+id/ic_launcher"
tools:listitem="@android:layout/simple_list_item_1" >
</ListView>
<ImageView
android:id="@+id/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:contentDescription="@string/ic_launcher"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/settings_button"
android:src="@drawable/settings_button_selected"
android:contentDescription="@string/action_settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" />
<ImageButton
android:id="@+id/exit_button"
android:src="@drawable/exit_button_selected"
android:contentDescription="@string/exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
我想知道是否有人能帮我把我认为是android.R.layout.simple_list_item_1从我的menu.xml文件转换成我的list@+id/list。有什么建议吗?
谢谢
从…起
Laurence=]要做到这一点,您必须使用自定义适配器,创建一个扩展BaseAdapter或ArrayAdapter类,并使用该类而不是简单的android适配器。
请参阅教程。在调用setListAdapter之前,在onCreate中调用setContentViewR.layout.menu。如果自定义xml遵循正常android布局的命名约定,即拥有一对id为@android:id/text1和@android:id/text2的TextView,然后他就可以把xml插入普通的适配器,它就可以工作了。我原本也这么认为,但后来在互联网上发现我可以使用@android:id/list而不是@id/list我的案例:-如果你每首歌有一个活动,那就是非常糟糕的应用程序设计。你为什么不使用一个单一的歌曲活动,并通过意图传递你想要它显示的歌曲呢?很好的提示。我会调查的。谢谢你,走吧,你能解释一下我是怎么做的吗?那会是一个列表片段吗?我不知道从哪里开始。这是非常基本的,如果你在谷歌上搜索安卓意向和意向附加,你应该能够解决它。@323go我在谷歌上搜索过,我能找到的最好的方法是通过查找,但这并不能真正解释如何将多个活动组合成一个活动,我知道你来自哪里,我想这样做,它看起来很基本,但我需要一些源代码来查看,没有。谢谢。我知道必须是这样简单的事情。成功了-