繁体   English   中英

如何在膨胀布局中设置文本?

[英]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总是返回它的第一个孩子的view

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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