如何在Android中获取列表数据的计数
在我的应用程序中,我应该从服务器获取列表,并显示在如何在Android中获取列表数据的计数,android,list,arraylist,android-recyclerview,Android,List,Arraylist,Android Recyclerview,在我的应用程序中,我应该从服务器获取列表,并显示在回收视图中 我想要中奖者的数量显示回收视图项目。 我的服务器列表: "title": "Transporter", "awards": [ { "description": "Best Performance by an Actor in a Motion Picture - Comedy or Musical", "year": 1998, "won
回收视图中
我想要中奖者的数量显示回收视图
项目。
我的服务器列表:
"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"
}
]
}
]
}
对于连接到服务器,我使用改装
和我的代码:
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) {
}
});
interfaceeapi-api=ApiClient.getClient().create(interfaceeapi.class);
Call Call=api.getCellerityWard(CellerityWardSendData);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.body().getData()!=null){
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
}
});
我怎样才能知道有多少数据是“赢的”:真的
请帮助我,谢谢大家从字符串响应数据创建一个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;
}
inttotalWINS=0;
字符串数据=response.body().getData();
JSONArray JSONArray=新的JSONArray(数据);
对于(int i=0;i
我假设您将在您的onResponse()
方法中处理此问题,尽管上述代码应该在您的活动中具有JSON字符串的任何位置都有效。我认为这将帮助您在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();
interfaceeapi-api=ApiClient.getClient().create(interfaceeapi.class);
Call Call=api.getCellerityWard(CellerityWardSendData);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.body().getData()!=null){
试一试{
字符串数据=response.body();
JSONObject JSONObject=新的JSONObject(数据);
JSONArray JSONArray=jsonObject.getJSONArray(“奖励”);
for(int i=0;i
我希望它对您有用…!试试这个:
try {
int wins = 0;
JSONObject jsonObject = new JSONObject(response.body().getData());
JSONArray awards = jsonObject.getJSONArray("awards");
for(int i=0;i<awards.length();i++)
{
JSONObject award = awards.getJSONObject(i);
if(award.getBoolean("won"))
wins++;
}
System.out.println(wins);
} catch (JSONException e) {
e.printStackTrace();
}
试试看{
int=0;
JSONObject=newJSONObject(response.body().getData());
JSONArray awards=jsonObject.getJSONArray(“awards”);
对于(int i=0;i支付您的庆祝活动回复类您能给我发送您的skype ID吗?请
try {
int wins = 0;
JSONObject jsonObject = new JSONObject(response.body().getData());
JSONArray awards = jsonObject.getJSONArray("awards");
for(int i=0;i<awards.length();i++)
{
JSONObject award = awards.getJSONObject(i);
if(award.getBoolean("won"))
wins++;
}
System.out.println(wins);
} catch (JSONException e) {
e.printStackTrace();
}