我从后端以对象数组的形式获取响应,并将响应存储在数组列表中,我想从ArrayList中检索特定的字段值并将其附加到listview。
这是我的代码:
Retrofit retrofit = builder.build();
APIService apiservice = retrofit.create(APIService.class);
Call<PostData> call = apiservice.transactiondetails(UID);
call.enqueue(new Callback<PostData>() {
@Override
public void onResponse(@NonNull Call<PostData> call, @NonNull Response<PostData> response) {
if (response.code() == 200) { // this is the case for success
if (response.isSuccessful()) {
response.body();
// ArrayList dp = new HashMap<String, Array>();
// dp = Objects.requireNonNull(response.body()).getToken1();
ArrayList dp = Objects.requireNonNull(response.body()).getToken1();
Log.d(TAG, String.valueOf(dp));
System.out.println("hash map is" + dp);
Intent i = new Intent(deyaPay.this, TransactionDetails.class);
i.putExtra("Transactiondetails",dp);
startActivity(i);
Toast.makeText(deyaPay.this, "success" + dp, Toast.LENGTH_SHORT).show();
//Toast.makeText(getApplicationContext(), dp, Toast.LENGTH_LONG).show();
}
}
and this is class for getting the response from backend:
public class PostData {
@SerializedName("Token")
@Expose
private String token;
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
@SerializedName("Details")
@Expose
private Map<String,Array> token1;
public Map<String, Array> getToken1() {
return token1;
}
public void setToken1(Map<String,Array> token1) {
this.token1 = token1;
}
}
那么如何将响应附加到listview上,这是我来自后端的响应:
[
{
Amount = 4;
TransactionID = deyaPay;
method = "Money Sent using deyaPay";
timestamp = "2018-09-03T06:01:41.306Z";
},
{
Amount = 2;
Token = "tok_1D69olLMFuWYhKXrAqwmTprz";
TransactionID = "ch_1D69omLMFuWYhKXrYpbNEtGH";
method = "Requested by";
timestamp = "2018-09-03T04:37:40.147Z";
},
{
Amount = 5000;
TransactionID = deyaPay;
method = "Money Sent using deyaPay";
timestamp = "2018-09-03T04:01:00.565Z";
},
{
Amount = 50;
TransactionID = deyaPay;
method = "Money Sent using deyaPay";
timestamp = "2018-09-03T03:59:01.708Z";
},
{
Amount = 2;
TransactionID = deyaPay;
method = "Money Sent using deyaPay";
timestamp = "2018-09-03T03:58:26.964Z";
},
{
Amount = 1;
Token = "tok_1D68nBLMFuWYhKXrGM2AN2IF";
method = "Add Money to deyaPay account using credit card/debit card";
timestamp = "2018-09-03T03:31:58.008Z";
},
{
Amount = 5;
Token = "tok_1D68cpLMFuWYhKXrM7oc2X7j";
method = "Add Money to deyaPay account using credit card/debit card";
timestamp = "2018-09-03T03:21:16.281Z";
},
{
Amount = 2;
Token = "tok_1D5pE1LMFuWYhKXr9BMvQ1dm";
method = "Add Money to deyaPay account using credit card/debit card";
timestamp = "2018-09-02T06:38:22.188Z";
},
{
Amount = 2;
Token = "tok_1D5pCxLMFuWYhKXrJoRcbN8o";
method = "Add Money to deyaPay account using credit card/debit card";
timestamp = "2018-09-02T06:37:16.393Z";
}]
答案 0 :(得分:0)
您的json应该看起来像这样:
[{
"Amount": "4",
"Token": "tok_1D69olLMFuWYhKXrAqwmTprz",
"TransactionID": "deyaPay",
"method": "Money Sent using deyaPay",
"timestamp": "2018-09-03T06:01:41.306Z"
},
{
"Amount": "4",
"Token": "tok_1D69olLMFuWYhKXrAqwmTprz",
"TransactionID": "deyaPay",
"method": "Money Sent using deyaPay",
"timestamp": "2018-09-03T06:01:41.306Z"
},
{
"Amount": "4",
"Token": "tok_1D69olLMFuWYhKXrAqwmTprz",
"TransactionID": "deyaPay",
"method": "Money Sent using deyaPay",
"timestamp": "2018-09-03T06:01:41.306Z"
},
{
"Amount": "4",
"Token": "tok_1D69olLMFuWYhKXrAqwmTprz",
"TransactionID": "deyaPay",
"method": "Money Sent using deyaPay",
"timestamp": "2018-09-03T06:01:41.306Z"
},
{
"Amount": "4",
"Token": "tok_1D69olLMFuWYhKXrAqwmTprz",
"TransactionID": "deyaPay",
"method": "Money Sent using deyaPay",
"timestamp": "2018-09-03T06:01:41.306Z"
},
{
"Amount": "4",
"Token": "tok_1D69olLMFuWYhKXrAqwmTprz",
"TransactionID": "deyaPay",
"method": "Money Sent using deyaPay",
"timestamp": "2018-09-03T06:01:41.306Z"
},
{
"Amount": "4",
"Token": "tok_1D69olLMFuWYhKXrAqwmTprz",
"TransactionID": "deyaPay",
"method": "Money Sent using deyaPay",
"timestamp": "2018-09-03T06:01:41.306Z"
},
{
"Amount": "4",
"Token": "tok_1D69olLMFuWYhKXrAqwmTprz",
"TransactionID": "deyaPay",
"method": "Money Sent using deyaPay",
"timestamp": "2018-09-03T06:01:41.306Z"
},
{
"Amount": "4",
"Token": "tok_1D69olLMFuWYhKXrAqwmTprz",
"TransactionID": "deyaPay",
"method": "Money Sent using deyaPay",
"timestamp": "2018-09-03T06:01:41.306Z"
}
]
您的响应应该是一个对象数组。那就是您得到的错误,您尝试在响应为数组时解析一个对象。
在列表视图适配器中,将文本设置为带有arraylist中值的项目:
amount.setText(data.get(position).getAmount());
token.setText(data.get(position).getToken());
在这里,我为您提供一个示例,说明如何使用您需要的自定义适配器来实现列表视图:https://medium.com/mindorks/custom-array-adapters-made-easy-b6c4930560dd