0

我遇到的问题有点奇怪 JSON 的 printstack 正确显示表中的所有元素的方式应该与方法 String doInBackground(String... args) 相同那是在列表视图的所有元素中显示相同的元素“确切地说,表格行中的最后一个元素可以有人告诉我我做错了什么”谢谢你的时间,你会发现我正在谈论的课程谢谢你

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) 中定义我的哈希映射

我在 listactivity 中定义它,感谢您抽出宝贵的时间并帮助将下面的代码替换为旧的 try { // 检查 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

                   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 回答