【发布时间】:2014-06-20 07:00:28
【问题描述】:
我正在制作一个应用程序,用户可以在其中选择给定区域中的城市,这会显示该给定区域中事件的列表视图。我使用这个视频指南来帮助我https://www.youtube.com/watch?v=zjHYyAJQ7Vw&index=21&list=PL2F07DBCDCC01493A。与视频中的人不同,我需要让我的清单说一件事,结果说另一件事。我很难找到一种方法来更改我的字符串,然后才能到达 try 语句。例如,我想将“City history”更改为“beachcity”(因此将它们带到此页面),“Fort Macon”更改为“fortmacon”,等等。让用户看到他们可以理解的东西。
有没有办法做到这一点?在最坏的情况下,有没有办法用一些 if 语句来改变它?下面是JAVA代码
public class Alantic extends ListActivity{
String classes[] = { "City history", "Fort Macon", "example2", "example3", "example4", "example5", "example6"};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Alantic.this, android.R.layout.simple_list_item_1, classes));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String abc = classes[position];
try{
Class ourClass = Class.forName("com.techreviewsandhelp.carteretcountyhistoryguide." + abc);
Intent ourIntent = new Intent("Alantic.this, ourClass");
startActivity(ourIntent);
}catch (ClassNotFoundException e){
e.printStackTrace();
}
}
这段代码应该如何工作是String类中的项目显示在列表中(它确实如此)>用户单击列表上的项目>项目转到String abc>(我需要一些东西来将项目转换为其他东西。城市历史需要是 BEACHCITY,Fort Macon 需要是 FORTMACON,等等)> 转换后的内容将进入 Class ourClass 行?这会在清单操作列表中查找相同的“com.tech....”> 这会启动另一个与操作名称匹配的 Java 文件> 这会启动布局和我想要的任何功能(按钮等等)。
我认为我不能在动作名称中使用空格,但我可以。我仍然需要更改名称,因为我必须在多个城市做同样的事情。这意味着我必须重复使用城市历史、墓地和其他一些东西作为每个城市的列表项。
以下是清单中的代码。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.techreviewsandhelp.com.carteretcountyhistoryguide"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:screenOrientation='portrait' >
<activity
android:name=".Splash"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.techreviewsandhelp.com.carteretcountyhistoryguide.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.techreviewsandhelp.carteretcountyhistoryguide.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Alantic"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.techreviewsandhelp.carteretcountyhistoryguide.Alantic" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Beachcity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.techreviewsandhelp.carteretcountyhistoryguide.BEACHCITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".FortMacon"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.techreviewsandhelp.carteretcountyhistoryguide.FORTMACON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
我测试了这些操作,它们运行良好。
【问题讨论】:
-
请问,你为什么在标题中提到
(Aniruddha)?请摆脱人身攻击。 -
转发不会帮助您获得更多答案。
-
@sᴜʀᴇsʜᴀᴛᴛᴀ 我的另一个线程被搁置了,而 Aniruddha 正在帮助我。 Aniruddha 说他无法在 cmets 中显示答案,并为我打开一个新线程。
-
顺便说一句,如何更改列表视图中的颜色背景和文本颜色?几乎我的整个应用程序都有黑色背景和白色文本。该列表具有白色背景和黑色文本。
-
要更改列表视图的颜色,您必须有选项,您可以尝试更改列表项的样式。或者您可以创建一个自定义列表适配器。然后,您可以将适配器指向您可以更改背景和文本颜色的自定义列表项。
标签: java android eclipse listview