Hi All,

I have an Absolute Layout with two buttons on top, a Text View of
fixed size in the middle and List View as the rest of the Layout.

I want to use the top left button to add a list view item to the
existing list and the top right button to remove the last item from
the list. The List is layed out properly when the App starts, but as
soon as any of the two buttons is pressed, the Activity Force Closes.

Here is the code:


package com.sample.headerlist;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.HeaderViewListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class headerlist extends Activity implements
View.OnClickListener
{
        private ListView                listView;
         private List<String>    strings;

        @Override
        public void onCreate(Bundle savedInstanceState)
        {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);

                Button additem = (Button) findViewById(R.id.add_item);
                Button deleteitem = (Button) findViewById
(R.id.del_item);

                 listView = (ListView) findViewById(R.id.listView);

                additem.setOnClickListener(this);
                deleteitem.setOnClickListener(this);

                strings = new ArrayList<String>();
                strings.add("First");
                strings.add("Second");
                strings.add("Third");
                listView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, strings));
        }

        @SuppressWarnings("unchecked")
        @Override
        public void onClick(View view)
        {
                if(view.getId() == R.id.add_item)
                {
                        strings.add("another one");
                        ((ArrayAdapter<String>)
((HeaderViewListAdapter) listView.getAdapter()).getWrappedAdapter
()).notifyDataSetChanged();
                }
                else if(view.getId() == R.id.del_item)
                {
                        if(strings.size() > 0)
                        {
                                strings.remove(strings.size() - 1);
                                ((ArrayAdapter<String>)
((HeaderViewListAdapter)listView.getAdapter()).getWrappedAdapter
()).notifyDataSetChanged();
                        }
                }
        }
}


main.xml:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android";>

<Button
android:id="@+id/add_item"
android:layout_width="120px"
android:layout_height="wrap_content"
android:text="Add Item"
android:layout_x="0px"
android:layout_y="0px"
>
</Button>

<Button
android:id="@+id/del_item"
android:layout_width="120px"
android:layout_height="wrap_content"
android:text="Delete Item"
android:layout_x="159px"
android:layout_y="0px"
>
</Button>

<TextView

android:layout_width="315px"
android:layout_height="25px"
android:text="SEND TO:"
android:textSize="20px"
android:gravity="center_horizontal"
android:layout_x="2px"
android:layout_y="54px"
>
</TextView>

<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_x="0px"
android:layout_y="82px"
>
</ListView>

</AbsoluteLayout>


Please Help.

Abhishek

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to