1

I'm new to android developing, so i apologise if this is a simple/noob-ish question, and for any incorrect terminology.

but what i need to know is how can i include a list alongside of other UI elements (such as TextView, ImageView elements etc)

upto now, all i have been able to achieve is a list activity all on its own, which to do this i have been using the ListActivity class type.

My list activity:

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class ListViewExample extends ListActivity 
{
    String[] exampleList = {
            "Item 1",
            "Item 2",
            "Item 3"
            //etc etc
    };

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

        setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, exampleList));
    }
}

Which is started within my Main class/activity:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

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

        startActivity(new Intent( this, ListViewExample.class));
    }
}

but with the function of "startActivity()", this seems to just switch to that activity, and not "include" it to the current, which of-corse means that any elements within "R.layout.main" (defined above the calling of "startActivity()) are not shown.

Is there anyway to include this activity within my main activity? or is there a better way of making a list?

(my goal will eventually be to make the list array dynamic, just thought id say in case that affected on any suggested solution).

thanks for any help (:

92Jacko
  • 523
  • 2
  • 10
  • 18

2 Answers2

1

Using the startActivity to start your ListViewExample starts a whole new activity (with a whole new view) and puts it on top of the stack. When you click the back button, then your main activity will be displayed. Please see this link to learn more about the activity lifecycle.

It sounds like what you want to do is define some other UI elements alongside your listview. I dont know if you can do this on the SIDE, but I know you can include buttons/textviews on top or bottom of a listview. See this post as a good example of how to put a button below a listview.

EDIT: As an example (taken from the second link), you would do something like this:

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

    <Button android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:id="@+id/testbutton"
        android:text="@string/hello" android:layout_alignParentBottom="true" />

    <ListView android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:id="@+id/list"
        android:layout_alignParentTop="true" android:layout_above="@id/testbutton" />

</RelativeLayout>

Now you could also put that button on bottom if you wanted, or include a textbox on the top and a button on bottom.

Yes as Espiandev said, you would want your main activity to extend Activity. Then in your XML you would have the above. The way you would get your listview to bind to would be

ListView lv = (ListView)findViewById(R.id.list);

Then you could bind to it:

lv.setAdapter(...)

Community
  • 1
  • 1
Jack
  • 9,156
  • 4
  • 50
  • 75
  • hi, thanks for the suggestion (: the first solution works (: but as for the second, i get a force close D: any ideas as to why? thanks again (: – 92Jacko Aug 02 '11 at 19:28
  • Maybe because you are calling setContentView(R.layout.listview) ? You should set the contentview to whatever the main xml filename is. Like if using main.xml, use R.layout.main. – Jack Aug 02 '11 at 19:34
  • got it working now after copying and pasting yours and the below's edits, so i must have had a small typo somewhere. thanks again for you time (: – 92Jacko Aug 02 '11 at 19:35
0

An alternative to the other answer, which is probably more scalable, is to simply make ListViewExample extend a basic Activity. Then, in the onCreate() method, retrieve the ListView by using findViewById() and then use setAdapter() on this. For example, if your ListView was given the id listview1:

public class ListViewExample extends Activity {
    String[] exampleList = {
            "Item 1",
            "Item 2",
            "Item 3"
            //etc etc
    };

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

        // Get an instance of your listview in code
        ListView listview = (ListView) findViewById(R.id.listview1);

        // Set the listview's adapter
        listview.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, exampleList));
    }
}

This will give you the flexibility to have a layout with more than just a listview in it

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
Alex Curran
  • 8,818
  • 6
  • 49
  • 55
  • I think you meant for ListViewExample to extend Activity rather than ListActivity – Jack Aug 02 '11 at 19:07
  • Is this line "ListView listview = (ListView) findViewById(R.id.listview1);" refering to the layout/style of the list within R.layout.listview? if so, after changing the id (listview1) to match the one i was using, i got a force close error. any ideas as to why? thanks (: – 92Jacko Aug 02 '11 at 19:26
  • Ahh, its working now, i copied the code directly and it work, so i must have had a small error somewhere, thanks again (: – 92Jacko Aug 02 '11 at 19:33
  • Yeah, the R.id.listview1 refers to the id of a listview itself, not the layout. Just put a listview in the layout and give it the id listview1, should work perfectly. If this is your accepted answer, please put a tick next to it :) – Alex Curran Aug 02 '11 at 19:52