In my android app I have a spinner that I initialize with some categories
public void loadData()
{
    Thread t=new Thread(new Runnable()
    {
        @Override
        public void run()
        {
            try
            {
                String link="http://"+Static.host+"/allcategories/?format=json";
                String json="";
                URL url = new URL(link);
                BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                String line;
                while ((line = in.readLine()) != null)
                {
                    json += line;
                }
                in.close();
                JSONArray elements=new JSONArray(json);
                for (int i = 0; i < elements.length(); i++)
                {
                    JSONObject category=elements.getJSONObject(i);
                    spinnerArray.add(category.getString("title"));
                }
                runOnUiThread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        spinnerAdapter.notifyDataSetChanged();
                    }
                });
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    });
    t.start();
    try
    {
        t.join();
    }
    catch (Exception e)
    {
        Toast.makeText(MainActivity.this,e.getMessage(),Toast.LENGTH_LONG).show();
    }
}
I created a function that will search articles based on the selected category
public void getArticlesByCategory(String category)
{
    // I search articles and I display them in a listview
}
Then I created an event listener for the spinner
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
    {
        getArticlesByCategory(spinner.getSelectedItem().toString());
    }
    @Override
    public void onNothingSelected(AdapterView<?> adapterView)
    {
    }
});
Finally I call loadData in onCreate
loadData();
The problem is that the event is triggered in the initialization of the spinner, which causes getArticlesByCategory to be called every time a new element is added.
I would like to know how I can trigger the event only when I manually select from the spinner.
 
     
     
     
    