Andre Viau
Andre Viau

Reputation: 275

Adding Items to a list view in android

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>

Upvotes: 4

Views: 32314

Answers (1)

Ishu
Ishu

Reputation: 5457

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

Upvotes: 6

Related Questions