can anyone tell me whats wrong or suggest the best way to populate a
ListView on runtime from one screen to another.
Please see my code below
1. the list always ends up empty
2. I get a invalid resource error in the end.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
    android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
    android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
<EditText
android:id="@+id/entry" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"/>
<Button     android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"   android:layout_below="@id/
entry"    android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"    android:text="Add to List" />
<Button   android:id="@+id/viewlist"
android:layout_width="wrap_content"
        android:layout_height="wrap_content"
android:layout_below="@id/ok"
        android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
        android:text="View List" />
</LinearLayout>

public class HelloWorld extends Activity {

    /** Called when the activity is first created. */

        public ListView lv;
        public ArrayList<String> arr = new ArrayList<String>();

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

        Button okButton = (Button) findViewById(R.id.ok);
        okButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                EditText et = (EditText) findViewById(R.id.entry);
                arr.add(et.getText().toString());
                Toast.makeText(HelloWorld.this, "Added: " + et.getText(),
Toast.LENGTH_SHORT).show();
                et.bringToFront();
            }
        });

        Button viewListButton = (Button) findViewById(R.id.viewlist);
        viewListButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                setContentView(R.layout.text);
                lv = (ListView) findViewById(R.id.list);
                lv.setAdapter(new
ArrayAdapter<String>(HelloWorld.this, R.id.list, arr));
            }
        });
    }
}

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

Reply via email to