如何使用mysql数据库中的数据填充Android微调器

时间:2013-11-23 04:40:01

标签: android mysql android-spinner

我正在尝试从mysql数据库中检索一些数据,以便用该数据填充微调器。我设法将数据插入spinner到mysql数据库但是当我尝试检索它时,它在Logcat中显示spinner为null! 我在网上搜索但我无法找到解决问题的方法。

这是从mysql数据库中检索数据的java代码:

private static final String TAG_FACULTY_NAME = "faculty_name";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.editactivity); //Connected to XML file for the interface

    // save button
    btnSave = (Button) findViewById(R.id.lecturerSave);

    //Sharedpreference to get lecturer details
    SharedPreferences sp = getSharedPreferences("logindetails", 0);

    uid = sp.getString("uid", "-1");

    // Getting complete lecturer details in background thread
    new GetLecturerDetails().execute();


    // save button click event
    btnSave.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // starting background task to update lecturer account
                new SaveLecturerDetails().execute();

        }
    });

}

/**
 * Background Async Task to Get complete lecturer account details
 * */
class GetLecturerDetails extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(EditActivity.this);
        pDialog.setMessage("Loading. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    /**
     * Getting lecturer account details in background thread
     * */
    protected String doInBackground(String... params) {

         //updating UI from Background Thread
       // runOnUiThread(new Runnable() {
         //   public void run() {
                // Check for success tag
                int success;

                try {
                    // Building Parameters
                    List<NameValuePair> params1 = new ArrayList<NameValuePair>();
                    params1.add(new BasicNameValuePair("uid", uid));

                    // getting lecturer account details by making HTTP request
                    // Note that lecturer account details url will use GET request
                    JSONObject json = jsonParser.makeHttpRequest(
                            url_lecturer_details, "GET", params1);

                    // check your log for json response
                    Log.d("Single Product Details", json.toString());

                    // json success tag
                    success = json.getInt(TAG_SUCCESS);
                    if (success == 1) {
                        // successfully received lecturer account details
                        JSONArray productObj = json
                                .getJSONArray(TAG_PRODUCT); // JSON Array

                        // get first lecturer account object from JSON Array
                        final JSONObject product = productObj.getJSONObject(0);

                        // lecturer account with this uid found
                        // Edit Text
                        runOnUiThread(new Runnable() {
                               public void run() {

                        txtdrop_down = (Spinner) findViewById(R.id.spinner_faculty);


                        // display lecturer data in EditText


                        try
                        {

                        txtdrop_down.setTag(product.getString(TAG_FACULTY_NAME));
                        }catch (JSONException e)
                        {
                            e.printStackTrace();
                        }
                               }
                        });

                    } else{
                        // lecturer account with uid not found
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


        return null;
            }



    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog once got all details
        pDialog.dismiss();


    }
}

这是我从Logcat获得的错误:

11-23 12:25:02.543: E/AndroidRuntime(2499): FATAL EXCEPTION: main
11-23 12:25:02.543: E/AndroidRuntime(2499): java.lang.NullPointerException
11-23 12:25:02.543: E/AndroidRuntime(2499):     at android.widget.Spinner$DialogPopup.show(Spinner.java:935)
11-23 12:25:02.543: E/AndroidRuntime(2499):     at android.widget.Spinner.performClick(Spinner.java:614)
11-23 12:25:02.543: E/AndroidRuntime(2499):     at android.view.View$PerformClick.run(View.java:17721)
11-23 12:25:02.543: E/AndroidRuntime(2499):     at android.os.Handler.handleCallback(Handler.java:730)
11-23 12:25:02.543: E/AndroidRuntime(2499):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-23 12:25:02.543: E/AndroidRuntime(2499):     at android.os.Looper.loop(Looper.java:137)
11-23 12:25:02.543: E/AndroidRuntime(2499):     at android.app.ActivityThread.main(ActivityThread.java:5103)
11-23 12:25:02.543: E/AndroidRuntime(2499):     at java.lang.reflect.Method.invokeNative(Native Method)
11-23 12:25:02.543: E/AndroidRuntime(2499):     at java.lang.reflect.Method.invoke(Method.java:525)
11-23 12:25:02.543: E/AndroidRuntime(2499):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-23 12:25:02.543: E/AndroidRuntime(2499):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-23 12:25:02.543: E/AndroidRuntime(2499):     at dalvik.system.NativeStart.main(Native Method)
你能帮我解决这个问题吗? 谢谢。

2 个答案:

答案 0 :(得分:0)

Android Hive Link

上面的链接有一个示例,它在运行时从用户获取值并存储在DB中,然后显示在Spinner中。它将向您展示如何从DB中获取值并在Spinner中显示它

答案 1 :(得分:0)

class ItemUpdater extends AsyncTask<String, String, String>{
        private ProgressDialog Dialog = new ProgressDialog(MobileQDC_Booking_Screen.this);
        protected void onPreExecute() {
            Dialog.setMessage("Please wait...");
            Dialog.show();
            Dialog.setCancelable(false);
        }
        @Override
        protected String doInBackground(String... params) {


            try {
                DefaultHttpClient httpclient = new DefaultHttpClient();
                HttpGet httpget = new HttpGet("your web service url");
                HttpResponse response = httpclient.execute(httpget);
                BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                StringBuffer sb = new StringBuffer("");
                String line = "";
                String NL = System.getProperty("line.separator");
                while ((line = in.readLine()) != null) {
                    sb.append(line + NL);
                }
                in.close();
                String result = sb.toString();
                Log.v("My Response :: ", result);
                JSONObject jsonObject = new JSONObject(result);
                JSONArray resultArray = jsonObject.getJSONArray("Customer");
                for (int i = 0; i < resultArray.length(); i++) {
                    JSONObject c = resultArray.getJSONObject(i);
                    String sbs = c.getString("ItemName");
                    ItemsList.add(sbs);
                }
            } catch (Exception e) {

                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(String result) {
            SelectCloth.setAdapter(new ArrayAdapter<String>(MobileQDC_Booking_Screen.this, android.R.layout.simple_dropdown_item_1line, ItemsList));
            new ProcessUpdater().execute();             
            Dialog.dismiss();
        }

    }