0

请您帮我找出以下代码的问题所在!当从微调器中选择的项目没有发生

即:没有看到 spinner.setOnItemSelectedListener 的主体!

代码 :

 package com.example.spinner;


 import java.util.ArrayList;
 import java.util.List; 
 import org.json.JSONArray;
 import org.json.JSONObject;
 import android.os.Bundle;
 import android.app.Activity;
 import android.util.Log; 
 import android.view.View;
   import android.widget.AdapterView;
   import android.widget.ArrayAdapter;
   import android.widget.Spinner;
   import android.widget.Toast;
   import android.widget.AdapterView.OnItemSelectedListener;

   import java.util.HashMap; 
   import org.apache.http.NameValuePair; 
   import org.json.JSONException; 


   import android.annotation.SuppressLint; 
   import android.content.Intent;
   import android.os.AsyncTask;  
   public class MainActivity extends Activity {


        // Creating JSON Parser object
        JSONParser jParser = new JSONParser();

        ArrayList<HashMap<String, String>> productsList;


        // url to get all products list
        private static String url_all_products = "http://10.0.2.2/RPM-connect/get_all_patients.php";

        // JSON Node names
        private static final String TAG_SUCCESS = "success";
        private static final String TAG_DOCTOR = "products";

        private static final String TAG_ID = "id";
        private static final String TAG_F_NAME = "F_name";
        private static final String TAG_S_NAME = "S_name";
        private static final String TAG_L_NAME = "L_name"; 

        // products JSONArray
        JSONArray doctor = null;

        ArrayList<String> spinnerArray ;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            // Hashmap for ListView
            productsList = new ArrayList<HashMap<String, String>>();

            spinnerArray = new ArrayList<String>();

            // Loading products in Background Thread
            new LoadAllProducts().execute();


              Spinner spinner = new Spinner(this);
                              //ArrayAdapter <HashMap<String, String>> spinnerArrayAdapter = new ArrayAdapter<HashMap<String, String>>(

              ArrayAdapter <String> spinnerArrayAdapter = new ArrayAdapter <String >(
                        this, android.R.layout.simple_spinner_item,  spinnerArray );
                spinnerArrayAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );

                spinner = (Spinner) findViewById( R.id.spinner1 );
                spinner.setAdapter(spinnerArrayAdapter);
                spinner.setOnItemSelectedListener(new OnItemSelectedListener(){

                    @Override
                    public void onItemSelected(AdapterView<?> arg0,
                            View arg1, int arg2, long arg3) {


                        Toast.makeText(MainActivity.this, "You have selected : "  , 
                                Toast.LENGTH_SHORT).show();
                    }

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

                    }

                } );
            }




        // Response from Edit Product Activity
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            // if result code 100
            if (resultCode == 100) { 
                Intent intent = getIntent();
                finish();
                startActivity(intent);
            }

        }

        /**
         * Background Async Task to Load all product by making HTTP Request
         * */
        @SuppressLint("NewApi")
        class LoadAllProducts extends AsyncTask<String, String, String> {

            /**
             * getting All products from url
             * */
            protected String doInBackground(String... args) {
                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                // getting JSON string from URL
                JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);

                // Check your log cat for JSON reponse
                Log.d("All Products: ", json.toString());

                try {
                    // Checking for SUCCESS TAG
                    int success = json.getInt(TAG_SUCCESS);

                    if (success == 1) {
                        // products found
                        // Getting Array of Products
                        doctor = json.getJSONArray(TAG_DOCTOR);



                        // looping through All Products
                        for (int i = 0; i < doctor.length(); i++) {
                            JSONObject c = doctor.getJSONObject(i);


                            // Storing each json item in variable
                            String ID = c.getString(TAG_ID);
                            String F_name = c.getString(TAG_F_NAME);
                            String S_name = c.getString(TAG_S_NAME);
                            String L_name = c.getString(TAG_L_NAME);



                            // creating new HashMap
                            HashMap<String, String> map = new HashMap<String, String>();

                            // adding each child node to HashMap key => value
                            map.put(TAG_ID, ID);
                            map.put(TAG_F_NAME, F_name);
                            map.put(TAG_S_NAME, S_name);
                            map.put(TAG_L_NAME, L_name);



                            // adding HashList to ArrayList
                            productsList.add(map);


                            Log.d("Before temp", productsList.toString());

                            spinnerArray.add(F_name + " " +  L_name);
                            // temp[i]=  F_name+" "+L_name ; 
                            Log.d("TT",spinnerArray.get(i) );
                        }



                    } else {
                        // no products found
                        // Launch Add New product Activity
                        Intent i = new Intent(getApplicationContext(),
                                MainActivity.class);
                        // Closing all previous activities
                        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(i);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                return null;
            }


        }


        } 
4

2 回答 2

1

在设置适配器之前,您ArrayList可能不会被填充。在你那里AsyncTask实现并调用你的适配器。此外,您创建一个新的然后使用您的 xml 中存在的一个onPostExecute()AsyncTasknotifyDataSetChanged()Spinner

 Spinner spinner = new Spinner(this);

这不是必需的。Spinner就这样声明你的

public class MainActivity extends Activity {
 Spinner spinner;

然后像你稍后做的那样从 xml 初始化它

spinner = (Spinner) findViewById( R.id.spinner1 );

notifyDataSetChanged() 文档

于 2013-04-22T22:28:43.540 回答
1

完成将项目加载到SpinnerAdapter内部后AsyncTask,您需要告诉Adapter刷新自身以加载新值。

这可以通过调用来完成:spinnerArrayAdapter.notifyDataSetChanged()

因为AsyncTask在另一个线程上运行,您将需要实现

@Override
public void onPostExecute(String result) {
    spinnerArrayAdapter.notifyDataSetChanged()
}

并调用spinnerArrayAdapter.notifyDataSetChanged()它的内部,如上所示。 onPostExecute并且onPreExecute在主线程上运行,因此它们可以Views在 UI 线程上进行更改。

于 2013-04-22T22:26:42.270 回答