2014-04-16 80 views
1

1.i在我的代碼中使用微調。在微調我顯示國家代碼與他們的名字,但在Ui部分我想只顯示代碼,當用戶從微調選擇任何國家。android無法拆分值?

2.我現在正爲國家代碼(印度)和國家代碼值(91) 兩個值的名稱,但我只想顯示國家代碼值(91),因爲我使用了splliting functonality。

,但它不是劈裂和兩個值都出現了,我......,我是什麼困惑我哪裏錯了, 這裏是我的代碼提前

public class RegisterScreen extends Activity implements OnClickListener { 
TextView tv_header,male,female,textview_agree ,spinner_text; 
EditText name,email,phone,password ; 
Button create_account; 
CheckBox checkBoxmale,checkBoxfemale; 
TextView spinner_text_img,txt1,txtClickHere1; 
LinearLayout round_bg_spinone; 
XMPPObjectPass commanInstance; 
private RegisterScreen thisActivity; 
private Thread registerThread; 
private ProgressDialog progressDialog; 
AlertDialog alert; 
android.app.AlertDialog.Builder builder; 
public static final int MESSAGE_START_CREATE = 1124; 
public static final int MESSAGE_START_CREATE_WORKING = 1125; 
public static final int MESSAGE_CREATE_COMPLETE = 1126; 
public static final int MESSAGE_CREATE_ERROR = 1127; 
String genderTxt,login; 
public String name_val,phone_val,emailId_val,password_val,username_val; 

protected String[] CountryCodeArray = { "+93 Afghanistan ", "+335 Albania", "+213  Algeria", 
     "+376  Andorra", "+244  Angola", "+1264  Anguilla", "+672  Antarctica", 
     "+54  Argentina", "+374  Armenia", "+297 Aruba", "+43  Austria", "+880 Bangladesh", 
     "+375 Belarus","+32 Belgium", "+501 Belize", "+299 Benin", "+975 Bhutan", 
     "+55  Brazil", "+673  Brunei", "+257  Burundi", "+855 Cambodia", 
     "+237 Cameroon", "+1 Canada", "+235  Chad", "+56  Chile", "+86  China" 
     ,"+61 Cocos Island", "+57 Colombia", "+269 Comoros", "+242 Congo", 
     "+53 Cuba", "+357  Cyprus", "+45  Denmark", "+20 Egypt", 
     "+291 Eritrea", "+372 Estonia", "+358 Finland", "+33 France", "+995 Georgia", 
     "+49 Germany", "+30 Greece", "+299 Greenland", 
     "+224 Guinea", "+504  Honduras", "+852  Hong Kong", 
     "+354  Iceland","+91   india", "+62 Indonesia", "+98  Iran" 
     , "+964  Iraq", "+972  Israel", "+39  Italy", "+81  Japan", "+254  Kenya", 
     "+686 Kiribati", "+82 Korea", "+856 Laos", "+371 Latvia", "+961 Lebanon", 
     "+231 Liberia", "+389 Macedonia", "+265 Malawi", 
     "+60 Malaysia", "+52  Mexico", "+377  Monaco", 
     "+95  Myanmar","+977  Nepal", "+31 Netherlands", "+64  NewZealand", 
     "+47 Norway", "+92 Pakistan", "+51 Peru", "+7 Russia", "+966 Saudi Arabia", 
     "+65 Singapore", "+27 SouthAfrica", "+34 Spain", 
     "+94 Sri Lanka", "+46  Sweden", "+41  Switzerland", 
     "+866  Taiwan","+66  Thailand", "+256 Uganda", "+1  United States", 
     "+263 Zambia" 
}; 
protected ArrayList<String> state_val = new ArrayList<String>(); 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.registration); 
    commanInstance=(XMPPObjectPass)getApplicationContext(); 
    thisActivity = this; 
    round_bg_spinone = (LinearLayout)findViewById(R.id.round_bg_spinone); 
    name=(EditText)findViewById(R.id.userName); 
    email=(EditText)findViewById(R.id.email); 
    phone=(EditText)findViewById(R.id.phone); 
    tv_header=(TextView)findViewById(R.id.tv_header); 
    male=(TextView)findViewById(R.id.male); 
    female=(TextView)findViewById(R.id.female); 
    txt1=(TextView)findViewById(R.id.txt1); 
    textview_agree=(TextView)findViewById(R.id.textview_agree); 
    spinner_text=(TextView)findViewById(R.id.spinner_text); 
    checkBoxmale=(CheckBox)findViewById(R.id.checkBoxmale); 
    checkBoxfemale=(CheckBox)findViewById(R.id.checkBoxfemale); 
    create_account=(Button)findViewById(R.id.create_account); 
    spinner_text_img=(TextView)findViewById(R.id.spinner_text_img); 
    password=(EditText)findViewById(R.id.password); 
    create_account.setOnClickListener(this); 
    checkBoxfemale.setOnCheckedChangeListener(listener); 
    checkBoxmale.setOnCheckedChangeListener(listener); 

    for(String country : CountryCodeArray) 
    { 
     String[] parts = country.split(" "); 
     String code = parts[0]; 

    } 
} 
private OnCheckedChangeListener listener = new OnCheckedChangeListener() { 

    public void onCheckedChanged(CompoundButton arg0, boolean isChecked) { 
     if(isChecked){ 
      switch(arg0.getId()) 
      { 
      case R.id.checkBoxfemale: 
       checkBoxfemale.setChecked(true); 
       checkBoxmale.setChecked(false); 
       genderTxt = "F"; 
       Log.i(""+genderTxt, "genderTxt"); 

       break; 
      case R.id.checkBoxmale: 
       checkBoxmale.setChecked(true); 
       checkBoxfemale.setChecked(false); 
       genderTxt = "M"; 
       Log.i(""+genderTxt, "genderTxt"); 
       break; 

      } 
     } 

    } 
}; 
@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch (v.getId()) { 
    case R.id.create_account: 
     createAccount(); 
     break; 
    case R.id.spinner_text_img: 
     SingleChoiceWithRadioButton(); 
     // showState(); 
    default: 
     break; 
    } 
} 

public void createAccount() 
{ 
    showLoading(); 
    emailId_val = email.getText().toString(); 
    name_val= name.getText().toString(); 
    phone_val= phone.getText().toString(); 
    password_val=password.getText().toString(); 
    Constants.email_id = emailId_val; 
    Constants.phone_no = phone_val; 


    /* email.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 
     } 

     @Override 
     public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
       int arg3) { 
     } 

     @Override 
     public void afterTextChanged(Editable arg0) { 
      Validation.hasText(email); 
     } 
    }); 
    name.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 
     } 

     @Override 
     public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
       int arg3) { 
     } 

     @Override 
     public void afterTextChanged(Editable arg0) { 
      Validation.isUsername(name, false); 
     } 
    }); 
    password.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 
     } 

     @Override 
     public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
       int arg3) { 
     } 

     @Override 
     public void afterTextChanged(Editable arg0) { 
      Validation.ispassword(password, false); 
     } 
    }); 
    phone.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      Validation.isPhoneNumber(phone, false); 
     } 
    });*/ 

    String request ="<?xml version='1.0' encoding='UTF-8'?>" 
      +"<registration><name>"+name_val+"</name>" 
      +"<email>"+emailId_val+"</email>" 
      +"<phoneNo>"+phone_val+"</phoneNo>" 
      +"<gender>"+genderTxt+"</gender>" 
      +"<password>"+password_val+"</password>"+"</registration>"; 

    new HTTPPost(getApplicationContext(),this).execute(request,"http://54.254.243.97/wisechatter/registrationAPI.php","getRegisterationResponse"); 

} 
/* private boolean checkValidation() { 
    boolean ret = true; 

    if (!Validation.hasText(email)) ret = false; 
    if (!Validation.isUsername(name, true)) ret = false; 
    if (!Validation.isPhoneNumber(phone, true)) ret = false; 
    if (!Validation.ispassword(password, true)) ret = false; 
    return ret; 
} 
private void submitForm() { 
}*/ 

public void showLoading() 
{ 
    progressDialog = ProgressDialog.show(RegisterScreen.this, "", 
      "Please wait...", true); 
} 

public void getRegisterationResponse(int code, String response) 
{ 

    System.out.println("<--- "+response); 
    if(response.contains("Registration Success")) 
    { 
     progressDialog.dismiss(); 
     System.out.println("<--- "+response); 
     // showAlertWithOneBtn("Success", "Account created successfully."); 

     Intent i=new Intent(RegisterScreen.this,ConfirmRegisterationActivity.class); 
     startActivity(i); 
    } 
    else if (response!=null) 
    { 
     progressDialog.dismiss(); 
     try 
     { 
      JSONObject jsonObj = new JSONObject(response); 
      String value = jsonObj.getString("error"); 
      showAlertWithOneBtn("Failed", value);  
     } 

     catch (JSONException e) 
     { 
      e.printStackTrace(); 
     } 



    } 
} 

private void SingleChoiceWithRadioButton() { 
    builder = new AlertDialog.Builder(this); 
    builder.setTitle("Select Country Code"); 
    builder.setSingleChoiceItems(CountryCodeArray, -1, 
      new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 



      String country = ""; 
      String[] countries = CountryCodeArray; 

      countries = country.split(":"); 
      for(String part : countries) 
      { 
       country += part; 
      } 
      Toast.makeText(RegisterScreen.this, 
        CountryCodeArray[which] + " Selected", 
        Toast.LENGTH_LONG).show(); 
      dialog.dismiss(); 
      spinner_text.setText(""+ CountryCodeArray[which]); 

     } 
    }); 
    builder.setNegativeButton("Cancel", 
      new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.dismiss(); 
     } 
    }); 
    alert = builder.create(); 
    alert.show(); 
} 

public void showAlertWithOneBtn(final String title,String Msg) 
{ 
    // TODO Auto-generated method stub 
    AlertDialog.Builder alt_bld = new AlertDialog.Builder(this); 
    alt_bld.setTitle(title) 
    .setMessage(Msg) 
    .setIcon(android.R.drawable.ic_dialog_alert) 
    .setCancelable(false) 
    .setPositiveButton("OK", new DialogInterface.OnClickListener() 
    { 
     public void onClick(DialogInterface dialog, int id) 
     { 
      dialog.cancel(); 
      if(title.contains("Success")) 
      { 
       finish(); 
      } 
      else if(title.contains("Failed")) 
      { 
       finish(); 
      } 

     } 
    }); 

    AlertDialog alert = alt_bld.create(); 
    alert.setTitle(title); 
    //alert.setIcon(R.drawable.mobileassisticon); 
    try{ 
     alert.show(); 
    } 
    catch(Exception e) 
    {} 

} 
    } 

謝謝.. :)

+0

你的分裂工作正常....我檢查netbeans..please有日誌,看看哪裏有問題,這個問題是不是與拆分 –

+0

等我發送......... – User11

回答

0

喜代碼看起來很好,但但如果您使用的代碼值,好像我們會看到代碼

for(String country : CountryCodeArray) 
    { 
     String[] parts = country.split(" "); 
     String code = parts[0]; 

    } 

的碼值是本地內部for循環,你不使用它....鼠她比做這個

再次存儲它在一個數組中,並相應地使用!

你可以做的是

的String []代碼=新的String [CountryCodeArray.length]

並將值存儲在數組指定的代碼中。通過類似

String[] code= new String[CountryCodeArray.length]; 
    for(int i =0;i< CountryCodeArray.length;i++) 
    { 
     String[] parts = CountryCodeArray[i].split(" "); 
     code[i] = parts[0]; 

    } 

並分別使用數組代碼!!

+0

行.. .thanku ..我試試.... :) – User11

+0

請回復,如果它工作正常! –

+0

先生它不工作....我得到兩個值,但我只是想單一的價值 – User11

0

更換

for(String country : CountryCodeArray) 
{ 
    String[] parts = country.split(" "); 
    String code = parts[0]; 

} 

String[] code= new String[CountryCodeArray.length]; 
    for(int i =0;i< CountryCodeArray.length;i++) 
    { 
     String[] parts = CountryCodeArray[i].split(" "); 
     code[i] = parts[0]; 

    } 

並且你可以通過索引上的代碼獲得國家代碼值。