deluxan
deluxan

Reputation: 422

How to setText in inflated layout?

I am using volley in my android project. In that I have to inflate the layout. It is working fine. But inside Inflated layout I am using setText for the TextView. And the layout inflate is looping based on the API Array length. It is looping correctly.

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);

But setText is working only for the first iteration. The remaining iterated layouts are empty. Help me to fix it

Upvotes: 0

Views: 559

Answers (1)

Md. Asaduzzaman
Md. Asaduzzaman

Reputation: 15433

Try to get inflated view separately and do your operation in that view. Hope this help you.

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);

As layout.findViewById always return the view of it's first child.

Upvotes: 1

Related Questions