我在我的android项目中使用了volley。在这种情况下,我必须增加布局。一切正常。但是在膨胀布局中,我将setText
使用TextView
。而且布局膨胀是基于API数组长度的循环。它正确循环。
JsonObjectRequest innerLiveRequest = new JsonObjectRequest
(Request.Method.GET, liveURL, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray liveArray = response.getJSONArray("data");
for (int i = 0; i < liveArray.length(); i++) {
JSONObject data = liveArray.getJSONObject(i);
String liveID = data.getString("id");
final String league_id = data.getString("league_id");
final String localteam_id = data.getString("localteam_id");
final String visitorteam_id = data.getString("visitorteam_id");
final String localScore = data.getJSONObject("scores").getString("localteam_score");
final String visitorScore = data.getJSONObject("scores").getString("visitorteam_score");
final String minute = data.getJSONObject("time").getString("minute");
final String willStart = data.getJSONObject("time").getJSONObject("starting_at").getString("time");
if (league_id.equals(leagueID)) {
layout.addView(LayoutInflater.from(getContext()).inflate(R.layout.layout_expanded_layout, layout, false));
TextView minutesPlaying = layout.findViewById(R.id.minutes);
TextView local = layout.findViewById(R.id.localTeam);
TextView visitor = layout.findViewById(R.id.visitorTeam);
TextView localScoreText = layout.findViewById(R.id.localTeamScore);
TextView visitorScoreText = layout.findViewById(R.id.visitorTeamScore);
local.setText(localteam_id);
visitor.setText(visitorteam_id);
localScoreText.setText(localScore);
visitorScoreText.setText(visitorScore);//Log.i("ITER", liveId);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
Volley.newRequestQueue(getContext()).add(innerLiveRequest);
但是setText
仅在第一次迭代中有效。其余的迭代布局为空。帮我修复它
答案 0 :(得分:1)
尝试分别获取inflated
view
,然后在该view
中进行操作。希望对您有帮助。
View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_expanded_layout, layout, false);
layout.addView(view);
TextView minutesPlaying = view.findViewById(R.id.minutes);
TextView local = view.findViewById(R.id.localTeam);
TextView visitor = view.findViewById(R.id.visitorTeam);
TextView localScoreText = view.findViewById(R.id.localTeamScore);
TextView visitorScoreText = view.findViewById(R.id.visitorTeamScore);
local.setText(localteam_id);
visitor.setText(visitorteam_id);
localScoreText.setText(localScore);
visitorScoreText.setText(visitorScore);
由于layout.findViewById
始终返回其第一个孩子的view
。