1

我有来自 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();
}

和输入Resep;

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 中的数据一样多,例如 =listview: data1, data2, data3; save: data3, data3, data3;

4

3 回答 3

2

将整个事情放在一个 FOR LOOP 中,在 listview 中获取总视图的大小,因此对于每个循环,数据都将上传到数据库中!!

于 2013-08-22T04:41:47.467 回答
2

在查看@Exeptional 答案后,这就是我的处理方式;

    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;
    }
}
于 2013-08-22T05:23:59.460 回答
0

尝试在 forloop 的 postexecute 中使用这个

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());
    }
}
于 2013-08-22T05:01:39.320 回答