2013-08-22 95 views
0

我有來自listview的數據,我想將它保存在我的數據庫中,這是我的代碼;將數據從列表視圖保存到數據庫

listResep = (ListView) findViewById(R.id.listResep); 
int leng = listResep.getCount(); 
for(int i = 0; i < leng; i++) { 
    resep = listResep.getItemAtPosition(i).toString(); 
    new inputResep().execute(); 
} 

and for inputResep;

class inputResep extends AsyncTask<String, String, String> { 

    /** 
    * Before starting background thread Show Progress Dialog 
    * */ 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(InputRM.this); 
     pDialog.setMessage("save Resep.."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     pDialog.show(); 
    } 
    /** 
    * Creating product 
    * */ 
    protected String doInBackground(String... args) { 
     // Building Parameters 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("rm", rm)); 
     params.add(new BasicNameValuePair("noregis", noregis)); 
     params.add(new BasicNameValuePair("tanggal", tglinput)); 
     params.add(new BasicNameValuePair("pukul", pukul)); 
     params.add(new BasicNameValuePair("ruper", ruper)); 
     params.add(new BasicNameValuePair("kelas", kelas)); 
     params.add(new BasicNameValuePair("profesi", profesi)); 
     params.add(new BasicNameValuePair("kajian", resep)); 
     params.add(new BasicNameValuePair("id_user", id_user)); 
     params.add(new BasicNameValuePair("tglsave", tglsave)); 
     // getting JSON Object 
     // Note that create product url accepts POST method 
     json = jsonParser.makeHttpRequest(URL_INPUT_RESEP,"POST", params); 

     // check log cat for response 
     Log.d("Create Response", json.toString()); 
     // check for success tag 
     try { 
      int success = json.getInt(TAG_SUCCESS); 
      Log.d("stat", success+""); 
      if (success > 0) { 
       // successfully created product 

      } else { 
       // failed to create product 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
      Log.d("test", "JSONException"+e.getMessage()); 
     } 
     return null; 
    } 
    /** 
    * After completing background task Dismiss the progress dialog 
    * **/ 
    protected void onPostExecute(String file_url) { 
     pDialog.dismiss(); 
    } 
} 

但每次我試着輸入resep時間,但挽救的ListView儘可能在列表視圖,前數據最後的數據= listview: data1, data2, data3; save: data3, data3, data3;

+3

我想你應該把你的asynctask移出for循環。 – Raghunandan

+0

爲什麼在循環內多次執行異步任務? –

+0

非常錯誤的使用'AsyncTask',你應該做的是利用'AsyncTask'的Param部分。生成Params並將它們傳入你的'AsyncTask'並在'doInBackground()'中訪問它們,然後作爲一個整體上傳你的數據,或者可能在chuknks中。順便說一句,使用這種技術你需要執行一次'AsyncTask'。 –

回答

1

把整個事情在for循環中,獲得的大小在listview等全部視圖中,每個循環的數據將被上傳到數據庫中!

+1

這不是答案,而是一個評論! –

+0

如果問這個問題的人說它沒有幫助,那麼我甚至會刪除 – Exceptional

+0

@Exeptional它的工作原理!謝謝 – Darjeeling

0

儘量期運用在for循環

protected void onPostExecute(response response) { 
    pDialog.dismiss(); 


try { 
     int success = json.getInt(TAG_SUCCESS); 
     Log.d("stat", success+""); 
     if (success > 0) { 
      // successfully created product 

     } else { 
      // failed to create product 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
     Log.d("test", "JSONException"+e.getMessage()); 
    } 
} 
2

看着@Exeptional答案後,我這是怎麼對付它這一個在postexecute;

int leng = listResep.getCount(); 
    Log.d("jml resep", ""+leng); 
    for(int i = 0; i < leng; i++) { 
     resep = listResep.getItemAtPosition(i).toString(); 

     Log.d("save resep"+i, resep); 
//  new inputResep().execute(); 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("rm", rm)); 
     params.add(new BasicNameValuePair("noregis", noregis)); 
     params.add(new BasicNameValuePair("tanggal", tglinput)); 
     params.add(new BasicNameValuePair("pukul", pukul)); 
     params.add(new BasicNameValuePair("ruper", ruper)); 
     params.add(new BasicNameValuePair("kelas", kelas)); 
     params.add(new BasicNameValuePair("profesi", profesi)); 
     params.add(new BasicNameValuePair("kajian", resep)); 
     params.add(new BasicNameValuePair("id_user", id_user)); 
     params.add(new BasicNameValuePair("tglsave", tglsave)); 
     // getting JSON Object 
     // Note that create product url accepts POST method 
     json = jsonParser.makeHttpRequest(URL_INPUT_RESEP,"POST", params); 

     // check log cat for response 
     Log.d("Create Response", json.toString()); 
     // check for success tag 
     try { 
      int success = json.getInt(TAG_SUCCESS); 
      Log.d("stat", success+""); 
      if (success > 0) { 
       // successfully created product 

      } else { 
       // failed to create product 

      } 

     } catch (JSONException e) { 
      e.printStackTrace(); 
      Log.d("test", "JSONException"+e.getMessage()); 
     } 
    } 

編輯:

它正常工作時,我用模擬器試過,然後我試圖安裝在我的平板電腦,起初它工作正常,但是當我試圖挽救數據沒有奏效,所以我改變它,把循環放在asyncTask中:

class inputResep extends AsyncTask<String, String, String> { 
    /** 
    * Creating product 
    * */ 
    protected String doInBackground(String... args) { 
     int leng = listResep.getCount(); 
     Log.d("jml resep", ""+leng); 
     for(int i = 0; i < leng; i++) { 
      resep = listResep.getItemAtPosition(i).toString(); 
      // Building Parameters 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      params.add(new BasicNameValuePair("rm", rm)); 
      params.add(new BasicNameValuePair("noregis", noregis)); 
      params.add(new BasicNameValuePair("tanggal", tglinput)); 
      params.add(new BasicNameValuePair("pukul", pukul)); 
      params.add(new BasicNameValuePair("ruper", ruper)); 
      params.add(new BasicNameValuePair("kelas", kelas)); 
      params.add(new BasicNameValuePair("profesi", profesi)); 
      params.add(new BasicNameValuePair("kajian", resep)); 
      params.add(new BasicNameValuePair("id_user", id_user)); 
      params.add(new BasicNameValuePair("tglsave", tglsave)); 

      // getting JSON Object 
      // Note that create product url accepts POST method 
      json = jsonParser.makeHttpRequest(URL_INPUT_RESEP,"POST", params); 

      Log.d("testinput", "4"); 
      // check log cat for response 
      Log.d("Create Response", json.toString()); 
      // check for success tag 
      try { 
       int success = json.getInt(TAG_SUCCESS); 
       Log.d("stat", success+""); 
       if (success > 0) { 
        // successfully created product 
        Log.d("test", "berhasil simpan resep"); 

       } else { 
        // failed to create product 

        Log.d("test", "failed to create product "); 
       } 

       Log.d("test", "in try"); 

      } catch (JSONException e) { 
       e.printStackTrace(); 
       Log.d("test", "JSONException"+e.getMessage()); 
      } 
     } 
     return null; 
    } 
}