获得Spinner结果

时间:2016-06-06 19:55:27

标签: android spinner

从我的微调器和我的日历中获取信息时出现问题,这就是我在我的活动中设置它的方式。我进入吐司(结果)CodeGA,CodeGD,GareA,GareD,datee all null我做错了什么?

private EditText DateEtxt;
private Spinner spinner1;
private Spinner spinner2;
private DatePickerDialog datePickerDialog;
private SimpleDateFormat dateFormatter;

String URl;
String GareA;
String GareD;
String CodeGD;
String CodeGA;
String datee;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_trajet);
    dateFormatter = new SimpleDateFormat("dd/MM/yyyy");
    dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT"));
    DateEtxt = (EditText) findViewById(R.id.etxt_date);
    DateEtxt.setInputType(InputType.TYPE_NULL);
    DateEtxt.requestFocus();
    DateEtxt.setOnClickListener(this);
    Calendar newCalendar = Calendar.getInstance();
    datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            Calendar newDate = Calendar.getInstance();
            newDate.set(year, monthOfYear, dayOfMonth);
            datee = dateFormatter.format(newDate.getTime()).toString();
            DateEtxt.setText(dateFormatter.format(newDate.getTime()));
        }
    },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
    spinner1 = (Spinner)findViewById(R.id.spinner1);
    spinner2 = (Spinner)findViewById(R.id.spinner2);
    List<StringWithTag> list = new ArrayList<StringWithTag>();

    list.add(new StringWithTag("AEROPORT Med V", "00190"));
    list.add(new StringWithTag("OUELED RAHOU", "00672"));
    list.add(new StringWithTag("OUJDA", "00490"));
    list.add(new StringWithTag("OULAD KHITIB", "00262"));
    list.add(new StringWithTag("Port Tanger Med", "00291"));


    ArrayAdapter<StringWithTag> adapter = new ArrayAdapter<StringWithTag>(this,android.R.layout.simple_spinner_item, list);
    adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    spinner1.setAdapter(adapter);
    spinner2.setAdapter(adapter);

    spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            StringWithTag s = (StringWithTag) adapterView.getItemAtPosition(i) ;
            GareD = s.string.toString();
            Object tag = s.tag ;
            CodeGD = tag.toString();
        }

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

        }
    });
    spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            StringWithTag s = (StringWithTag) adapterView.getItemAtPosition(i) ;
            GareA = s.string.toString();
            Object tag = s.tag ;
            CodeGA = tag.toString();
        }

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

        }
    });

    URl = "http://www.oncf.ma/Pages/ResultatsHoraire.aspx?depart="+GareD+"&arrivee="+GareA+"&CodeRD=0093&CodeGD="+CodeGD+"&CodeRA=0093&CodeGA="+CodeGA+"&heure=0000&date="+datee;

}

public void onSearch (View view) {
    Toast.makeText(
        Trace.this, URl,Toast.LENGTH_LONG
    ).show();

}
    @Override
    public void onClick(View view) {
        if(view == DateEtxt) {
            datePickerDialog.show();
        }
    }

我创建了一个StringWithTag类来将每个微调器项与一个id相关联,该类的代码在最后

public class StringWithTag {
public String string;
public Object tag;

public StringWithTag(String stringPart,Object tagPart){
    string = stringPart;
    tag = tagPart;
}
@Override
public String toString(){
    return string;
}
}

3 个答案:

答案 0 :(得分:0)

请尝试使用ArrayAdapter,而不是使用AdapterView。

(StringWithTag)adapter.get(position);

答案 1 :(得分:0)

Spinner的getItemAtPosition似乎没有返回数据

  

获取与列表中指定位置关联的数据。

无论那意味着什么。重要的价值是它返回的位置。这是列表中的位置(第二个值或 int i ),因此列表中用于填充微调器的位置&# 39; s列表。因此,您只需要使用 i 作为列表中的偏移量,从用于填充微调器的列表中检索数据。

然而,Spinner有另一个怪癖,你可能会发现它会导致你出现问题。也就是说,创建Spinner时会触发OnItemSelectedListener。因此,您可能需要延迟添加侦听器,直到创建Spinner之后。您可以通过在通过runnable触发Spinner的 post 事件时添加侦听器来执行此操作。这是一个示例(这也显示使用位置值,它是光标而不是列表,因此使用moveToPosition,您可以引用相应的List成员) : -

        // Set Spinner's onItemSelectedListener ie to act when an aisle is selected.
        // Note!!! run on post, as a runnable, so that initial onItemSelected event (at initialisation) is not captured.
        current_aislelistspinner.post(new Runnable() {
            @Override
            public void run() {
                current_aislelistspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        currentaislelistcursor.moveToPosition(position);
                        currentaisleid = currentaislelistcursor.getLong(ShopperDBHelper.AISLES_COLUMN_ID_INDEX);
                        if(devmode) {
                            Log.d(Constants.LOG,"ACTIVITY: " + THIS_ACTIVITY + " SECTION: Running - AISLELIST OnItemSelectedLIstener" +
                                    "- AILSEID Extracted=" + currentaisleid);
                        }
                        currentproductsperaisleecursor = shopperdb.getProductsperAisle(currentaisleid,productsperaislesortorder);
                        current_productsperaislecursoradapter.swapCursor(currentproductsperaisleecursor);
                        current_productsperaislecursoradapter.notifyDataSetChanged();
                    }
                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {
                    }
                });
            }
        });

答案 2 :(得分:0)

如果您尝试从微调器获取数据,即在选定值上使用此

public void onItemSelected(AdapterView parent, View v, int position, long id)       {

select.setText(items[position]); // here trying to set output to text view you can give toast here

}

 public void onNothingSelected(AdapterView parent) {

 select.setText(""); // if nothing selected

 }

有关详细信息,请访问[here](http://androidcoding.in/2016/02/28/android-tutorial-on-spinner/“spinner教程”)