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

我已经读过如何从这个quet 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();
            }


这是我的库尔课程:

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类型。

我应该在哪里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对象

10-08 07:04