android无法分割值?

时间:2014-04-16 07:23:29

标签: android spinner

1.我在我的代码中使用微调器。在微调器中我显示国家代码及其名称。但是在Ui部分我只想显示代码。,当用户从微调器中选择任何国家时。

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)
    {}

}
  }

先谢谢.. :))

2 个答案:

答案 0 :(得分:0)

嗨代码看起来很好,但你在哪里使用代码的值,好像我们会看到代码

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

    }

代码的值是for循环的局部内部,你没有使用它....而不是这样做

再次将其存储在数组中并相应地使用!!

你可以做的是

String [] code = new String [CountryCodeArray.length];

并将值存储在名为code的数组中。通过类似

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

    }

分别使用数组代码!!

答案 1 :(得分: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];

    }

您可以通过在代码上传递索引来获取国家/地区代码值。