ac4android
Well-Known Member
Hi.
Problem: AVD not displaying the detail from the fragment!
Addendum: Android Monitor and Event log did NOT log any errors!
The display on AVD should be like this:
Core Agony
100 Pull-ups
100 Push-ups
100 Sit-ups
100 Squats
I am following some codes, the objective is to display an array of gym workouts and detail of each of the workouts. For testing, I hard-coded Java to return the detail for array index 1
Both the Java and the XML seem alright to me, however, the AVD displays only the workout e.g. "Core Agony", but does not display the detail.
I am using Nexus 4 API21 for this gradle.
I've pulled the XML from content_main.XML into activity_main.XML because it is too much trouble for testing purposes.
Here are the java constructors and getters from Workout.java:
MainActivity.java and its XML file follow, this is where I use a setter to hardcode the array index position to 1:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android
rientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
class="com.hfad.workout.WorkoutDetailFragment"
android:id="@+id/detail_frag"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment_workout_detail" />
</LinearLayout>
The fragment WorkoutDetailFragment.java and its XML file follow:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android
rientation="vertical"
tools:context="com.hfad.workout.WorkoutDetailFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/textTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@String/textTitle" />
<TextView
android:id="@+id/textDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@String/textDescription"/>
</LinearLayout>
Problem: AVD not displaying the detail from the fragment!
Addendum: Android Monitor and Event log did NOT log any errors!
The display on AVD should be like this:
Core Agony
100 Pull-ups
100 Push-ups
100 Sit-ups
100 Squats
I am following some codes, the objective is to display an array of gym workouts and detail of each of the workouts. For testing, I hard-coded Java to return the detail for array index 1
Both the Java and the XML seem alright to me, however, the AVD displays only the workout e.g. "Core Agony", but does not display the detail.
I am using Nexus 4 API21 for this gradle.
I've pulled the XML from content_main.XML into activity_main.XML because it is too much trouble for testing purposes.
Here are the java constructors and getters from Workout.java:
Code:
public class Workout {
private String name;
private String description;
public static final Workout[] workouts = {
new Workout("The Limb Loosener", "5 Handstand push-ups\n10 1-legged squats\n15 Pull-ups"),
new Workout("Core Agony", "100 Pull-ups\n100 Push-ups\n100 Sit-ups\n100 Squats"),
};
// CONSTRUCTOR: each workout has a name and description
private Workout(String name, String description){
this.name = name;
this.description = description;
}
// GETTERS for the private variables
public String getName(){
return name;
}
public String getDescription() {
return description;
}
public String toString(){
return name;
}
}
Code:
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WorkoutDetailFragment frag = (WorkoutDetailFragment) getFragmentManager().findFragmentById(R.id.detail_frag); // my reference to the fragment
frag.setWorkout(1); // hardcode the index position to 1
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android

android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
class="com.hfad.workout.WorkoutDetailFragment"
android:id="@+id/detail_frag"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment_workout_detail" />
</LinearLayout>
The fragment WorkoutDetailFragment.java and its XML file follow:
Code:
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* A simple {@link Fragment} subclass.
*/
public class WorkoutDetailFragment extends Fragment {
private long workoutId;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_workout_detail, container, false);
}
@Override
public void onStart() {
super.onStart();
View view = getView();
if (view != null) {
Workout workout = Workout.workouts[(int) workoutId];
// Get the textTitle from fragment_workout_detail.XML
TextView title = (TextView) view.findViewById(R.id.textTitle);
title.setText(workout.getName());
// Get the textDescription from the XML
TextView description = (TextView) view.findViewById(R.id.textDescription);
description.setText(workout.getDescription());
}
}
public void setWorkout(long id){
this.workoutId = id;
}
}
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android

tools:context="com.hfad.workout.WorkoutDetailFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/textTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@String/textTitle" />
<TextView
android:id="@+id/textDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@String/textDescription"/>
</LinearLayout>
Last edited: