在微调器上设置文本

时间:2017-09-16 05:19:16

标签: android

我想为三个微调器实现一个代码,用户将在注册时填写出生日期,当他/她将登录并希望更新DOB时,DOB将传递我在日期间完成的微调器和月,但一年有一个问题,即适配器。适配器获取(thisYear-18)年并打印它但我希望spinYear将获取我在注册时存储的值。

@Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edit_details_fragment);
    Spinner spinDay = (Spinner) findViewById(R.id.spinnerDay);
    Spinner spinMonth = (Spinner) findViewById(R.id.spinnerMonth);
    Spinner spinYear = (Spinner) findViewById(R.id.spinnerYear);
    Button update = (Button)findViewById(R.id.update);

    Intent intent =getIntent();
     String str_DOB = intent.getStringExtra("dateofbirth");
    Log.e("DOB************",str_DOB);
    String[] d = str_DOB.split("-");
    Log.e("d[0]-------",d[0].toString());
    spinYear.setSelection (Integer.parseInt(d[0]));
    spinMonth.setSelection (Integer.parseInt(d[1]));
    spinDay.setSelection (Integer.parseInt(d[2]));

年适配器的代码

    ArrayList<String> years = new ArrayList<String>();
    int thisYear = Calendar.getInstance().get(Calendar.YEAR);
    thisYear=thisYear-18;
    for (int i = thisYear; i >= 1967; i--) {
        years.add(Integer.toString(i));
    }
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, years);
    spinYear.setAdapter(adapter);
    spinYear.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) {
            final String yearName =  adapterView.getItemAtPosition(pos).toString().trim();
        }
        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
        }
    });  

2 个答案:

答案 0 :(得分:2)

如果我理解正确,你想根据传入的Intent设置年份微调器的选择。

setSelection(int position)获取要选择的微调器中项目的索引。

致电spinYear.setAdapter(adapter); int index = years.indexOf(d[0]); if (index != -1) { spinYear.setSelection(index); } 后,请执行以下操作:

Google Aggregate Rating

答案 1 :(得分:0)

您应该检查 onItemSelected()方法中的,因为此方法获取位置0并将其设置为first.try以使用以下代码: -

spinYear.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) {

if((d[2]).equals(adapterView.getItemAtPosition(pos).toString()))
{
            final String yearName =  adapterView.getItemAtPosition(pos).toString().trim();
}
        }
        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
        }
    });