I am currently working on a time table app with 5 days. My idea to do this was to place 5 linear layouts in a relative layout and the relative layout in a scroll view as it would not fit on the screen.
I made my xml:
<ScrollView 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"
tools:context="schouten.jamie.maantje.com.roosternotifierfriesepoort.Activities.RoosterActivity">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/maandag"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/dinsdag"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/woensdag"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/donderdag"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/vrijdag"
android:orientation="vertical">
</LinearLayout>
</RelativeLayout>
</ScrollView>
After that i started writing the code:
for (int i = 0; i < maandag.length(); i++) {
JSONObject maandagJSONObject = maandag.getJSONObject(i);
LayoutInflater inflater = (LayoutInflater)getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout main =(LinearLayout)findViewById(R.id.maandag);
View view = inflater.inflate(R.layout.roosteruur, main, false);
TextView leerkracht = (TextView)view.findViewById(R.id.leerkracht);
TextView les = (TextView)view.findViewById(R.id.vak);
TextView lokaal = (TextView)view.findViewById(R.id.lokaal);
TextView uur = (TextView)view.findViewById(R.id.uur);
uur.setText(String.valueOf(i + 1)+")");
lokaal.setText(maandagJSONObject.getString("lokaal"));
les.setText(maandagJSONObject.getString("les"));
leerkracht.setText(maandagJSONObject.getString("leraar"));
Log.v("roosterp", maandagJSONObject.getString("les"));
main.addView(view);
}
Ran it and it worked great so i thought copy and paste that 4 more times and change the days and done!
So i did just that:
public void parseJSON(String json){
Log.v("JPARSER","parseJSON start");
try {
JSONObject objects = new JSONObject(json);
JSONArray maandag = objects.getJSONArray("maandag");
JSONArray dinsdag = objects.getJSONArray("dinsdag");
JSONArray woensdag = objects.getJSONArray("woensdag");
JSONArray donderdag = objects.getJSONArray("donderdag");
JSONArray vrijdag = objects.getJSONArray("vrijdag");
for (int i = 0; i < maandag.length(); i++) {
JSONObject maandagJSONObject = maandag.getJSONObject(i);
LayoutInflater inflater = (LayoutInflater)getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout main =(LinearLayout)findViewById(R.id.maandag);
View view = inflater.inflate(R.layout.roosteruur, main, false);
TextView leerkracht = (TextView)view.findViewById(R.id.leerkracht);
TextView les = (TextView)view.findViewById(R.id.vak);
TextView lokaal = (TextView)view.findViewById(R.id.lokaal);
TextView uur = (TextView)view.findViewById(R.id.uur);
uur.setText(String.valueOf(i + 1)+")");
lokaal.setText(maandagJSONObject.getString("lokaal"));
les.setText(maandagJSONObject.getString("les"));
leerkracht.setText(maandagJSONObject.getString("leraar"));
Log.v("roosterp", maandagJSONObject.getString("les"));
main.addView(view);
}
for (int i = 0; i < dinsdag.length(); i++) {
JSONObject dinsdagJSONObject = dinsdag.getJSONObject(i);
LayoutInflater inflater = (LayoutInflater)getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout main =(LinearLayout)findViewById(R.id.maandag);
View view = inflater.inflate(R.layout.roosteruur, main, false);
TextView leerkracht = (TextView)view.findViewById(R.id.leerkracht);
TextView les = (TextView)view.findViewById(R.id.vak);
TextView lokaal = (TextView)view.findViewById(R.id.lokaal);
TextView uur = (TextView)view.findViewById(R.id.uur);
uur.setText(String.valueOf(i + 1)+")");
lokaal.setText(dinsdagJSONObject.getString("lokaal"));
les.setText(dinsdagJSONObject.getString("les"));
leerkracht.setText(dinsdagJSONObject.getString("leraar"));
Log.v("roosterp", dinsdagJSONObject.getString("les"));
main.addView(view);
}
for (int i = 0; i < woensdag.length(); i++) {
JSONObject woensdagJSONObject = woensdag.getJSONObject(i);
LayoutInflater inflater = (LayoutInflater)getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout main =(LinearLayout)findViewById(R.id.maandag);
View view = inflater.inflate(R.layout.roosteruur, main, false);
TextView leerkracht = (TextView)view.findViewById(R.id.leerkracht);
TextView les = (TextView)view.findViewById(R.id.vak);
TextView lokaal = (TextView)view.findViewById(R.id.lokaal);
TextView uur = (TextView)view.findViewById(R.id.uur);
uur.setText(String.valueOf(i + 1)+")");
lokaal.setText(woensdagJSONObject.getString("lokaal"));
les.setText(woensdagJSONObject.getString("les"));
leerkracht.setText(woensdagJSONObject.getString("leraar"));
Log.v("roosterp", woensdagJSONObject.getString("les"));
main.addView(view);
}
for (int i = 0; i < donderdag.length(); i++) {
JSONObject donderdagJSONObject = donderdag.getJSONObject(i);
LayoutInflater inflater = (LayoutInflater)getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout main =(LinearLayout)findViewById(R.id.maandag);
View view = inflater.inflate(R.layout.roosteruur, main, false);
TextView leerkracht = (TextView)view.findViewById(R.id.leerkracht);
TextView les = (TextView)view.findViewById(R.id.vak);
TextView lokaal = (TextView)view.findViewById(R.id.lokaal);
TextView uur = (TextView)view.findViewById(R.id.uur);
uur.setText(String.valueOf(i + 1)+")");
lokaal.setText(donderdagJSONObject.getString("lokaal"));
les.setText(donderdagJSONObject.getString("les"));
leerkracht.setText(donderdagJSONObject.getString("leraar"));
Log.v("roosterp", donderdagJSONObject.getString("les"));
main.addView(view);
}
for (int i = 0; i < vrijdag.length(); i++) {
JSONObject vrijdagJSONObject = vrijdag.getJSONObject(i);
LayoutInflater inflater = (LayoutInflater)getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout main =(LinearLayout)findViewById(R.id.maandag);
View view = inflater.inflate(R.layout.roosteruur, main, false);
TextView leerkracht = (TextView)view.findViewById(R.id.leerkracht);
TextView les = (TextView)view.findViewById(R.id.vak);
TextView lokaal = (TextView)view.findViewById(R.id.lokaal);
TextView uur = (TextView)view.findViewById(R.id.uur);
uur.setText(String.valueOf(i + 1)+")");
lokaal.setText(vrijdagJSONObject.getString("lokaal"));
les.setText(vrijdagJSONObject.getString("les"));
leerkracht.setText(vrijdagJSONObject.getString("leraar"));
Log.v("roosterp", vrijdagJSONObject.getString("les"));
main.addView(view);
}
}
catch (JSONException e){
Log.v("JPARSER","WUT? " + e);
}
}
But the result was not wat i was expecting instead of 5 nice little linear layouts i got wat looks like 1 just mixed with the data of a couple days so i guess there is something that is being overwritten somewhere but can't find where.
Try replacing your RelativeLayout with LinearLayout and set its orientation to vertical.
LinearLayout will arrange your views automatically, but if you want to use a RelativeLayout, then you will have to add android:layout_below
attribute to each view you have inside that RelativeLayout. So LinearLayout is obviously more simple.