'Android LayoutInflator - Multiple views in for loop
I am creating my first app with Java for android in android studio and have some problems with the LayoutInflator.
The java code of the Activity looks like this:
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.io.Serializable;
import java.util.ArrayList;
public class MatchActivity extends MatchSettingsActivity implements View.OnClickListener, Serializable {
public ArrayList<Player> matchParticipants = new ArrayList<>();
public String matchType;
public int setsAmount;
public int legsAmount;
public String checkOutType;
public String atcMode;
public int matchFinished;
public final String matchType501 = "501";
public final String matchType301 = "301";
public ArrayList<View> playerRowViewsArrayList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_match);
ImageButton backButton = findViewById(R.id.match_back_button);
backButton.setOnClickListener(this);
Match match = (Match) getIntent().getSerializableExtra("match_object");
setMatchSettings(match.matchParticipants, match.matchType,
Integer.parseInt(match.setsAmount), Integer.parseInt(match.legsAmount),
match.checkOut, match.atcMode, match.matchFinished);
createMatchUI(matchType, matchParticipants);
}
@Override
public void onClick(View v) {
super.onClick(v);
if (v.getId() == R.id.match_back_button) {
Intent matchIntent = new Intent(MatchActivity.this, MainActivity.class);
MatchActivity.this.startActivity(matchIntent);
setContentView(R.layout.activity_main);
}
}
public void createMatchUI(String matchType, ArrayList<Player> matchParticipants) {
LinearLayout mainMatchLayout = findViewById(R.id.main_match_layout);
LayoutInflater inflater = getLayoutInflater();
if (matchType.equals(matchType501) || matchType.equals(matchType301)) {
for (int i=0; i<matchParticipants.size(); i+=1) {
View playerRowView = inflater.inflate(R.layout.player_row_layout, mainMatchLayout, false);
TextView nameTextView = playerRowView.findViewById(R.id.name);
TextView pointsTextView = playerRowView.findViewById(R.id.points);
TextView averageTextView = playerRowView.findViewById(R.id.average);
TextView lastDart1 = playerRowView.findViewById(R.id.last_d1);
TextView lastDart2 = playerRowView.findViewById(R.id.last_d2);
TextView lastDart3 = playerRowView.findViewById(R.id.last_d3);
TextView lastDartsCombined = playerRowView.findViewById(R.id.last_darts_combined);
nameTextView.setText(matchParticipants.get(i).playerName);
if (matchType.equals(matchType501)) {
pointsTextView.setText(matchType501);
} else {
pointsTextView.setText(matchType301);
}
averageTextView.setText("");
lastDart1.setText("");
lastDart2.setText("");
lastDart3.setText("");
lastDartsCombined.setText("");
playerRowViewsArrayList.add(playerRowView);
}
for (int i1=0; i1<playerRowViewsArrayList.size(); i1+=1) {
mainMatchLayout.addView(playerRowViewsArrayList.get(i1));
}
}
}
public void setMatchSettings(ArrayList<Player> matchParticipants, String matchType,
int setsAmount, int legsAmount, String checkOutType,
String atcMode, int matchFinished) {
this.matchParticipants = matchParticipants;
this.matchType = matchType;
this.setsAmount = setsAmount;
this.legsAmount = legsAmount;
this.checkOutType = checkOutType;
this.atcMode = atcMode;
this.matchFinished = matchFinished;
}
}
In the method createMatchUI I am trying to display one view for each player Object in a list.
The layout that should be created for each player using the LayoutInflator looks like this
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/points"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="30sp"
android:fontFamily="@font/suit_bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/last_d1"
app:layout_constraintTop_toTopOf="parent"
android:gravity="center"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:background="@drawable/custom_rl_button_blue_borderless"/>
<TextView
android:id="@+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="16sp"
android:fontFamily="@font/suit_bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/points"
app:layout_constraintEnd_toEndOf="@id/points"
app:layout_constraintBottom_toBottomOf="@id/last_darts_combined"
android:gravity="center"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:background="@drawable/custom_rl_button_blue_borderless"/>
<TextView
android:id="@+id/last_d1"
android:layout_width="0dp"
android:layout_height="0dp"
android:textSize="16sp"
android:fontFamily="@font/suit_bold"
app:layout_constraintEnd_toStartOf="@id/last_d2"
app:layout_constraintTop_toTopOf="@id/points"
app:layout_constraintStart_toEndOf="@id/points"
app:layout_constraintBottom_toBottomOf="@id/points"
android:gravity="center"
app:layout_constraintBottom_toTopOf="@id/last_darts_combined"
android:background="@drawable/custom_rl_button_blue_borderless"/>
<TextView
android:id="@+id/last_d2"
android:layout_width="0dp"
android:layout_height="0dp"
android:textSize="16sp"
android:fontFamily="@font/suit_bold"
app:layout_constraintEnd_toStartOf="@id/last_d3"
app:layout_constraintTop_toTopOf="@id/points"
app:layout_constraintStart_toEndOf="@id/last_d1"
app:layout_constraintBottom_toBottomOf="@id/points"
android:gravity="center"
android:background="@drawable/custom_rl_button_blue_borderless"/>
<TextView
android:id="@+id/last_d3"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:textSize="16sp"
android:fontFamily="@font/suit_bold"
app:layout_constraintEnd_toStartOf="@id/sets"
app:layout_constraintStart_toEndOf="@id/last_d2"
app:layout_constraintTop_toTopOf="@id/points"
app:layout_constraintBottom_toBottomOf="@id/points"
android:background="@drawable/custom_rl_button_blue_borderless"/>
<TextView
android:id="@+id/last_darts_combined"
android:layout_width="0dp"
android:layout_height="0dp"
android:textSize="16sp"
android:fontFamily="@font/suit_bold"
app:layout_constraintEnd_toEndOf="@id/last_d3"
app:layout_constraintStart_toStartOf="@id/last_d1"
app:layout_constraintTop_toTopOf="@id/name"
app:layout_constraintBottom_toBottomOf="@id/name"
android:gravity="center"
android:background="@drawable/custom_rl_button_blue_borderless"/>
<TextView
android:id="@+id/sets"
android:layout_width="0dp"
android:layout_height="0dp"
android:textSize="16sp"
android:fontFamily="@font/suit_bold"
app:layout_constraintTop_toTopOf="@id/points"
app:layout_constraintEnd_toStartOf="@id/legs"
app:layout_constraintStart_toEndOf="@id/last_d3"
app:layout_constraintBottom_toBottomOf="@id/points"
android:gravity="center"
android:background="@drawable/custom_rl_button_blue_borderless"/>
<TextView
android:id="@+id/legs"
android:layout_width="0dp"
android:layout_height="0dp"
android:textSize="16sp"
android:fontFamily="@font/suit_bold"
app:layout_constraintTop_toTopOf="@id/points"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/sets"
app:layout_constraintBottom_toBottomOf="@id/points"
android:gravity="center"
android:background="@drawable/custom_rl_button_blue_borderless"/>
<TextView
android:id="@+id/average"
android:layout_width="0dp"
android:layout_height="0dp"
android:textSize="16sp"
android:fontFamily="@font/suit_bold"
app:layout_constraintStart_toEndOf="@id/last_darts_combined"
app:layout_constraintTop_toTopOf="@id/name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="@id/name"
android:gravity="center"
android:background="@drawable/custom_rl_button_blue_borderless"/>
<View
android:id="@+id/divider"
android:layout_width="1dp"
android:layout_height="0dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toBottomOf="@+id/name"
app:layout_constraintTop_toTopOf="@+id/points"
app:layout_constraintEnd_toStartOf="@id/points"
app:layout_constraintStart_toStartOf="@id/points"/>
<View
android:id="@+id/divider2"
android:layout_width="1dp"
android:layout_height="0dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toBottomOf="@+id/name"
app:layout_constraintTop_toTopOf="@+id/points"
app:layout_constraintEnd_toStartOf="@id/last_d1"
app:layout_constraintStart_toEndOf="@id/points"/>
<View
android:id="@+id/divider3"
android:layout_width="1dp"
android:layout_height="0dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toBottomOf="@+id/points"
app:layout_constraintTop_toTopOf="@+id/points"
app:layout_constraintEnd_toStartOf="@id/last_d2"
app:layout_constraintStart_toEndOf="@id/last_d1"/>
<View
android:id="@+id/divider4"
android:layout_width="1dp"
android:layout_height="0dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toBottomOf="@+id/points"
app:layout_constraintTop_toTopOf="@+id/points"
app:layout_constraintEnd_toStartOf="@id/last_d3"
app:layout_constraintStart_toEndOf="@id/last_d2"/>
<View
android:id="@+id/divider5"
android:layout_width="1dp"
android:layout_height="0dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toBottomOf="@+id/name"
app:layout_constraintTop_toTopOf="@+id/points"
app:layout_constraintEnd_toStartOf="@id/sets"
app:layout_constraintStart_toEndOf="@id/last_d3"/>
<View
android:id="@+id/divider6"
android:layout_width="1dp"
android:layout_height="0dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toBottomOf="@+id/points"
app:layout_constraintTop_toTopOf="@+id/points"
app:layout_constraintEnd_toStartOf="@id/legs"
app:layout_constraintStart_toEndOf="@id/sets"/>
<View
android:id="@+id/divider7"
android:layout_width="1dp"
android:layout_height="0dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toBottomOf="@+id/name"
app:layout_constraintTop_toTopOf="@+id/points"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/legs"/>
<View
android:id="@+id/divider8"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="?android:attr/listDivider"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="@id/points"
app:layout_constraintTop_toBottomOf="@id/points"/>
<View
android:id="@+id/divider9"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="?android:attr/listDivider"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@id/points"
app:layout_constraintTop_toTopOf="@id/points"/>
<View
android:id="@+id/divider10"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="?android:attr/listDivider"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="@id/name"
app:layout_constraintTop_toBottomOf="@id/name"/>
</androidx.constraintlayout.widget.ConstraintLayout>
and the root activity/layout looks like this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_color">
<androidx.appcompat.widget.Toolbar
android:id="@+id/match_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/toolbar_color"
android:theme="@style/toolbarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.appcompat.widget.Toolbar>
<TextView
android:id="@+id/match_label"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:gravity="center_vertical"
app:layout_constraintStart_toStartOf="@id/match_toolbar"
app:layout_constraintBottom_toBottomOf="@id/match_toolbar"
app:layout_constraintEnd_toEndOf="@id/match_toolbar"
app:layout_constraintTop_toTopOf="@id/match_toolbar"
android:text="@string/match_label"
android:textColor="@color/white"
android:fontFamily="@font/suit_light"
android:textSize="20sp"/>
<ImageButton
android:id="@+id/match_back_button"
android:layout_width="35dp"
android:layout_height="25dp"
android:background="@drawable/back_arrow2"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
app:layout_constraintBottom_toBottomOf="@id/match_toolbar"
app:layout_constraintStart_toStartOf="@id/match_toolbar"
app:layout_constraintTop_toTopOf="@id/match_toolbar" />
<LinearLayout
android:id="@+id/main_match_layout"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="horizontal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/match_toolbar"
app:layout_constraintBottom_toTopOf="@id/input_layout"
android:layout_margin="15dp">
</LinearLayout>
<LinearLayout
android:id="@+id/input_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
[...]
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
The problem I am facing is, that always only one view is added to the root layout (mainMatchLayout) like this:
Instead I want one layout for each player object that exists in my ArrayList matchParticipants.
Can anyone figure out what I'm doing wrong?
Thank you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

