Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8563441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:57:39+00:00 2026-06-11T16:57:39+00:00

First to say that I looked everywhere and I cannot find a solution, but

  • 0

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:

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

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(Toast.makeText(getBaseContext(), "Selected:" + string1.toString(),Toast.LENGTH_LONG ).show();) 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 if((parser.getValue(e, KEY_PROF)).equals(string1.toString())), but when I compare, for example, to a word if((parser.getValue(e, KEY_PROF)).equals("test")) 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:57:40+00:00Added an answer on June 11, 2026 at 4:57 pm

    When the onCreate() method runs, it executes all of the code. This means that the class member “Object string1” has not been set because the spinner event hasen’t fired yet. You may want to look at the Android Activity life cycle so that you initialize and call methods at the appropriate time.

    The key is that once the spinner event “onItemSelected” is handled that you not only update

    string1=spin.getSelectedItem().toString();
    

    but also update the list and call notify for the change.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First let me say that I have been working on this issue for about
First let me say that I did see this article: How to remove AspxAutoDetectCookieSupport
I can't figure this one out. At first let me say that my cache
First I would like to say that I am new to this site, never
First off I should say that I don't have any experience in working with
First of all i want to say that i have searched each and every
I seem to have looked everywhere for this. Here is the code i use
I have an appender that I only want the first X characters (for this
I have looked but haven't found a reasonable answer. I have a first TableViewController
I looked through all the similar questions that showed up before typing this, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.