Android Java Cannot find local variable - java

My MainActivity
public class MainActivity extends AppCompatActivity {//My MainActivity
ArrayList<Response> responses = new ArrayList<>();
private String BASE_URL = "https://covid-193.p.rapidapi.com/";
RecyclerView recyclerView;
MyRecycleViewAdapter myRecycleViewAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycleViewCovid);
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create());//this is convert json
Retrofit retrofit = builder.build();
Covid19WebApi covid19WebApi = retrofit.create(Covid19WebApi.class);
Call<Covid19Model> call = covid19WebApi.getData();//this is call api interfacee method
call.enqueue(new Callback<Covid19Model>() {
#Override
public void onResponse(Call<Covid19Model> call, retrofit2.Response<Covid19Model> response) {
responses = (ArrayList<Response>) response.body().getResponse();//This is my Api response 232 items
LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(layoutManager);
myRecycleViewAdapter = new MyRecycleViewAdapter(responses);//My Adapter passing data in my Adapter
recyclerView.setAdapter(myRecycleViewAdapter);
}
#Override
public void onFailure(Call<Covid19Model> call, Throwable t) {
Toast.makeText(MainActivity.this,t.getLocalizedMessage(),Toast.LENGTH_LONG).show();//this is toast message failure
}
});
}
My Adapter
public class MyRecycleViewAdapter extends RecyclerView.Adapter<MyRecycleViewAdapter.ViewHolder>
{
private ArrayList<Response> responseArrayList;
public MyRecycleViewAdapter ( ArrayList<Response> responseArrayList) {
this.responseArrayList = responseArrayList;//Response data items in My Adapter
}
#NonNull
#Override
public MyRecycleViewAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());//Api passing data added this layout
View view = layoutInflater.inflate(R.layout.row_layout,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyRecycleViewAdapter.ViewHolder holder, int position) {
holder.setData(responseArrayList.get(position));//Api Items List
}
#Override
public int getItemCount() {
return responseArrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView continent;
TextView country;
TextView population;
TextView casesNew;
TextView caseActive;
TextView caseCritical;
TextView caseRecovered;
TextView caseM_pop;
TextView caseTotal;
TextView deathsNew;
TextView deathsM_pop;
TextView deathsTotal;
TextView testsM_pop;
TextView testsTotal;
TextView day;
TextView time;
TextView Deaths;
TextView Tests;
TextView Cases;
public ViewHolder(#NonNull View itemView) {//My ViewHolder Connect Api data and View Layout TextView
super(itemView);
continent = itemView.findViewById(R.id.continentName);//My layout Textview id
country = itemView.findViewById(R.id.countryName);
population =itemView.findViewById(R.id.populationName);
casesNew = itemView.findViewById(R.id.CaseNewName);
caseActive = itemView.findViewById(R.id.casesActiveName);
caseCritical=itemView.findViewById(R.id.criticalName);
caseRecovered=itemView.findViewById(R.id.recoveredName);
caseM_pop = itemView.findViewById(R.id.M_popName);
caseTotal = itemView.findViewById(R.id.totalCases);
deathsNew = itemView.findViewById(R.id.deathsNewName);
deathsM_pop = itemView.findViewById(R.id.deaths1M_pop);
deathsTotal = itemView.findViewById(R.id.totalDeaths);
testsM_pop = itemView.findViewById(R.id.tests1M_pop);
testsTotal = itemView.findViewById(R.id.testsTotal);
day = itemView.findViewById(R.id.dayName);
time = itemView.findViewById(R.id.timeName);
Cases = itemView.findViewById(R.id.Cases);
Deaths = itemView.findViewById(R.id.Deaths);
Tests = itemView.findViewById(R.id.Tests);
}
private void setData(Response response){
Response res = response;
continent.setText(res.getContinent());//Debugging this TextView Api data is null
country.setText(res.getCountry());
population.setText(res.getPopulation());
casesNew.setText(res.getCases().getNew());
caseActive.setText(res.getCases().getActive());
caseCritical.setText(res.getCases().getCritical());
caseRecovered.setText(res.getCases().getRecovered());
caseM_pop.setText(res.getCases().get1MPop());
caseTotal.setText(res.getCases().getTotal());
deathsM_pop.setText(res.getDeaths().get1MPop());
deathsNew.setText(res.getDeaths().getNew());
deathsTotal.setText(res.getDeaths().getTotal());
testsM_pop.setText(res.getTests().get1MPop());
testsTotal.setText(res.getTests().getTotal());
day.setText(res.getDay());
time.setText(res.getTime());
Tests.setText(res.getTests().toString());
Cases.setText(res.getCases().toString());
Deaths.setText(res.getDeaths().toString());
}
}
}
My Problems
Please Help me What is the Problem?
I pull the data with the Api, but when I debug, I see that the data comes to My MainActivity and is added to the Adapter from there, but when the data reaches the Viewholder, it becomes null when set to the TextView. I don't understand what the problem is.
My Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#8BC34A"
android:orientation="vertical">
<TextView
android:id="#+id/continentName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:hint="continent"
android:textColor="#color/black"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/countryName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="country"
android:textColor="#color/black"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/continentName" />
<TextView
android:id="#+id/populationName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="population"
android:textColor="#color/black"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/countryName" />
<TextView
android:id="#+id/dayName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="day"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/populationName" />
<TextView
android:id="#+id/timeName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="time"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/dayName" />
<TextView
android:id="#+id/CaseNewName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="new"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/timeName" />
<TextView
android:id="#+id/casesActiveName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="active"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/CaseNewName" />
<TextView
android:id="#+id/criticalName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="critical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/casesActiveName" />
<TextView
android:id="#+id/recoveredName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="recovered"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/criticalName" />
<TextView
android:id="#+id/M_popName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="1M_pop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/recoveredName" />
<TextView
android:id="#+id/totalCases"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="total"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/M_popName" />
<TextView
android:id="#+id/deaths1M_pop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="1M_pop(deaths)"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/totalCases" />
<TextView
android:id="#+id/deathsNewName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="new(Deaths)"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/deaths1M_pop" />
<TextView
android:id="#+id/totalDeaths"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="total(deaths)"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/deathsNewName" />
<TextView
android:id="#+id/tests1M_pop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="1M_pop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/totalDeaths" />
<TextView
android:id="#+id/testsTotal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:hint="total(tests)"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tests1M_pop" />
<TextView
android:id="#+id/Deaths"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="Deaths"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/testsTotal" />
<TextView
android:id="#+id/Tests"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="Tests"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Deaths" />
<TextView
android:id="#+id/Cases"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:hint="Cases"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Tests" />
</LinearLayout>"

Related

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.

Constraint layout has space bottom and top

I'm building an app using Constraint layout, but it has extra space bottom and top. I can't find where they come from. I checked maybe ten times from top to bottom all code but there is no margin or padding or anything else for it.
<?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:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
android:paddingLeft="0dp"
android:paddingTop="0dp"
android:paddingRight="0dp"
android:paddingBottom="0dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/Brand"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/brandname"
style="#style/viewParent.headerText"
android:text="Did"
android:textColor="#color/white" />
<TextView
android:id="#+id/brandname2"
style="#style/viewParent.headerText2"
android:layout_toRightOf="#id/brandname"
android:text="You"
android:textColor="#color/yellow" />
<TextView
style="#style/viewParent.headerText2"
android:layout_toRightOf="#id/brandname2"
android:text="Know!"
android:textColor="#color/red"
android:textStyle="bold|italic" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/container_main"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="4dp"
android:background="#drawable/card_bg"
android:paddingLeft="16dp"
android:paddingRight="16dp"
app:layout_constraintBottom_toTopOf="#id/guideline2"
app:layout_constraintEnd_toEndOf="#id/guidelineyazisag"
app:layout_constraintStart_toStartOf="#id/guidelineyazisol"
app:layout_constraintTop_toBottomOf="#id/guideline">
<TextView
android:id="#+id/activity_main_text_view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="simple Title Text"
android:textColor="#5E4C4C"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/activity_main_did_u_know"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Category"
android:textColor="#5E4C4C"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.core.widget.NestedScrollView
android:id="#+id/scrollview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:fillViewport="true"
app:layout_constraintBottom_toTopOf="#id/activity_main_image_view"
app:layout_constraintTop_toBottomOf="#id/activity_main_did_u_know">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#id/activity_main_image_view"
app:layout_constraintTop_toBottomOf="#id/activity_main_did_u_know">
<TextView
android:id="#+id/factTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:gravity="center_vertical|center_horizontal"
android:lineSpacingExtra="8dp"
android:padding="10dp"
android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged."
android:textColor="#000000"
android:textSize="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
<ImageView
android:id="#+id/activity_main_image_view"
android:layout_width="30dp"
android:layout_height="40dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:clickable="true"
android:src="#drawable/ic_share_black_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/scrollview" />
<ImageView
android:id="#+id/activity_main_fav_button"
android:layout_width="30dp"
android:layout_height="40dp"
android:layout_margin="16dp"
android:clickable="true"
android:src="#drawable/ic_favorite_border_black_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/scrollview" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/LeftRight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#id/adView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/guideline2">
<ImageView
android:id="#+id/activity_main_left_button"
android:layout_width="60dp"
android:layout_height="60dp"
android:clickable="true"
android:src="#drawable/ic_previous"
android:elevation="3dp"
android:background="#drawable/okarka"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/guideline3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/activity_main_right_button"
android:layout_width="60dp"
android:layout_height="60dp"
android:clickable="true"
android:src="#drawable/ic_next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
android:background="#drawable/okarka"
app:layout_constraintStart_toEndOf="#+id/guideline3"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
ads:layout_constraintBottom_toBottomOf="parent"
ads:layout_constraintLeft_toLeftOf="parent"
ads:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/LeftRight" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.076" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.7" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guidelineyazisol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.07" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guidelineyazisag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.93" />
</androidx.constraintlayout.widget.ConstraintLayout>
I tried adding guidelines to 0 points in the bottom but it also starts in that space. I gave -(minus) values to margin and padding but doesn't work. I don't know what else to do. You can see from screenshots how it looks.
public class MainActivity extends AppCompatActivity {
private ColorWheel colorWheel = new ColorWheel();
private TextView factTextView;
private TextView textViewId;
private TextView textViewTableName;
ImageView tableImageView;
AdView adView;
ImageView shareImageView;
ImageView favImageView;
float x1,x2,y1,y2;
int i = 1;
int adCounter = 0;
private ConstraintLayout constraintLayout;
String fact;
ArrayList<Fact> favList = new ArrayList<>();
private InterstitialAd mInterstitial;
AdRequest interAdRequest;
int favId;
String favTable;
Typeface typeface;
List<Fact> mData;
private DatabaseFacts db;
GradientDrawable shape;
#SuppressLint({"ClickableViewAccessibility", "SetTextI18n"})
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DatabaseFacts(this);
db.copyDbIfNotExists();
shape = new GradientDrawable();
shape.setCornerRadius(40);
mData = new ArrayList<>();
MobileAds.initialize(this, "ca-app-pub-2469721886989416~7390658870");
adView = findViewById(R.id.adView);
AdRequest bannerAdRequest = new AdRequest.Builder().build();
adView.loadAd(bannerAdRequest);
interAdRequest = new AdRequest.Builder().build();
mInterstitial = new InterstitialAd(this);
mInterstitial.setAdUnitId("ca-app-pub-2469721886989416/9441000894");
mInterstitial.loadAd(interAdRequest);
factTextView = findViewById(R.id.factTextView);
constraintLayout = (ConstraintLayout) findViewById(R.id.container_main);
textViewId = findViewById(R.id.activity_main_text_view_id);
favImageView = findViewById(R.id.activity_main_fav_button);
shareImageView = findViewById(R.id.activity_main_image_view);
textViewTableName = findViewById(R.id.activity_main_did_u_know);
// tableImageView = findViewById(R.id.img_user);
// if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
// typeface = getResources().getFont(R.font.sourceserifproregular);
// }
factTextView.setTypeface(typeface);
Intent intent = getIntent();
final int choice = intent.getIntExtra("choice",0);
switch (choice) {
case 1:
textViewTableName.setText("General Facts");
factTextView.setText(db.getFact((readFromShared("defaultKey") - 1), DatabaseFacts.TABLE_GENERAL_NAME).getFact());
textViewId.setText("Fact " + (readFromShared("defaultKey") - 1) + " of " + db.getFactsCount(DatabaseFacts.TABLE_GENERAL_NAME));
if (db.getFact((readFromShared("defaultKey") - 1), DatabaseFacts.TABLE_GENERAL_NAME).isFavorite() == 0) {
favImageView.setImageResource(R.drawable.ic_favorite_border_black_24dp);
} else {
favImageView.setImageResource(R.drawable.ic_favorite_black_24dp);
}
ImageView leftClick = (ImageView) findViewById(R.id.activity_main_left_button);
leftClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
leftClickMethod("defaultKey", DatabaseFacts.TABLE_GENERAL_NAME);
}
});
ImageView rightClick = (ImageView) findViewById(R.id.activity_main_right_button);
rightClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
rightClickMethod("defaultKey",DatabaseFacts.TABLE_GENERAL_NAME);
return;
}
});
favImageView.setOnClickListener(v -> handleFavorites("defaultKey", DatabaseFacts.TABLE_GENERAL_NAME));
break;
Remove app:layout_constraintBottom_toTopOf="#id/guideline" from Brand and app:layout_constraintTop_toBottomOf="#id/LeftRight" from adView. You can't put constraints for Top and Bottom because it will center your View between those two constraints. Same about left and right (start, end), but you have match_parent here, so this problem doesn't occur (visualy - if you set wrap_content, view will go to the center too).

how do you fix a java.langIllegal state exception on fragments?

I've made a checklist app and when the user add's the check to the app I get the below error and a crash. Can someone provide some advice to how to fix this? As i'm using fragments, i've created a second Java file and refernced that to the XML as you cannot have more then one class in a single Java, i've tried merging them but get lots of errors and cannot use the public class 'extends' more then once.
Here's the error
Heres the XML
<TextView
android:id="#+id/text_checklist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Checklist"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="24sp"
android:textStyle="bold"
android:typeface="normal"
app:fontFamily="#font/basic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.491"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/text_checklist"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="#+id/inputText"
android:layout_width="369dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="13dp"
android:ems="10"
android:hint="#string/placeholder"
android:inputType="textPersonName" />
<Button
android:id="#+id/check_list_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="57dp"
android:onClick="addButton2"
android:text="#string/buttonText" />
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="107dp" />
Heres the 1st Java (Fragment)
checklistViewModel =
ViewModelProviders.of(this).get(com.example.csd301_project_trackday_timing_dh.ui.checklist.ChecklistViewModel.class);
View root = inflater.inflate(R.layout.fragment_checklist, container, false);
final TextView textView = root.findViewById(R.id.text_checklist);
checklistViewModel.getText().observe(this, new Observer<String>() {
#Override
public void onChanged(#Nullable String s) {
textView.setText(s);
}
});
return root;
}
}
Heres the 2nd Java (CheckList)
Button btn_checklist;
EditText inputText;
ListView listView;
ArrayList<String> list;
public void addButton2(View view){
String text = inputText.getText().toString();
list.add(text);
ArrayAdapter adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,list);
listView.setAdapter(adapter);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_checklist);
btn_checklist= findViewById(R.id.check_list_button);
inputText=findViewById(R.id.inputText);
listView=findViewById(R.id.listView);
list = new ArrayList<>();
}
}

Android Studio with Java: ArrayAdapter not working correctly

I'm making an app in which the user can enter a few items and have it displayed as he adds items. I'm using an adapter to achieve this but instead of showing me a neat list with items, the adapter creates a list of several views repeating themselves (i.e Instead of just using the given TextView as a Template for the entry in the listview, it uses all views in that activity).
Here is the code:
This is from the createEntry class. This code initiates the adapter and loads the previous entries for a particular day. It send adds them all to the adapter.
public void loadEntries() {
events = new ArrayList<>();
adapter = new EntryAdapter(this, entries);
ListView listView = findViewById(R.id.entry_list);
listView.setAdapter(adapter);
new AsyncTask<Void, Void, List<String>>() {
#Override
protected List<String> doInBackground(Void... voids) {
return appDB.entryDao().getEntriesFromDate(day, monthNum, year);
}
#Override
protected void onPostExecute(List items) {
generateEntryTable(items);
}
}.execute();
}
public void generateEntryTable(List items) {
for (int i = 0; i < items.size(); i++) {
adapter.add(new TableEntries(year, monthNum, day, entryContent));
}
}
This is my custom EntryAdapter class:
public class EntryAdapter extends ArrayAdapter<Entry> {
public EntryAdapter(Context context, ArrayList<Entry> entries) {
super(context, 0, entries);
}
#Override
public View getView(int position, View convertView, ViewGroup container {
Entry entry = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_create_event, container, false);
}
TextView newEntry = convertView.findViewById(R.id.new_entry);
newEntry.setText(entry.getContent());
return convertView;
}
}
This is the XML-file:
<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:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CreateEntry">
<TextView
android:id="#+id/date"
android:layout_width="222dp"
android:layout_height="44dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="215dp"
android:layout_marginRight="215dp"
android:layout_marginBottom="685dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="403dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="116dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="200dp"
android:background="#5CFF9800"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="#+id/linearLayout2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/date"
app:layout_constraintVertical_bias="0.0">
<TextView
android:id="#+id/textView3"
android:layout_width="112dp"
android:layout_height="35dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="276dp"
android:layout_marginRight="276dp"
android:layout_marginBottom="8dp"
android:text="#string/event"
android:textSize="24sp" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/addEvents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:contentDescription="#string/add_events"
android:cropToPadding="true"
android:onClick="addEvent"
android:scaleType="fitStart"
android:tint="#FF3F02"
app:srcCompat="#android:drawable/ic_menu_add" />
<EditText
android:id="#+id/addEventsPrompt"
android:layout_width="353dp"
android:layout_height="30dp"
android:background="#android:color/transparent"
android:clickable="true"
android:freezesText="false"
android:gravity="start|fill|center_vertical"
android:hint="#string/startTyping"
android:inputType="textShortMessage|textLongMessage|textMultiLine|textPersonName|text"
android:textAllCaps="false"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:textSize="14sp" />
</TableRow>
<ListView
android:id="#+id/event_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="#+id/entry_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/new_entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="156dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:id="#+id/toDoTitle"
android:layout_width="102dp"
android:layout_height="33dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="322dp"
android:layout_marginRight="322dp"
android:text="#string/to_do"
android:textSize="24sp" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="#+id/addToDo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:background="#00FFFFFF"
android:contentDescription="#string/add_to_dos"
android:onClick="addToDo"
android:scaleType="fitStart"
android:tint="#FF3B00"
app:srcCompat="#android:drawable/ic_menu_add" />
<EditText
android:id="#+id/addToDoPrompt"
android:layout_width="344dp"
android:layout_height="32dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_weight="100"
android:autofillHints=""
android:background="#android:color/transparent"
android:gravity="start|fill_horizontal|center_vertical"
android:hint="#string/startTyping"
android:inputType="textShortMessage|textLongMessage|textMultiLine|text"
android:textSize="14sp" />
<TextView
android:id="#+id/new_to_do"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
</LinearLayout>
If you need to see any other code, please let me know.
The difference in usage of adapter to the tutorials that I have seen is that, in my app, the listview is not the only view displayed in the activity so that may be the/a problem.
You're going about this in the wrong manner. In your EntryAdapter:
#Override
public View getView(int position, View convertView, ViewGroup container {
Entry entry = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext())
.inflate(R.layout.activity_create_event, container, false);
}
...
In that inflate statement you are inflating the entire XML layout for your Activity (R.layout.activity_create_event) for every row of your ListView. Instead, you should be using a much simpler layout containing just your TextView in a separate layout XML file, for example let's call this res/layout/list_row.xml:
<LinearLayout 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:orientation="vertical">
<TextView
android:id="#+id/new_entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Placeholder Text" />
</LinearLayout>
Then replace the layout you're inflating in getView() like so:
#Override
public View getView(int position, View convertView, ViewGroup container {
Entry entry = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext())
.inflate(R.layout.list_row, container, false);
}
...
I'd also recommend looking into RecyclerView as that has now superceded ListView.

How control media player in android across all activities?

Hello friends i am creating a mp3 player which have two activities how can i control media from both activities like play pause and progress bar here is
the first activity have list of the song when user click any songs it work fine and below this song listview i create progress and bar and media controll button like play pause and next and back its include spreate layoute ad in below list when user click on this layout it redirect another activity and which have also same feature like play pause and and progress bar how can i controll this media from both activities?
my code!
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#1e1e2c"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/song_list"/>
<include
android:layout_below="#+id/song_title"
layout="#layout/mediacontroller"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<RelativeLayout 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:background="#1e1e2c"
android:layout_height="match_parent">
<ImageView
android:layout_width="180dp"
android:layout_height="180dp"
android:id="#+id/rotate"
android:layout_centerInParent="true"
android:src="#drawable/musiccirlce"/>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">
<ImageButton
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:clickable="true"
android:focusable="true"
android:id="#+id/repeat"
android:visibility="visible"
android:scaleType="centerCrop"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#drawable/baseline_repeat_white_48dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.023"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.811" />
<ImageButton
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:clickable="true"
android:focusable="true"
android:id="#+id/repeatenable"
android:visibility="gone"
android:scaleType="centerCrop"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#drawable/repeatenable"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.023"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.811" />
<ImageButton
android:id="#+id/shuffle"
android:layout_width="20dp"
android:layout_height="24dp"
android:visibility="visible"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:scaleType="centerCrop"
android:src="#drawable/baseline_shuffle_white_48dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.976"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.811" />
<ImageButton
android:layout_width="25dp"
android:id="#+id/shufflenable"
android:layout_height="25dp"
android:visibility="gone"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:scaleType="centerCrop"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#drawable/shuffulenabled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.976"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.811" />
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="99dp"
android:layout_alignParentBottom="true">
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="80dp"
android:clickable="false"
android:background="#color/backgroundColor"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="55dp"
android:layout_height="55dp"
android:padding="15dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="16dp"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:scaleType="centerCrop"
android:src="#mipmap/outline_thumb_up_alt_black_48"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/songtime"
app:layout_constraintVertical_bias="0.56"
android:layout_marginRight="8dp"
android:layout_marginLeft="16dp" />
<ImageButton
android:id="#+id/imageButton2new"
android:layout_width="55dp"
android:layout_height="55dp"
android:padding="15dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="16dp"
android:visibility="gone"
android:clickable="true"
android:focusable="true"
android:scaleType="centerCrop"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#mipmap/baseline_thumb_up_alt_black_48"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/songtime"
app:layout_constraintVertical_bias="0.56"
android:layout_marginRight="8dp"
android:layout_marginLeft="16dp" />
<ImageButton
android:id="#+id/button"
android:layout_width="55dp"
android:layout_height="55dp"
android:padding="15dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="8dp"
android:scaleType="centerCrop"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#mipmap/baseline_thumb_down_alt_black_18"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/endTime"
app:layout_constraintVertical_bias="0.56"
android:layout_marginRight="16dp"
android:layout_marginLeft="8dp" />
<ImageButton
android:id="#+id/buttontwo"
android:layout_width="55dp"
android:layout_height="55dp"
android:padding="15dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="8dp"
android:visibility="gone"
android:scaleType="centerCrop"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#mipmap/baseline_thumb_down_alt_black_18"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/endTime"
app:layout_constraintVertical_bias="0.56"
android:layout_marginRight="16dp"
android:layout_marginLeft="8dp" />
<TextView
android:id="#+id/songtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:fontFamily="sans-serif"
android:text="1:05"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/endTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:fontFamily="sans-serif"
android:text="3:06"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<LinearLayout
android:id="#+id/linearLayout5"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<ImageButton
android:id="#+id/play_button_main"
android:layout_width="55dp"
android:layout_height="55dp"
android:scaleType="centerCrop"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#drawable/play_button" />
<ImageButton
android:id="#+id/pause_button_main"
android:layout_width="55dp"
android:visibility="gone"
android:scaleType="centerCrop"
android:layout_height="55dp"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#drawable/pause_button" />
</LinearLayout>
<ImageButton
android:layout_width="55dp"
android:id="#+id/pervious"
android:layout_height="55dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:padding="15dp"
android:clickable="true"
android:focusable="true"
android:scaleType="fitCenter"
android:src="#drawable/backword_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/linearLayout5"
app:layout_constraintHorizontal_bias="0.754"
app:layout_constraintStart_toEndOf="#+id/imageButton2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<ImageButton
android:id="#+id/next"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:padding="15dp"
android:scaleType="fitCenter"
android:src="#drawable/forword_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/button"
app:layout_constraintHorizontal_bias="0.25"
app:layout_constraintStart_toEndOf="#+id/linearLayout5"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
/>
</android.support.constraint.ConstraintLayout>
</RelativeLayout>
<SeekBar
android:id="#+id/seekBar3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/relativeLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/relativeLayout"
app:layout_constraintVertical_bias="0.75" />
</android.support.constraint.ConstraintLayout>
</RelativeLayout>
package music.player.musicplayer;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class MainActivity extends AppCompatActivity {
private ArrayList<Song> songList;
private ListView songView;
LinearLayout nextactivity;
SeekBar seekBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
songView = findViewById(R.id.song_list);
songList = new ArrayList<Song>();
seekBar=findViewById(R.id.seekBar2);
getSongList();
Collections.sort(songList, new Comparator<Song>(){
public int compare(Song a, Song b){
return a.getTitle().compareTo(b.getTitle());
}
});
SongAdapter songAdt = new SongAdapter(this, songList);
songView.setAdapter(songAdt);
nextactivity=findViewById(R.id.mediacontroller);
nextactivity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
});
songView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
//song info method retrive
public void getSongList() {
//retrieve song info
ContentResolver musicResolver = getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
if (musicCursor != null && musicCursor.moveToFirst()) {
//get columns
int titleColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
int artistColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
int dataColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DATA);
//add songs to list
do {
long thisId = musicCursor.getLong(idColumn);
String thisTitle = musicCursor.getString(titleColumn);
String thisArtist = musicCursor.getString(artistColumn);
String thisPath = musicCursor.getString(dataColumn);
songList.add(new Song(thisId, thisTitle, thisArtist, thisPath));
}
while (musicCursor.moveToNext());
}
}
}
package music.player.musicplayer;
public class Song {
private long id;
private String title;
private String artist;
private String getpath;
public long getId() {
return id;
}
public String getTitle() {
return title;
}
public String getArtist() {
return artist;
}
public String getGetpath() {
return getpath;
}
public Song(long id, String title, String artist, String getpath ) {
this.id = id;
this.title = title;
this.artist = artist;
this.getpath = getpath;
}
}
package music.player.musicplayer;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
class SongAdapter extends BaseAdapter{
private ArrayList<Song> songs;
private LayoutInflater songInf;
public SongAdapter(Context c, ArrayList<Song> theSongs){
songs=theSongs;
songInf=LayoutInflater.from(c);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return songs.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
RelativeLayout songLay = (RelativeLayout)songInf.inflate(R.layout.song, parent, false);
//get title and artist views
TextView songView = (TextView)songLay.findViewById(R.id.song_title);
TextView artistView = (TextView)songLay.findViewById(R.id.song_artist);
//get song using position
Song currSong = songs.get(position);
//get title and artist strings
songView.setText(currSong.getTitle());
artistView.setText(currSong.getArtist());
//set position as tag
songLay.setTag(position);
return songLay;
}
}
Here is my activities picture you get idea what i actually i want
actity two:

Categories