【问题标题】:Java If statement that can change String可以更改字符串的 Java If 语句
【发布时间】: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


【解决方案1】:

例如有两个数组 - 一个带有显示名称,一个带有要发送的名称。或者使用HashMap,但这可能是矫枉过正。

这样:

public class Alantic extends ListActivity{
String displayClasses[] = { "City history", "Fort Macon", "example2", "example3", "example4", "example5", "example6"};
String classes[] = { "BEACHCITY", "FORTMACON", "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, displayClasses));
}

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

另一种选择是创建一个自定义(数组)适配器并使用包含这两个名称的自定义数据类。

【讨论】:

  • 它不起作用。这是工作簿的链接。 dropbox.com/sh/1sg8p9uvjbolqxx/AADcMiPWB_JVEysb01B_hUtBa
  • 三个人怎么没有注意到错误new Intent("Alantic.this, ourClass");。它使用操作而不是 ComponentName 创建意图。
  • @DoctororDrive 因为这不是问题所在。
【解决方案2】:
    @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,     displayClasses));

   Hashmap hm = new HashMap<String, String>;
    hm.put("City History","BEACHCITY");// add your items

   }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
   super.onListItemClick(l, v, position, id);
    HashMap hasmap = new HashMap
    String abc = classes[position ];// this becomes your key
    String newString = hm.get(abc);
    try{
   Class ourClass = Class.forName("com.techreviewsandhelp.carteretcountyhistoryguide." +       newString);
   Intent ourIntent = new Intent("Alantic.this, ourClass");
   startActivity(ourIntent);
   }catch (ClassNotFoundException e){
    e.printStackTrace();
   }
  }

【讨论】:

  • 我只是复制并粘贴您的代码,看看它是否可以工作,在导入 HashMap 后出现了一些错误。 2 abc 无法解析为变量,Hashmap 无法解析为类型,hm 无法解析,标记上的语法错误(链接 32)。
  • 我将哈希映射更改为类型化映射。就 ABC 而言,我从未使用过您的方法……您的问题是将价值观相互联系起来。我使用自定义列表适配器..然后我得到这样的值... String yourStringFromListItem = ((YourModel) getListAdapter().getItem(position)).yourGetter();
  • 新代码给了我 7 个错误。 abc 只是获取用户从列表中选择的类名的字符串的名称。
  • 再说一次,我不知道你为什么要那样做 ABC。创建一个自定义列表适配器,一个数据模型,然后使用与我在之前评论中发布的类似代码。这个对我有用。您知道如何创建自定义列表适配器吗?问题是当您不使用自定义适配器时,您无法从列表项中指定返回的信息。因此,使用自定义列表适配器,您可以返回您想要的内容。那将成为您的 abc,然后您可以使用它作为在哈希图中找到您的值的键。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-30
  • 1970-01-01
  • 2012-11-01
  • 1970-01-01
  • 2018-03-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多