将 GSON 中的值放入 android 中的 TextView

Putting values from GSON into TextView in android

我正在尝试从 GSON 中的 URL 获取数据并将这些值放入 TextView。我正在使用 ArrayList 添加来自 GSON 的值。这是处理该问题的 AsyncTask<> 代码:

private class getData extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(CardViewExpandCollapse.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected Void doInBackground(Void... voids) {

        HttpHandler httpHandler = new HttpHandler();

        String s = httpHandler.makeServiceCall(mUrl);

        Gson gson = new Gson();
        AccountDetail[] accountDetails = gson.fromJson(s, AccountDetail[].class);

        ArrayList<AccountDetail> details = new ArrayList<>(Arrays.asList(accountDetails));

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

        String address = details.get(0).getRow().getBillToAddress();

        Double balance = details.get(0).getRow().getTotalAccountBalance();

        Double invoices = details.get(0).getRow().getTotalOpenInvoicesValue();

        Log.e(Tag, "Response from URL " + s);

        Log.e(Tag, "Address is " + address);
        Log.e(Tag, "Balance is " + balance);
        Log.e(Tag, "Invoice is " + invoices);

        accDetails.put("billToAddress", address);
        accDetails.put("totalAccountBalance", balance + "\tUSD");
        accDetails.put("totalOpenInvoicesValue", invoices + "\tUSD");

        hashMap.add(accDetails); // size = 3

        return null;
    }

    @Override
    protected void onPostExecute(Void values) {
        super.onPostExecute(values);
        if (pDialog.isShowing()) {
            pDialog.dismiss();
        }

        /*How can I add values from hash map to TextView here?*/

    }
}

doInBackground() 方法中,我从 Gson 获取数据并将其添加到哈希映射中。 在 onPostExecute() 方法中,我想将 GSON 的值添加到 TextView。如何将 hashMap 中的值放入 TextView

您的 doInBackground 函数需要 return hashMap 变量才能在 onPostExecute 中访问它。

改变protected Void doInBackground(Void... voids)

protected HashMap<String, String> doInBackground(Void... voids)

然后在那个函数中让你的 return 成为 return hashMap;

下一步改变你的protected void onPostExecute(Void values)

protected void onPostExecute(HashMap<String, String> hashMap)

最后,将您的 class 声明更改为 private class getData extends AsyncTask<Void, Void, HashMap<String, String>>

您的 AsyncTask 的 doInBackground() 方法应该 return 一个 HashMap。

然后您的 onPostExecute() 方法将收到它并更新您的 TextView。

这是一个代码示例:

private class GetData extends AsyncTask<Void, Void, HashMap<String, String>>() {
    @Override
    protected HashMap<String, String> doInBackground(Void... voids) {
        HashMap<String, String> accDetails = new HashMap<String, String>();
            //Your GSON decoding
        return accDetails ;
    }

    @Override
    protected void onPostExecute(HashMap<String, String> accDetails ) {
       // Display the text in your TextView i.e.
       // yourTextView.setText = accDetails.get("billToAddress");
    }
};