Java 将GSON中的值放入android中的TextView

Java 将GSON中的值放入android中的TextView,java,android,textview,gson,Java,Android,Textview,Gson,我试图从GSON中的URL获取数据,并将这些值放入TextView。我正在使用ArrayList从GSON添加值。以下是处理该任务的AsyncTask代码: private class getData extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new Pr

我试图从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?*/

    }
}
私有类getData扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(CardViewExpandCollapse.this);
setMessage(“请稍候…”);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的空位背景(空位…空位){
HttpHandler HttpHandler=新的HttpHandler();
字符串s=httpHandler.makeServiceCall(mUrl);
Gson Gson=新的Gson();
AccountDetail[]accountDetails=gson.fromJson(s,AccountDetail[].class);
ArrayList details=新的ArrayList(Arrays.asList(accountDetails));
HashMap accDetails=新建HashMap();
字符串地址=details.get(0.getRow().getBillToAddress();
Double balance=details.get(0.getRow().getTotalAccountBalance();
Double invoices=details.get(0.getRow().getTotalOpenInvoicesValue();
Log.e(标签,“来自URL的响应”+s);
Log.e(标签,“地址为”+地址);
Log.e(标签,“余额为”+余额);
日志e(标签“发票为”+发票);
accDetails.put(“billToAddress”,地址);
accDetails.put(“总账户余额”,余额+“\tUSD”);
accDetails.put(“totalOpenInvoicesValue”,发票+“\tUSD”);
hashMap.add(accDetails);//大小=3
返回null;
}
@凌驾
受保护的void onPostExecute(void值){
super.onPostExecute(值);
if(pDialog.isShowing()){
pDialog.disclose();
}
/*如何将哈希映射中的值添加到此处的TextView*/
}
}
doInBackground()
方法中,我从Gson获取数据并将其添加到哈希映射中。
onPostExecute()
方法中,我想将
GSON
中的值添加到
TextView
。如何将
hashMap
中的值放入
TextView

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

更改
受保护的Void doInBackground(Void…voids)

受保护的HashMap doInBackground(Void…voids)

然后在该函数中返回
returnhashmap

下一步更改受保护的void onPostExecute(void值)

受保护的void onPostExecute(HashMap HashMap)


最后,将类声明更改为
私有类getData extends AsyncTask

您的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");
    }
};
私有类GetData扩展异步任务(){
@凌驾
受保护的HashMap doInBackground(无效…无效){
HashMap accDetails=新建HashMap();
//你的GSON解码
返回详细信息;
}
@凌驾
受保护的void onPostExecute(HashMap accDetails){
//在文本视图中显示文本,即。
//yourTextView.setText=accDetails.get(“billToAddress”);
}
};

如果我将
受保护的Void doInBackground(Void…voids)
更改为
受保护的HashMap doInBackground(Void…voids)
,然后我得到了这个错误:*
doInBackground
在我的类中与
Android.os.AsycTask
doInBackground
冲突,试图使用不兼容的返回类型**对,我忘了添加需要将类声明更改为
private-class-getData-extenses-AsyncTask
只需执行
myTextView.xt即可(hashMap.get(“MyValue”);
。您看到的异常是什么?确实如此;)如果您觉得合适,请接受我的回答