Fattie
Fattie

Reputation: 12635

Put a ListActivity "in" my main activity view

I will try to phrase this question more succinctly:

Say I have a completely functioning ListActivty say .. say, GreenListActivity.

How do I "put that" GreenListActivity inside another existing activity layout ? Thanks!


My android MainActivity has an xml file activity_main.xml, like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                tools:context="com.client.app.MainActivity">

    // some other images, buttons widgets, etc here

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_container"
        xmlns:android="http://schemas.android.com/apk/res/android">

        <ListView
            android:id="@+id/feedView"
            android:layout_width="wrap_content"
            ..etc..
            android:scrollbars="none"/>

    </android.support.v4.widget.SwipeRefreshLayout>

    // some other images, buttons widgets, etc here

</RelativeLayout>

It all works well, the table is complex, everything works great. So simply IN MY MAIN ACTIVITY, I have...

feed = (ListView) findViewById(R.id.feedView);
superDap = new SuperAdapter(this, getLayoutInflater());
feed.setAdapter(superDap);
superDap.notifyDataSetChanged();

and all the other code relating to the table and cell handling.

Now, I roughly understand ListActivity so I've made a new class...

public class MainFeedActivity extends ListActivity

I did put that in the manifest.

Now, I understand that generally in the main activity, I will launch the new list activity like this:

Intent feedIntent = new Intent(MainActivity.this, MainFeedActivity.class);
startActivity( feedIntent );

My confusion,

Question one, in activity_main.xml what do I do to tell it that where I currently have <ListView android:id="@+id/feedView" in fact I want to "put in" my new MainFeedActivity ?

Question two in fact what should the xml file for MainFeedActivity be like? (Should there be one??)

Sorry, I found many great tutorials fully explaining "how to make a ListActivity",

but,

much to my shame I can't learn how to "put that list" inside the main activity page. That is to say "in" the xml of the main activity. Or am I just doing it all wrong? (Should the list NOT BE a new activity, maybe?!!) Thanks for any pointers! Phew

Upvotes: 0

Views: 1251

Answers (3)

Michele La Ferla
Michele La Ferla

Reputation: 6884

This is how the xml file should be built:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="209dp" >
</ListView>

<TextView
    android:id="@+id/group"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

//add any additional widgets here
</LinearLayout>

In the classes, I would use fragments to implement the above:

public static class MainFeedFragment extends ListFragment {

    public void onStart() {
        super.onStart();

        // selecting single ListView item
        ListView lv = getListView();

        // Lauching the Event details screen on selecting a single event
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // getting values from selected ListItem
                String ID = ((TextView) view.findViewById(R.id.leader_id))
                        .getText().toString();

                Intent intent = new Intent(view.getContext(),
                        CoreTeamDetails.class);
                intent.putExtra(pid, ID);
                view.getContext().startActivity(intent);
            }
        });
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.main,container, false);

        //added code to show how adapter connects to list
        ListAdapter adapter = new SimpleAdapter(getActivity(),membersList,R.layout.coreteam_item,

        setListAdapter(adapter);
        return rootView;
        }
    }

Hope this helps you.

Upvotes: 1

Phant&#244;maxx
Phant&#244;maxx

Reputation: 38098

A ListView can be shown using different methods.

You might use a ListActivity or a ListFragment (if you are to use Fragments).
In this case, the list hase to be named list (android:id="@android:id/list").
Android will recognize it and manage it.

OR (and I do prefere this one), use a ListView in your design.
In this case the ListView can have any id (such as android:id="@+id/feedView").
Now you have to thell Android to finde the ListView by id.

In the same activity where you have the ListView, you then programmatically create an adapter, fill it and assign it to your ListView.

I hope I cleared your doubts (at least in part).

Anyway, here's a great tutorial on ListViews by Lars Vogel: http://www.vogella.com/tutorials/AndroidListView/article.html

Upvotes: 2

elmorabea
elmorabea

Reputation: 3263

I think you're a bit confused about what should be done.

If i understand correctly you have a MainActivity, which somehow navigates to a different activity that has a list.

  • Your main activity has an XML file which defines its own layout, which represents the home screen.
  • Your FeedList activity which has a different XML that defines its layout as well.

Those two are different and separate from eachother, when you navigate from HomeActivity to FeedList activity using intents as you mentioned, your new activity will be launched and it's XML will inflated and presented on the screen, so there is no need to put the list 'in' the home activity.

Note: use a normal Activity not a ListActivity and define the ListView you want in the xml of that activity.

feel free to ask if anything is unclear

Upvotes: 2

Related Questions