简体   繁体   中英

How to get total items count in specific index of List<object>?

I have used retrofit 2 for request data from API and have some JSON response format like below.

{
     result: [
        {
           kolom1: "PI 0",
           kolom2: "PV 0",
           kolom3: "TP 0",
           kolom4: "10",
           kolom5: "20",
           kolom6: "30",
           kolom7: "FK 0",
           kolom8: "CA 0"
        },
        {
           kolom1: "PI 1",
           kolom2: "PV 1",
           kolom3: "TP 1",
           kolom4: "10",
           kolom5: "20",
    }

This is my model class.

public class DetailKPIModel {
    private String kolom1, kolom2, kolom3, kolom4, kolom5, kolom6, kolom7, kolom8;

    public String getKolom1() {
        return kolom1;
    }
}

This is my response model class.

public class ResponseModel {
    private List<DetailKPIModel> resultDetail;

    public List<DetailKPIModel> getResultDetail() {
        return resultDetail;
    }
}

This is my onResponse method.

@Override
public void onResponse(Call<ResponseModel> call, Response<ResponseModel> response) {
   //get total items in result index 0
}

I want to know size of

response.body().getResultDetail().get(0)

How to get total of items in every index of result? My expected results are like these:

  • Total result index 0 is 8
  • Total result index 1 is 5

try this way on your model class

    public class DetailKPIModel {
            private String kolom1, kolom2, kolom3, kolom4, kolom5, kolom6, kolom7, kolom8;

            public String getKolom1() {
                return kolom1;
            }

            public int getCount(String value){
                int count =0;
                if(value.equalsIgnoreCase(kolom1.substring(kolom1.length()-1))){
                    count++;
                }
                .
                .
                .

                if(value.equalsIgnoreCase(kolom8.substring(kolom8.length()-1))){
                    count++;
                }
return count;
            }
        }

pass the value 0 or 1 in getCount() method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM