spinner setOnItemSelectedListener不起作用

时间:2014-04-03 17:47:20

标签: spinner

我有一个微调器,我从我的数据库动态放置它的项目,但问题是我无法通过方法setOnItemSelectedListener

知道选择了哪个项目

这是我的java代码:

public class Choix extends Activity {
JSONArray ja1 = null;

List<String> list = new ArrayList<String>();
ArrayAdapter<String> dataAdapter;
Spinner spinner;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.choix_espace); 
    spinner = (Spinner) findViewById(R.id.spinner); 
    liste_ecoles k = new liste_ecoles();
    k.execute();

  dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, list);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(dataAdapter);


    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
        // TODO Auto-generated method stub
            Toast.makeText(getBaseContext(), ""+arg2, Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

    }
private class liste_ecoles extends AsyncTask<String, Integer, Object> {
    String ch1="";

    @Override
    protected Object doInBackground(String... params) {

        JSONArray ja = null;


        try {

            URL twitter = new URL("...");
            URLConnection tc = twitter.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    tc.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                ja = new JSONArray(line);
            }
        } catch (Exception e) {
        }

        return ja;

    }
    @Override
    protected void onPostExecute(Object resultat) {
        JSONArray ja = (JSONArray) resultat;
        if (resultat != null) {

            try {

                for (int i = 0; i < ja.length(); i++) {

                    JSONObject jo1 = null;
                    jo1 = ja.getJSONObject(i);
                    ch1 = jo1.getString("nom_ecole"); 
                    list.add(ch1);


                }


        }
        catch (Exception e) {
            }
        }
        }
        }   
    }

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我解决了我的问题;我刚刚添加了&#34; dataAdapter.notifyDataSetChanged(); &#34;在我的微调器上添加项目后