So I am reading Commonsware’s Android Programming Tutorials and I am stuck with the part where the book asks me to add a ListView. Here is my layout’s xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<RelativeLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TableLayout
android:id="@+id/details"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:stretchColumns="1">
<TableRow>
<TextView android:text="Name:"/>
<EditText android:id="@+id/name"/>
</TableRow>
<TableRow>
<TextView android:text="Address:"/>
<EditText android:id="@+id/address"/>
</TableRow>
<TableRow>
<TextView android:text="Type: "/>
<RadioGroup android:id="@+id/types">
<RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
android:id="@+id/take_out" android:text="Take-Out"/>
<RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
android:id="@+id/sit_down" android:text="Sit-Down"/>
<RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
android:id="@+id/delivery" android:text="Delivery"/>
</RadioGroup>
</TableRow>
<Button android:id="@+id/save"
android:text="Save"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
</TableLayout>
<ListView android:id="@+id/restaurants"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_above="@id/details"
/>
</RelativeLayout>
</ScrollView>
and this is my activity code
package com.example;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;
import java.util.LinkedList;
import java.util.List;
public class LunchList extends Activity {
List<Restaurant> model = new LinkedList<Restaurant>();
ArrayAdapter<Restaurant> arrayAdapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button save = (Button) findViewById(R.id.save);
save.setOnClickListener(onSave);
ListView listView = (ListView) findViewById(R.id.restaurants);
arrayAdapter = new ArrayAdapter<Restaurant>(this, android.R.layout.simple_list_item_1, model);
listView.setAdapter(arrayAdapter);
}
private View.OnClickListener onSave = new View.OnClickListener(){
public void onClick(View view) {
EditText nameField = (EditText) findViewById(R.id.name);
EditText addressField = (EditText) findViewById(R.id.address);
Restaurant restaurant = new Restaurant();
restaurant.setName(nameField.getText().toString());
restaurant.setAddress(addressField.getText().toString());
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.types);
switch (radioGroup.getCheckedRadioButtonId()) {
case (R.id.take_out):
restaurant.setType("take_out");
break;
case (R.id.sit_down):
restaurant.setType("sit_down");
break;
case (R.id.delivery):
restaurant.setType("delivery");
break;
case (R.id.korean):
restaurant.setType("korean");
break;
case (R.id.chinese):
restaurant.setType("chinese");
break;
case (R.id.japanese):
restaurant.setType("japanese");
break;
case (R.id.italian):
restaurant.setType("italian");
break;
case (R.id.indonesian):
restaurant.setType("indonesian");
break;
}
arrayAdapter.add(restaurant);
Log.i("LunchList", "Array Adapter Size: " + arrayAdapter.getCount());
}
};
}
I added a logging line to see whether the object is being added to the adapter or not and it looks like it is being added in there. The UI however is not showing the ListView and I do not see stuff getting added in the list.
Edit XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TableLayout
android:id="@+id/details"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:stretchColumns="1">
<TableRow>
<TextView android:text="Name:"/>
<EditText android:id="@+id/name"/>
</TableRow>
<TableRow>
<TextView android:text="Address:"/>
<EditText android:id="@+id/address"/>
</TableRow>
<TableRow>
<TextView android:text="Type: "/>
<RadioGroup android:id="@+id/types">
<RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
android:id="@+id/take_out" android:text="Take-Out"/>
<RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
android:id="@+id/sit_down" android:text="Sit-Down"/>
<RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
android:id="@+id/delivery" android:text="Delivery"/>
</RadioGroup>
</TableRow>
<Button android:id="@+id/save"
android:text="Save"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
</TableLayout>
<ListView android:id="@+id/restaurants"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="@id/details"
/>
</RelativeLayout>
</ScrollView>
As far as I can see, you initialise model:
But don’t put any content in it, so there is nothing for your list to show
EDIT: If you are adding content to your list dynamically, make sure that you are updating the list like this:
Or:
notifyDataSetChanged()lets theListViewknow that the list contents have changed and it should redraw itselfEDIT: Also, you are adding the
ListViewabove theTableLayoutcalleddetails, which has its heigh and width set tofill_parent, so you may not see theListViewif theTableLayoutis taking up the whole screen. Try changing the height ofdetailstowrap_content