Android spinner retrieved value shows as toast but can't be used further?

Punjabi Killaz

New Member
First to say that I looked everywhere and I cannot find a solution, but I'm sorry if I repeat something. So this is a situation, I have an android application that is parsing data from some xml url and shows it in listview. Also there is a spinner filled with some items, so i want to filter that retrived data from xml with variable selected from spinner and than add to listview. I created setOnItemSelectedListener and when i choose something from spinner I get toast message of that value, but I can't use it later. This is the code:\[code\]public class profesorActivity extends ListActivity { // All static variables static final String URL = "http://cvele.net78.net/obavestenja1.xml"; // XML node keys static final String KEY_OBAV = "obavestenja"; // parent node static final String KEY_PROF = "profesor"; static final String KEY_PRE = "predmet"; static final String KEY_TXT = "tekst"; static final String KEY_VRE = "vreme"; Spinner spin; ArrayAdapter<String> aa; String[] items = { "item1","item2", "item3", "item4", "item5", "item6","item7"}; ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>(); XMLParser parser = new XMLParser(); String xml = parser.getXmlFromUrl(URL); // getting XML Document doc = parser.getDomElement(xml); // getting DOM element Object string1; NodeList nl = doc.getElementsByTagName(KEY_OBAV); public void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); super.onCreate(savedInstanceState); setContentView(R.layout.profesor); spin=(Spinner)findViewById(R.id.spinner); aa = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,items); aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spin.setAdapter(aa); spin.setOnItemSelectedListener(new OnItemSelectedListener(){ public void onItemSelected(AdapterView<?> parent, View view, int position, long id){ string1=spin.getSelectedItem().toString(); Toast.makeText(getBaseContext(), "Selected:" + string1.toString(),Toast.LENGTH_LONG ).show(); } public void onNothingSelected(AdapterView<?> parent) { Toast.makeText(getBaseContext(), "Spinner1: unselected",Toast.LENGTH_LONG ).show(); } }); NodeList nl = doc.getElementsByTagName(KEY_OBAV); // looping through all item nodes <item> for (int i = 0; i < nl.getLength(); i++) { // creating new HashMap HashMap<String, String> map = new HashMap<String, String>(); Element e = (Element) nl.item(i); // adding each child node to HashMap key => value map.put(KEY_PROF, parser.getValue(e,KEY_PROF)); map.put(KEY_PRE, parser.getValue(e, KEY_PRE)); map.put(KEY_TXT, parser.getValue(e, KEY_TXT)); map.put(KEY_VRE, parser.getValue(e, KEY_VRE)); if((parser.getValue(e, KEY_PROF)).equals(string1.toString())) menuItems.add(map); else Toast.makeText(getBaseContext(), "nothing", Toast.LENGTH_LONG).show(); } // Adding menuItems to ListView ListAdapter adapter = new SimpleAdapter(this, menuItems,R.layout.list_item, new String[] { KEY_PROF, KEY_PRE, KEY_TXT, KEY_VRE }, new int[] {R.id.profesor,R.id.predmet, R.id.tekst, R.id.vreme}); setListAdapter(adapter);\[/code\]To be more precise, when I select something from spinner I want that in variable string1,than is shown, as I sad before, as a toast(\[code\]Toast.makeText(getBaseContext(), "Selected:" + string1.toString(),Toast.LENGTH_LONG ).show();\[/code\]) every time I select something from spinner it is shown, but down in that for loop string1 is useless, i cannot use it to compare to that variable \[code\]if((parser.getValue(e, KEY_PROF)).equals(string1.toString()))\[/code\], but when I compare, for example, to a word \[code\]if((parser.getValue(e, KEY_PROF)).equals("test"))\[/code\] it works, and also it works with firs default value in spinner. So changing the selected value doesn't change the state of variable string1, outside of listener. I tried many other way to save spinner selected item as string, but no luck. What I am doing wrong, I hope I described well problem, if I'm not please ask, I broke my head of thinking and searching the web. Thank you in ahead.
 
Back
Top