如何在Android中获取列表数据的计数

时间:2017-08-22 06:12:04

标签: android list arraylist android-recyclerview

在我的应用程序中,我应该从服务器获取列表并显示到RecyclerView
我希望获胜者数量显示recyclerView项目 来自服务器的我的列表:

    "title": "Transporter",
      "awards": [
        {
          "description": "Best Performance by an Actor in a Motion Picture - Comedy or Musical",
          "year": 1998,
          "won": false,
          "id": 13769,
          "name": "Jackie Brown",
          "entityType": 1,
          "imageUrl": "example.com/thumb2-13769 Jackie Brown.jpg"
        },
        {
          "description": "Best Performance by an Actor in a Supporting Role in a Motion Picture",
          "year": 1997,
          "won": false,
          "id": 1353,
          "name": "A Time to Kill",
          "entityType": 1,
          "imageUrl": "http://example.com/thumb2-1353 A Time to Kill.jpg"
        },
        {
          "description": "Best Performance by an Actor in a Miniseries or Motion Picture Made for Television",
          "year": 1995,
          "won": true,
          "id": 86836,
          "name": "Against the Wall",
          "entityType": 1,
          "imageUrl": "http://example.com/thumb2-86836 Against the Wall 1994.jpg"
        },
        {
          "description": "Best Performance by an Actor in a Supporting Role in a Motion Picture",
          "year": 1995,
          "won": false,
          "id": 1378,
          "name": "Pulp Fiction",
          "entityType": 1,
          "imageUrl": "http://example.com/thumb2-1378 Pulp Fiction.jpg"
        }
      ]
    }
  ]
}

要连接到服务器,我使用Retrofit我的代码:

InterfaceApi api = ApiClient.getClient().create(InterfaceApi.class);
    Call<CelebrityAwardResponse> call = api.getCelebrityAward(celebrityAwardSendData);

    call.enqueue(new Callback<CelebrityAwardResponse>() {
        @Override
        public void onResponse(Call<CelebrityAwardResponse> call, Response<CelebrityAwardResponse> response) {
            if (response.body().getData() != null) {

        }

        @Override
        public void onFailure(Call<CelebrityAwardResponse> call, Throwable t) {
        }
    });

我怎么知道有多少数据“won”:true

请帮帮我,谢谢大家&lt; 3

4 个答案:

答案 0 :(得分:1)

从字符串响应数据中创建一个JSONArray对象,然后迭代它并获取计数:

int totalWins = 0;
String data = response.body().getData();
JSONArray jsonArray = new JSONArray(data);
for (int i=0; i < array.length(); i++) {
    JSONObject row = array.getJSONObject(i);
    boolean won = row.getBoolean("won");
    totalWins += won ? 1 : 0;
}

我假设您将在onResponse()方法中处理此问题,但上述代码应该在您的活动中有JSON字符串的任何位置有效。

答案 1 :(得分:1)

InterfaceApi api = ApiClient.getClient().create(InterfaceApi.class);
        Call<CelebrityAwardResponse> call = api.getCelebrityAward(celebrityAwardSendData);
        call.enqueue(new Callback<CelebrityAwardResponse>() {
            @Override
            public void onResponse(Call<CelebrityAwardResponse> call, Response<CelebrityAwardResponse> response) {
                if (response.body().getData() != null) {
                    try {
                        String data = response.body();
                        JSONObject jsonObject = new JSONObject(data);
                        JSONArray jsonArray = jsonObject.getJSONArray("awards");

                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject row = jsonArray.getJSONObject(i);
                            boolean won = row.getBoolean("won");
                            if (won) {
                                Log.d("won", "won name : " + row.getString("name"));
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(Call<CelebrityAwardResponse> call, Throwable t) {
                }
            });

我希望它对你有用......!

答案 2 :(得分:0)

我认为这有助于您在Android中创建RecylerView

 public Adaptername  adapterobj;

         adapterobj = new Adapter name(context, ArrayList);
                    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(context);
                    recyclerViewOrders.setLayoutManager(mLayoutManager);
                    recyclerViewOrders.setItemAnimator(new DefaultItemAnimator());
                    recyclerViewOrders.setAdapter(adapterobj );
                    recyclerViewOrders.smoothScrollToPosition(0);
                    adapterobj .notify
               DataSetChanged();

答案 3 :(得分:0)

试试这个:

_, err := orm.NewOrm().Insert(product)

if err != nil {
    if err.Error() == "no LastInsertId available" {
        log.Println(err)
    } else {
        log.Fatal(err)
    }
}