2013-11-23 79 views
0

我試圖從mysql數據庫中檢索一些數據以便用該數據填充微調器。我設法將數據插入微調數據庫到MySQL數據庫,但當我嘗試檢索回它顯示在Logcat中,微調框爲空! 我在網上搜索,但我找不到解決我的問題。如何使用mysql數據庫中的數據填充Android微調器

這裏是從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) 

能不能請你幫我解決這個問題。 謝謝。

+0

取值並顯示它沒有u得到微調ID:像 微調S =(微調)findviewbyid(R.id.Spinner); – Dev

+0

@iCodeAtAndroid感謝您的回覆,是的,我做到了:txtdrop_down =(Spinner)findViewById(R.id.spinner_faculty); – Mack

+0

嗨,我設法解決與Android微調問題。問題是我沒有爲spinner聲明setOnItemSelectedListener,這就是爲什麼它顯示nullpointerexception。感謝你們。 – Mack

回答

0

Android Hive Link

上述鏈接具有在其又被示出爲在微調DB,運行時間和存儲需要從用戶值的例子。它會告訴你如何從數據庫中微調

+0

感謝您的回覆,您提供的鏈接涉及使用Sqlite數據庫,但我的問題在於MySQL。 – Mack

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

    } 
相關問題