Java的if语句,可以改变字符串 [英] Java If statement that can change String

查看:143
本文介绍了Java的if语句,可以改变字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个应用程序,用户可以在选择一个城市在给区,这引起了在给定区域的事件列表视图。我使用这个视频指南,以此来帮助我的https://www.youtube.com/watch?v=zjHYyAJQ7Vw&index=21&list=PL2F07DBCDCC01493A 。不同的人在视频,我需要我的名单说一件事,结果说一个。我无法找到一个方法来改变我的字符串它到达try语句之前。例如,我要改变城市史到beachcity(所以他们带到这个页),堡垒梅肯到fortmacon,等等。这离用户看到的东西,他们也能理解。

有没有办法做到这一点?在最坏的情况,有没有办法用一些if语句来改变呢?下面是JAVA code

 公共类Alantic扩展ListActivity {
字符串类[] = {城市史,堡垒梅肯,例题,示例3,例4,例5,example6};@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);
    setListAdapter(新ArrayAdapter<串GT;(Alantic.this,android.R.layout.simple_list_item_1,类));
}@覆盖
保护无效onListItemClick(ListView中升,视图V,INT位置,长的id){
    // TODO自动生成方法存根
    super.onListItemClick(L,V,位置ID);
    字符串ABC =类[位置]
    尝试{
    类ourClass =的Class.forName(com.techreviewsandhelp.carteretcountyhistoryguide。+ ABC);
    意图ourIntent =新意图(Alantic.this,ourClass);
    startActivity(ourIntent);
    }赶上(ClassNotFoundException的E){
        e.printStackTrace();
    }
}

这怎么code应努力为String类中的项目列表中显示出来(这确实)>用户点击列表项目>项目去串ABC>(我需要的东西的项目转换到别的市历史必须BEACHCITY堡梅肯必须FORTMACON,依此类推)>什么是转换将进入类ourClass行?这看起来对相同的com.tech ......清单操作列表中>这将启动与动作的名称相匹配另一个Java文件>此开始布局,而我想要的任何功能(按钮和什么不可以)。

我不认为我可以使用操作名称空间,但给我。我仍然需要名称改变,因为我不得不这样做同样的事情在多个城市。这意味着我必须重用城历史,墓地,和其他一些东西作为每个城市列表项。

下面是清单我的code。

 <清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
包=com.techreviewsandhelp.com.carteretcountyhistoryguide
安卓版code =1
机器人:=的versionName1.0><用途-SDK
    安卓的minSdkVersion =8
    机器人:targetSdkVersion =19/><应用
    机器人:allowBackup =真
    机器人:图标=@绘制/ ic_launcher
    机器人:标签=@字符串/ APP_NAME
    机器人:主题=@风格/ AppTheme
    机器人:screenOrientation ='画像'>
    <活动
        机器人:名字=泼水节
        机器人:标签=@字符串/ APP_NAME>
        &所述;意图滤光器>
            <作用机器人:名字=android.intent.action.MAIN/>            <类机器人:名字=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;
    <活动
        机器人:名字=com.techreviewsandhelp.com.carteretcountyhistoryguide.MainActivity
        机器人:标签=@字符串/ APP_NAME>
        &所述;意图滤光器>
            <作用机器人:名字=com.techreviewsandhelp.carteretcountyhistoryguide.MAINACTIVITY/>            <类机器人:名字=android.intent.category.DEFAULT/>
        &所述; /意图滤光器>
    < /活性GT;
     <活动
        机器人:AlanticNAME =
        机器人:标签=@字符串/ APP_NAME>
        &所述;意图滤光器>
            <作用机器人:名字=com.techreviewsandhelp.carteretcountyhistoryguide.Alantic/>            <类机器人:名字=android.intent.category.DEFAULT/>
        &所述; /意图滤光器>
    < /活性GT;     <活动
        机器人:名字=。Beachcity
        机器人:标签=@字符串/ APP_NAME>
        &所述;意图滤光器>
            <作用机器人:名字=com.techreviewsandhelp.carteretcountyhistoryguide.BEACHCITY/>            <类机器人:名字=android.intent.category.DEFAULT/>
        &所述; /意图滤光器>
    < /活性GT;
<活动
        机器人:FortMaconNAME =
        机器人:标签=@字符串/ APP_NAME>
        &所述;意图滤光器>
            <作用机器人:名字=com.techreviewsandhelp.carteretcountyhistoryguide.FORTMACON/>            <类机器人:名字=android.intent.category.DEFAULT/>
        &所述; /意图滤光器>
    < /活性GT;
< /用途>

我测试了行动,他们做工精细。


解决方案

  @覆盖
   保护无效的onCreate(捆绑savedInstanceState){
   // TODO自动生成方法存根
   super.onCreate(savedInstanceState);
   setListAdapter(新ArrayAdapter<串GT;(Alantic.this,android.R.layout.simple_list_item_1,displayClasses));   HashMap的HM =新的HashMap<字符串,字符串取代;
    hm.put(城市史,BEACHCITY); //添加项目   }    @覆盖
    保护无效onListItemClick(ListView中升,视图V,INT位置,长的id){
    // TODO自动生成方法存根
   super.onListItemClick(L,V,位置ID);
    HashMap的hasmap =新的HashMap
    字符串ABC =类[位置]; //这将成为你的关键
    字符串newString = hm.get(ABC);
    尝试{
   类ourClass =的Class.forName(com.techreviewsandhelp.carteretcountyhistoryguide。+ newString);
   意图ourIntent =新意图(Alantic.this,ourClass);
   startActivity(ourIntent);
   }赶上(ClassNotFoundException的E){
    e.printStackTrace();
   }
  }

I'm making an app where a user can pick a city in a giving area, and this brings up a listview of events in that given area. I'm using this video guide as a way to help me https://www.youtube.com/watch?v=zjHYyAJQ7Vw&index=21&list=PL2F07DBCDCC01493A . Unlike the person in the video, I need to have my list say one thing, and the results say another. I'm having trouble finding a way to change my String before it gets to the try statement. For example, I want to change "City history" to "beachcity" (so it takes them to this page), "Fort Macon" to "fortmacon", and so on. That away the user sees something they can understand.

Is there a way to do this? At worst, is there a way to change this with some if statement? Below is the JAVA code

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

How this code should work is items within String classes shows up in a list (which it does)> user clicks item on list> item goes to String abc> (I need something to convert the item to something else. City history needs to be BEACHCITY, Fort Macon needs to be FORTMACON, and so on)> what's converted will go into the Class ourClass line? this looks for the same "com.tech...." within the manifest action list> this starts another Java file that matches up with the action name> this starts the layout and any functions (buttons and what not) that I want.

I don't think I can use spaces in action names, but giving I can. I would still need the name to change, because I have to do this exact same thing in multiple cities. This means I have to reuse City history, cemeteries, and several other things as a list item for each city.

Below is my code from the manifest.

<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>

I tested out the actions, and they work fine.

解决方案

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

这篇关于Java的if语句,可以改变字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆