如何在下面的程序中像文本视图一样在微调器中显示GetCountryZipCode()?

时间:2018-08-29 09:35:53

标签: java android

我是Android新手。如何在Spinner中显示GetCountryZipCode(),如下面程序中的textview。我在Textview中得到的输出与我希望在Spinner {{3}中得到的输出相同},如何在Spinner中调用GetCountryCode()方法。

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{

    String [] rl;
    String spinnervalue;
    int Position;
    TextView txt;
    String text;
    Spinner spin;
    int conv;
    Object item;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Spinner spin = (Spinner) findViewById(R.id.simpleSpinner);     
        rl=this.getResources().getStringArray(R.array.CountryCodes);
        txt =(TextView)findViewById(R.id.txt);
        txt.setText(GetCountryZipCode());



        final Customadapter customAdapter=new Customadapter(getApplicationContext(),rl);
        spin.setAdapter(customAdapter);

    spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
          Object item;
          int Position;


            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {



            }

        @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
      }

    public String GetCountryZipCode(){
        String CountryID="";
        String CountryZipCode="";
        Object item;
        int Position;

        TelephonyManager manager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
        // getNetworkCountryIso
        CountryID= manager.getSimCountryIso().toUpperCase();
        String[] rl=this.getResources().getStringArray(R.array.CountryCodes);
        for(int i=0;i<rl.length;i++){
            String[] g=rl[i].split(",");
            if(g[1].trim().equals(CountryID.trim())){
                CountryZipCode=g[0];
                break;
            }

        }
        return CountryZipCode;
    }

    //Performing action onItemSelected and onNothing selected
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {


    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {


    }

}

1 个答案:

答案 0 :(得分:0)

公共类MainActivity扩展了AppCompatActivity实现AdapterView.OnItemSelectedListener {

String [] rl;
String spinnervalue;
int Position;
TextView txt;
String text;
Spinner spin;
int conv;
Object item;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    spin = (Spinner) findViewById(R.id.simpleSpinner);     
    rl=this.getResources().getStringArray(R.array.CountryCodes);
    txt =(TextView)findViewById(R.id.txt);
    txt.setText(GetCountryZipCode());



    final Customadapter customAdapter=new Customadapter(getApplicationContext(),rl);
    spin.setAdapter(customAdapter);

    GetCountryZipCode();

spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {



        }

    @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
  }

public String GetCountryZipCode(){
    String CountryID="";
    String CountryZipCode="";
    Object item;
    int Position;

    TelephonyManager manager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
    // getNetworkCountryIso
    CountryID= manager.getSimCountryIso().toUpperCase();
    String[] rl=this.getResources().getStringArray(R.array.CountryCodes);
    for(int i=0;i<rl.length;i++){
        String[] g=rl[i].split(",");
        if(g[1].trim().equals(CountryID.trim())){
            CountryZipCode=g[0];
            spin.setSelection(i);
            break;
        }

    }
    return CountryZipCode;
}

//Performing action onItemSelected and onNothing selected
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {


}

@Override
public void onNothingSelected(AdapterView<?> parent) {


}

}