gpt4 book ai didi

java - 如何使用 Retrofit 获取 ArrayList 中的最后一个元素

转载 作者:行者123 更新时间:2023-12-02 09:42:58 24 4
gpt4 key购买 nike

我正在使用改造从服务器获取数据。我想从 arrayList 中获取最后一个元素,该怎么做?

我已经阅读了如何从这个问题中获取最后一个元素 How to get the last value of an ArrayList但这行不通。

这是我的 JSON 对象 http://ipsta.tech/koor.php

这是我的适配器类

    private Context context;

public KinectAdapter(List<Koor> koorList, Context context) {
this.koorList = Collections.singletonList(koorList);
this.context = context;

}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.kinect,parent,false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.x.setText(String.valueOf(koorList.get(position).getX()));
holder.y.setText(String.valueOf(koorList.get(position).getY()));
}

@Override
public int getItemCount() {
return koorList.size();
}

public static class ViewHolder extends RecyclerView.ViewHolder{

public TextView x,y;

public ViewHolder(View itemView) {
super(itemView);

x = (TextView) itemView.findViewById(R.id.tvX);
y = (TextView) itemView.findViewById(R.id.tvY);
}
}

这是我的课

        koorArrayList = new ArrayList<>();
progressDialog.setTitle("Displaying data");
progressDialog.setMessage("Loading ...");
progressDialog.show();

ApiInterface apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
Call<List<Koor>> call = apiInterface.getAllDataKoor();
call.enqueue(new Callback<List<Koor>>() {
@Override
public void onResponse(Call<List<Koor>> call, Response<List<Koor>> response) {
koorArrayList = response.body();
Koor e = koorArrayList.get(koorArrayList.size() - 1);
KinectAdapter foodAdapter = new KinectAdapter(e, Kinect.this);
recyclerView.setLayoutManager(new LinearLayoutManager(Kinect.this));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(foodAdapter);
progressDialog.dismiss();
}

@Override
public void onFailure(Call<List<Koor>> call, Throwable t) {
Toast.makeText(Kinect.this, t.getMessage(), Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}

这是我的 Koor 类(class):

public class Koor {
@SerializedName("x")
private float x;
@SerializedName("y")
private float y;

public Koor(float x, float y, String time) {
this.x = x;
this.y = y;
}

public float getX() {
return x;
}

public float getY() {
return y;
}

而且我的输出 JSONobject 是 float 类型。

Where should i place list.get(list.size()-1; ?

最佳答案

在改造调用的onResponse中使用它

call.enqueue(new Callback<List<Koor>>() {
@Override
public void onResponse(Call<List<Koor>> call, Response<List<Koor>> response)
{
koorArrayList = response.body();
Koor mKoor = koorArrayList.get(koorArrayList.size() - 1);
}

..............

mKoor中,你有Koor列表中的最后一个Koor对象

关于java - 如何使用 Retrofit 获取 ArrayList 中的最后一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56910833/

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