0

having a problem getting the information from my spinners and my calendar, this is how i have it set up in my activity. I get in the toast (result) CodeGA,CodeGD,GareA,GareD,datee all null What am I doing wrong ?

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();
        }
    }

I created a class StringWithTag to associate every spinner item an id the code of this class is in end

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;
}
}

2 Answers 2

0

Instead of using AdapterView, try using your ArrayAdapter.

(StringWithTag)adapter.get(position);
1
  • I don't think this is where the error came from but the variables lose their values when they are outside the onSelectedItem Jun 6, 2016 at 20:29
0

the Spinner's getItemAtPosition doesn't appear to return the Data it

Gets the data associated with the specified position in the list.

Whatever that means. The important value is position that it returns. This is the position (2nd value or int i in your case)in the list and as such the position within the list that was used to populate the spinner's list. So all you have to do is retrieve the data from the list that you used to populate the spinner using i as the offset into the list.

However, Spinner's have another quirk which you may find causing you a problem. That is that the OnItemSelectedListener gets triggered when the Spinner is created. As such you may need to delay adding the listener until after the Spinner is created. You can do this by adding the listener when the Spinner's post event is triggered via a runnable. Here's an example (this is also shows using the position value, it's for a cursor rather than list so uses moveToPosition where you'd reference the respective List member) :-

        // 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) {
                    }
                });
            }
        });
1
  • I've add a Toast in the method onItemSelected and it worked it show me the value and the id of the item selected but when it's out the method onItemSelectedListener the variable become null. Jun 7, 2016 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.