سؤال

1.i am using spinner in my code.In spinner i am showing country code with their name.,but on Ui part i want to show only code.,when user select any country from spinner.

2.Right now i am geeting both value name of country code(india) and value of country code(91) but i want to show only country code value like (91) ,for that i used splliting functonality.

but its not spliting and both values are showing,what i do..,i am confused where i am wrong, here is my code

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

}
  }

Thanks in Advance..:)

هل كانت مفيدة؟

المحلول

Hi the code looks fine but but where you are using the value of code as if we will see the code

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

    }

the value of code is local inside to for loop, you are not using it.... rather than doing this

store it in an array again and use accordingly!!

what you can do is

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

and store values at array named code. by means something like

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

    }

and use the array code respectively!!

نصائح أخرى

Replace

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

}

with

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

    }

and you can get country code value by passing index on Code.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top