2016-09-18 7 views
0

テキストを設定してテキストをプロファイルデータフィールドに設定し、ユーザーの変更や更新を行う前に前の入力のeditTextsに変更します。続いループ内のスイッチを2回実行することはできません

は符号である:

オリジナルデータが文字列である: {K1 = V1、K2 = V2、K3 = V3、... K9 = V9}

public void parse(String foo) { 
     String foo2 = foo.substring(1, foo.length() - 1); // hack off braces 

     StringTokenizer st = new StringTokenizer(foo2, ","); 
     String[] key = new String[20]; 
     String[] value = new String[20]; 
     int i = 0; 
     while (st.hasMoreTokens()) { 
      String thisToken = st.nextToken();   
      String[] keyValue = thisToken.split("="); 
      try { 
       key[i] = keyValue[0]; 
       value[i] = keyValue[1]; 
       setTextToFields(key[i], value[i]); 
       i++; 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     }   
     //for (i = 0; key[i] != null && value[i] != null && i <= 9; i++) {    
     // setTextToFields(key[i], value[i]); 

    //} 

そして私はこのsetTextToFields方法の第2回以上呼び出されると、

public void setTextToFields(String key, String value) { 
     String dateOfBirth = null; 

     switch (key) { 
      case "dateOfBirth": 
       dateOfBirth = value; 
       try { 
        etYear.setText(dateOfBirth.substring(0, 4), TextView.BufferType.EDITABLE); 
        etMonth.setText(dateOfBirth.substring(5, 7), TextView.BufferType.EDITABLE); 
        etDay.setText(dateOfBirth.substring(8), TextView.BufferType.EDITABLE); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       return; 
      case "firstName": 
       etFirstName.setText(value, TextView.BufferType.EDITABLE); 
       return; 
      case "lastName": 
       etLastName.setText(value, TextView.BufferType.EDITABLE); 
       return; 
      case "country": 
       etCountry.setText(value, TextView.BufferType.EDITABLE); 
       return; 
      case "city": 
       etCity.setText(value, TextView.BufferType.EDITABLE); 
       return; 
      case "iDNumber": 
       etIDNumber.setText(value, TextView.BufferType.EDITABLE); 
       return; 
      case "postCode": 
       etPostCode.setText(value, TextView.BufferType.EDITABLE); 
       return; 
      case "mobilePhoneNumber": 
       etMobilePhoneNumber.setText(value, TextView.BufferType.EDITABLE); 
       return; 
      case "province": 
       etProvince.setText(value, TextView.BufferType.EDITABLE); 
       return; 
      case "address": 
       etAddress.setText(value, TextView.BufferType.EDITABLE); 
       return; 
      default: 
       Toast.makeText(ProfileActivity.this,"Nothing set to text", Toast.LENGTH_SHORT).show(); 
       return; 
} 

スイッチは、デフォルトに選択し、editTに何も設定していない:私は次のように、テキストを設定するにはsetTextToFieldsメソッドを呼び出しextビュー。

理由を教えてください。

+0

私はそうではないと言います。あなたは[mcve]を投稿できますか? –

+0

申し訳ありませんが、何を意味するのか分かりません。 –

答えて

0

最後に、自分の問題を解決する正しい方法を見つけました。

メソッドに渡されたキー[i]はトリミングされません。ですから、キーの後ろにtrim()を追加してください。

switch (key.trim()) { 
      case "dateOfBirth": 
       dateOfBirth = value; 
       try { 
        etYear.setText(dateOfBirth.substring(0, 4), TextView.BufferType.EDITABLE); 
        etMonth.setText(dateOfBirth.substring(5, 7), TextView.BufferType.EDITABLE); 
        etDay.setText(dateOfBirth.substring(8), TextView.BufferType.EDITABLE); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       return; 
      case "firstName": 
       etFirstName.setText(value, TextView.BufferType.EDITABLE); 
       return; 
      case "lastName": 
       etLastName.setText(value, TextView.BufferType.EDITABLE); 
       return; 
      case "country": 
       etCountry.setText(value, TextView.BufferType.EDITABLE); 
       return; 
      case "city": 
       etCity.setText(value, TextView.BufferType.EDITABLE); 
       return; 
      case "iDNumber": 
       etIDNumber.setText(value, TextView.BufferType.EDITABLE); 
       return; 
      case "postCode": 
       etPostCode.setText(value, TextView.BufferType.EDITABLE); 
       return; 
      case "mobilePhoneNumber": 
       etMobilePhoneNumber.setText(value, TextView.BufferType.EDITABLE); 
       return; 
      case "province": 
       etProvince.setText(value, TextView.BufferType.EDITABLE); 
       return; 
      case "address": 
       etAddress.setText(value, TextView.BufferType.EDITABLE); 
       return; 
      default: 
       Toast.makeText(ProfileActivity.this,"Nothing set to text", Toast.LENGTH_SHORT).show(); 
       return; 
    } 
2

whileループ内でiをインクリメントしてください。

key0とvalue0のみを設定しました。残りはヌルなので、forループとendは一度だけを除いて入力されません。また

、あなただけの2の代わりに

は私もHashMapをお勧めしたいwhileループ内setTextToFields(key[i], value[i]);を入れて、代わりにイコール

int i = 0; 
while (st.hasMoreTokens()) { 
    String thisToken = st.nextToken();   
    String[] keyValue = thisToken.split("\\s*=\\s*"); 
    try { 
     key[i] = keyValue[0]; 
     value[i] = keyValue[1]; 

周りのすべてのスペースをキャプチャするために正規表現を更新することができますアレイ。

+0

これはすてきなキャッチです:) –

+1

私の元の答えよりもずっと優れています;) –

+0

あなたの答えをありがとう。両方の方法でキー値がsetTextToFieldsに渡されましたが、スイッチは機能しません。監視トーストは私に「何もテキストに設定されていません」と言います。 –

関連する問題