如何在膨胀布局中设置文本?
How to setText in inflated layout?
我在 android 项目中使用 volley。因为我必须膨胀布局。它工作正常。但是在 Inflated 布局中,我使用 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
仅适用于第一次迭代。剩余的迭代布局是空的。帮我修一下
尝试分别获取 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
总是 return 第一个 view
child。
我在 android 项目中使用 volley。因为我必须膨胀布局。它工作正常。但是在 Inflated 布局中,我使用 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
仅适用于第一次迭代。剩余的迭代布局是空的。帮我修一下
尝试分别获取 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
总是 return 第一个 view
child。