4

Ok, I completed the tutoriel at this site, http://developer.android.com/resources/tutorials/views/hello-listview.html

My problem is that I want to add items to a list that is nested in a table layout. Like so. enter image description here

How do I do this??? With my current code, the app stops working when I select the desired page. Assume that myList exists... If I comment out the line setContentView(R.layout.log); the list is shown but without the layout I want.

public class LogActivity extends ListActivity{

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.log);

        ListView lv = getListView();
        lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myList));
        lv.setTextFilterEnabled(true);
    }
}

..

<?xml version="1.0" encoding="UTF-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:paddingTop="10dp" >

        <TextView
            android:id="@+id/txt_currentDate"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_span="6"
            android:gravity="center"
            android:text="@string/CurrentDate"
            android:textSize="18dp"
            android:textStyle="bold"
            android:typeface="serif" >
        </TextView>
    </TableRow>

    <TableRow
        android:id="@+id/progressBarRow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:paddingTop="5dp" >

        <ProgressBar
            android:id="@+id/DailyCalorieProgress"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_span="3"
            android:progress="80" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:paddingTop="5dp" >

        <TextView
            android:id="@+id/textView1"
            android:gravity="center"
            android:text="@string/CalorieLimit"
            android:textStyle="bold" >
        </TextView>

        <TextView
            android:id="@+id/textView2"
            android:gravity="center"
            android:text="@string/CalorieLimitData" >
        </TextView>

        <TextView
            android:id="@+id/textView3"
            android:gravity="center"
            android:text="@string/CaloriesLeft"
            android:textStyle="bold" >
        </TextView>

        <TextView
            android:id="@+id/textView4"
            android:gravity="center"
            android:text="@string/CaloriesLeftData" >
        </TextView>
    </TableRow>

    <TextView
        android:id="@+id/textView5"
        android:gravity="center_horizontal"
        android:text="@string/Line" >
    </TextView>

    <TableRow android:paddingTop="10dp" >

        <Button
            android:id="@+id/button1"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/CurrentFood" />

        <Button
            android:id="@+id/button2"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/CurrentExercises" />
    </TableRow>

    <!--<TableRow
        android:id="@+id/tableRow4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/txt_foodItem"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/foodItem"
            android:typeface="serif" >
        </TextView>

        <TextView
            android:id="@+id/txt_Ammount"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/ammount"
            android:typeface="serif" >
        </TextView>

        <TextView
            android:id="@+id/txt_calories"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/calories"
            android:typeface="serif" >
        </TextView>
    </TableRow>-->

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="203dp" >
    </ListView>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <Button
            android:id="@+id/btn_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="@string/remove" />

        <Button
            android:id="@+id/btn_remove"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/btn_add"
            android:text="@string/add" />
    </RelativeLayout>

</TableLayout>
Andre Viau
  • 275
  • 2
  • 5
  • 17

1 Answers1

6

Just replace the below lines with

ListView lv = getListView();
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myList));
lv.setTextFilterEnabled(true);

with

ListView lv = (ListView)findViewById(R.id.listView);
ArrayAdapter<String> myarrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myList);
lv.setAdapter(myarrayAdapter);
lv.setTextFilterEnabled(true);

now just add you new item in mylist and call

myarrayAdapter.notifyDataChanged();

also change the xml file list id and try

Ishu
  • 5,357
  • 4
  • 16
  • 17
  • If I comment out the line setContentView(R.layout.log); the list is shown, but without the desired layout. If I uncomment that line, the app stops working when I navigate to the log page. Any idea why it would do that? I replaced my code with yours, even though it seems to be the same thing. – Andre Viau Apr 01 '12 at 04:52
  • I swear I tried that already...It still does not work. Like I said, when I navigate to the log page, the app says, unfortunately, the app has stopped working. – Andre Viau Apr 01 '12 at 05:04
  • 03-30 03:19:01.940: E/AndroidRuntime(3717): java.lang.RuntimeException: Unable to start activity ComponentInfo{seg.android.calorie/seg.android.calorie.LogActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' – Andre Viau Apr 01 '12 at 05:23
  • I found the answer here, thanks alot for the help! http://stackoverflow.com/questions/3040374/runtime-exception-listview-whose-id-attribute-is-android-r-id-list – Andre Viau Apr 01 '12 at 05:35