0

私が抱えている問題はちょっと奇妙です。JSONのprintstackは、メソッドString doInBackground(String... args)と同じようにテーブル内のすべての要素を正しく表示しています問題はpostExecuteメソッドにありますそれはリストビューのすべての要素に同じ要素を表示しています「テーブルの行の最後の要素を正確に言うと、誰かが私が間違ったことを教えてくれますか」ありがとうございました

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

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

        /**
         * 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.getJSONFromUrl(balanceURL, "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
                    amount = json.getJSONArray(TAG_BALANCE);

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

                        // Storing each json item in variable
                        String amount = c.getString(TAG_AMOUNT);
                        String createdat = c.getString(TAG_CREATEDAT);
                        //String userid = c.getString(TAG_USERID);
                        // adding each child node to HashMap key => value
                        map.put(TAG_AMOUNT, amount);
                        map.put(TAG_CREATEDAT, createdat);
                        //map.put(TAG_USERID , userid);
                        // adding HashList to ArrayList
                        amountlist.add(map);


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

            return null;
        }


        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {

                    ListAdapter adapter = new SimpleAdapter(
                            ProDentActivity.this, amountlist,
                            R.layout.list_item, new String[] { TAG_AMOUNT,TAG_CREATEDAT},
                            new int[] { R.id.amount, R.id.createdat });
                    // updating listview
                    setListAdapter(adapter);


                }
            });

        }

    }

JSON:

{"balance":[{"amount":"50000","created_at":"2012-12-15 02:39:13"},{"amount":"50000","created_at":"2012-12-16 15:29:03"},{"amount":"30000","created_at":"2012-12-17 19:38:07"}],"success":1}

...そしてメソッドは戻ります

12-18 02:29:05.797: D/All Products:(885): {"success":1,"balance":[{"amount":"50000","created_at":"2012-12-15 02:39:13"},{"amount":"50000","created_at":"2012-12-16 15:29:03"},{"amount":"30000","created_at":"2012-12-17 19:38:07"}]}
4

1 に答える 1

0

問題が修正されました。私が間違っていたことを発見しました。問題は、メソッド protected String doInBackground(String... args) でハッシュ マップを定義しなかったことです。

私はリストアクティビティでそれを定義していました。お時間をいただきありがとうございます。次のコードを古いコードに置き換えるのを手伝ってください。

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

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

                    // Storing each json item in variable
                    String amount = c.getString(TAG_AMOUNT);
                    String createdat = c.getString(TAG_CREATEDAT);
                    //String userid = c.getString(TAG_USERID);
                    // adding each child node to HashMap key => value

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

                    map.put(TAG_AMOUNT, amount);
                    map.put(TAG_CREATEDAT, createdat);
                    //map.put(TAG_USERID , userid);
                    // adding HashList to ArrayList
                    amountlist.add(map);


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

        return null;
    }
于 2012-12-18T19:39:05.847 に答える