开发者

how to include list along side other UI elements? (Android)

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 e开发者_开发技巧xtends 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 (:


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(...)


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜