gpt4 book ai didi

java - 将GSON中的值放入android中的TextView

转载 作者:行者123 更新时间:2023-11-29 04:22:30 25 4
gpt4 key购买 nike

我正在尝试从 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 获取数据并将其添加到 HashMap 中。在 onPostExecute()方法,我想从 GSON 添加值至 TextView .如何将值放入 hashMapTextView

最佳答案

您的 AsyncTask 的 doInBackground() 方法应该返回一个 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");
}
};

关于java - 将GSON中的值放入android中的TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48230784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com