我是Android的新手,我不了解自定义ListView布局的工作方式。 我尝试在其中打印一些取自JSON文件的数据,但它会覆盖数据直到最后一个数据,这是因为它仅使用1行,而不创建多行。我不了解如何创建多行。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vInflater = getLayoutInflater();
final View myLayout = vInflater.inflate(R.layout.my_layout, null);
mTextName = myLayout.findViewById(R.id.textName);
mTextSurname = myLayout.findViewById(R.id.textSurname);
mProfilePicture = myLayout.findViewById(R.id.profilePicture);
mDialog = new ProgressDialog(this);
mDialog.setMessage("Sto Caricando");
mDialog.show();
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://reqres.in/api/users?page=2")
.get()
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
mDialog.cancel();
Log.e("asd", "OnFailure: richiesta fallita", e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
mDialog.cancel();
final String vResponse = response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
JSONObject vObj = new JSONObject(vResponse);
JSONArray vArray = vObj.getJSONArray("data");
for (int i = 0; i < vArray.length(); i++) {
JSONObject vSingleObj = vArray.getJSONObject(i);
mTextName.setText(vSingleObj.getString("first_name"));
mTextSurname.setText(vSingleObj.getString("last_name"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
;
});
}
}
});
}
我认为我每次使用.setText()
方法时都要调用自定义ListView的新行,但是我不知道如何