I see you're using two ListViews at one time... Out of curiosity, are
you controlling them both with the same ListActivity?

I would recommend pursuing my method as it cuts down on resource use--
displaying and managing one ListView with a header will be lighter
than two entirely separate ListViews. Just because it works doesn't
mean it's the best approach. Header views is the way stock Android
apps [built by Googlers] achieve this effect... I'd follow suit.

-Nick




On Apr 6, 3:04 pm, Simon Donovan <[email protected]> wrote:
> Hi Nick,
>
> Thanks for replying.  I managed to figure it out.  I didn't use the
> addHeaderView method in the end.
> This is my code....
>
> Simon
>
> main.xml
>
> <?xml version="1.0" encoding="utf-8"?>
> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/
> android"
>         android:layout_width="fill_parent"
>         android:layout_height="wrap_content"
>         android:orientation="vertical">
>         <ListView
>                 android:id="@+id/headerList"
>                 android:layout_width="fill_parent"
>                 android:layout_height="wrap_content">
>         </ListView>
>         <View
>                 android:layout_width="fill_parent"
>                 android:layout_height="2dip"
>                 android:background="#FFFFFFFF" />
>         <ListView
>                 android:id="@android:id/list"
>                 android:layout_width="fill_parent"
>                 android:layout_height="wrap_content">
>         </ListView>
> </LinearLayout>
>
> header_row.xml
>
> <?xml version="1.0" encoding="utf-8"?>
> <LinearLayout
>         xmlns:android="http://schemas.android.com/apk/res/android";
>         android:layout_width="fill_parent"
>         android:layout_height="wrap_content">
>         <ImageView
>         android:id="@+id/icon"
>         android:layout_width="wrap_content"
>         android:layout_height="fill_parent"
>         android:layout_marginRight="6dip"
>         android:src="@drawable/ic_menu_add" />
>         <LinearLayout
>         android:orientation="vertical"
>         android:layout_width="0dip"
>         android:layout_weight="1"
>         android:layout_height="fill_parent">
>          <TextView
>             android:id="@+id/caption"
>             android:layout_width="fill_parent"
>             android:layout_height="0dip"
>             android:layout_weight="1"
>             android:gravity="center_vertical"
>             android:textAppearance="?android:attr/textAppearanceLarge"/
>
>         </LinearLayout>
> </LinearLayout>
>
> public class HeaderFooterDemo extends ListActivity
> {
>         private static String[] items={"lorem", "ipsum", "dolor",
>                                                                               
>                                                           "sit", "amet", 
> "consectetuer",
>                                                                               
>                                                           "adipiscing", 
> "elit", "morbi",
>                                                                               
>                                                           "vel", "ligula", 
> "vitae",
>                                                                               
>                                                           "arcu", "aliquet", 
> "mollis",
>                                                                               
>                                                           "etiam", "vel", 
> "erat",
>                                                                               
>                                                           "placerat", "ante",
>                                                                               
>                                                           "porttitor", 
> "sodales",
>                                                                               
>                                                           "pellentesque", 
> "augue", "purus"};
>
>         private ArrayList arraylist;
>         private ArrayList arrayListHeader;
>         private ListView list;
>         private ListView headerList;
>         private ArrayAdapter listAdapter;
>         private ArrayAdapter listAdapterHeader;
>         private ArrayList<Caption> caption = null;
>         private CaptionAdapter adapter;
>
>         @Override
>         public void onCreate(Bundle icicle) {
>                 super.onCreate(icicle);
>                 setContentView(R.layout.main);
>
>                 list = (ListView) findViewById(android.R.id.list);
>
>         listAdapter = new ArrayAdapter<String>(this,
> android.R.layout.simple_list_item_1, items);
>
>         list.setAdapter(listAdapter);
>
>         listAdapter.notifyDataSetChanged();
>
>         caption = new ArrayList<Caption>();
>         Caption cap = new Caption();
>         cap.setCaption("Add a place");
>         caption.add(cap);
>
>         this.adapter = new CaptionAdapter(this, R.layout.header_row,
> caption);
>
>         headerList = (ListView) findViewById(R.id.headerList);
>
>         headerList.setAdapter(adapter);
>
>         adapter.notifyDataSetChanged();
>         }
>
>         private class CaptionAdapter extends ArrayAdapter<Caption>
>     {
>         private ArrayList<Caption> caption;
>
>                 public CaptionAdapter(Context context, int textViewResourceId,
> ArrayList<Caption> caption)
>                 {
>                         super(context, textViewResourceId, caption);
>                         this.caption = caption;
>                 }
>
>                 @Override
>             public View getView(int position, View convertView, ViewGroup
> parent)
>             {
>                         View v = convertView;
>
>                         if (v == null)
>             {
>                 LayoutInflater vi =
> (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
>                 v = vi.inflate(R.layout.header_row, null);
>             }
>
>                         Caption c = caption.get(position);
>
>                         if (c != null)
>             {
>                                 TextView caption = (TextView) 
> v.findViewById(R.id.caption);
>
>                                 if (caption != null)
>                 {
>
> caption.setText(c.getCaption());
>                 }
>             }
>
>                         return v;
>             }
>
>     }
>
> }
>
> On Apr 5, 11:31 pm, patbenatar <[email protected]> wrote:
>
>
>
> > Dig deeper in the ListView documentation... Look into adding a header
> > view [the method is like ListView.addHeaderView(View) or something..
> > verify this before using it].. You can use the header view for the +
> > Add A Place link and then the contents of the List will be the Places.
>
> > Hope this helps!
> > Nick
>
> > On Apr 5, 1:36 pm, Simon Donovan <[email protected]> wrote:
>
> > > Hi,
>
> > > I'm trying to recreate the UI screen called 'My Places' that is used
> > > in the Weather Channel app.  I'd attach a screenshot of it but I'm not
> > > sure if I can when posting here.  It seems they're using two listviews
> > > one on top of the other, but I'm not sure for certain.  Could anybody
> > > confirm this for me?  If they are doing this, how is this done?  I've
> > > tried to implement this, but without full success.  My top listview
> > > 'Add a place' 'comes up correctly, but the bottom listview will not
> > > appear/populate for me?  I shall attach my code so far......
>
> > > Any help would be greatly appreciated.
>
> > > Thanks
>
> > > Simon
>
> > > header_row.xml
>
> > > ?xml version="1.0" encoding="utf-8"?>
> > > <LinearLayout
> > >         xmlns:android="http://schemas.android.com/apk/res/android";
> > >         android:layout_width="fill_parent"
> > >         android:layout_height="wrap_content">
> > >         <ImageView
> > >         android:id="@+id/icon"
> > >         android:layout_width="wrap_content"
> > >         android:layout_height="fill_parent"
> > >         android:layout_marginRight="6dip"
> > >         android:src="@drawable/ic_menu_add" />
> > >         <LinearLayout
> > >         android:orientation="vertical"
> > >         android:layout_width="0dip"
> > >         android:layout_weight="1"
> > >         android:layout_height="fill_parent">
> > >          <TextView
> > >             android:id="@+id/caption"
> > >             android:layout_width="fill_parent"
> > >             android:layout_height="0dip"
> > >             android:layout_weight="1"
> > >             android:gravity="center_vertical"
> > >             android:text="Add a place"/>
> > >         </LinearLayout>
> > > </LinearLayout>
>
> > > main.xml
>
> > > <?xml version="1.0" encoding="utf-8"?>
> > >  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/
> > > android"
> > >         android:layout_width="fill_parent"
> > >         android:layout_height="?android:attr/listPreferredItemHeight"
> > >     android:padding="6dip">
> > >         <ListView
> > >                 android:id="@+id/header"
> > >                 android:layout_width="wrap_content"
> > >                 android:layout_height="wrap_content"/>
> > >         <LinearLayout
> > >                 android:orientation="vertical"
> > >                 android:layout_width="fill_parent"
> > >                 android:layout_height="wrap_content">
> > >                 <ListView
> > >                         android:id="@+id/list"
> > >                         android:layout_width="fill_parent"
> > >                         android:layout_height="wrap_content"/>
> > >         </LinearLayout>
> > > </LinearLayout>
>
> > > public class ListViewTest extends Activity
> > > {
> > >         private static String[] items={"lorem", "ipsum", "dolor",
> > >                 "sit", "amet", "consectetuer",
> > >                 "adipiscing", "elit", "morbi",
> > >                 "vel", "ligula", "vitae",
> > >                 "arcu", "aliquet", "mollis",
> > >                 "etiam", "vel", "erat",
> > >                 "placerat", "ante",
> > >                 "porttitor", "sodales",
> > >                 "pellentesque", "augue",
> > >                 "purus"};
>
> > >         private ListView Header;
> > >         private ListView List;
> > >         private ArrayList<Caption> caption = null;
> > >         private CaptionAdapter adapter;
> > >         private ArrayAdapter listAdapter;
>
> > >     @Override
> > >     public void onCreate(Bundle savedInstanceState)
> > >     {
> > >         super.onCreate(savedInstanceState);
> > >         setContentView(R.layout.main);
>
> > >         caption = new ArrayList<Caption>();
> > >         Caption cap = new Caption();
> > >         cap.setCaption("Add a place");
> > >         caption.add(cap);
>
> > >         this.adapter = new CaptionAdapter(this, R.layout.header_row,
> > > caption);
>
> > >         Header = (ListView) findViewById(R.id.header);
>
> > >         Header.setAdapter(adapter);
>
> > >         //Log.d("ListViewTest", "caption size is:" + caption.size());
>
> > >         adapter.notifyDataSetChanged();
>
> > >         List = (ListView) findViewById(R.id.list);
>
> > >         listAdapter = new ArrayAdapter<String>(this,
> > > android.R.layout.simple_list_item_1, items);
>
> > >         List.setAdapter(listAdapter);
>
> > >         listAdapter.notifyDataSetChanged();
>
> > >         //setListAdapter(new ArrayAdapter<String>(this,
> > >                                 //android.R.layout.simple_list_item_1,
> > >                                 //items));
> > >     }
>
> > >     private class CaptionAdapter extends ArrayAdapter<Caption>
> > >     {
> > >         private ArrayList<Caption> caption;
>
> > >                 public CaptionAdapter(Context context, int 
> > > textViewResourceId,
> > > ArrayList<Caption> caption)
> > >                 {
> > >                         super(context, textViewResourceId, caption);
> > >                         this.caption = caption;
> > >                 }
>
> > >                 @Override
> > >             public View getView(int position, View convertView, ViewGroup
>
> ...
>
> read more »

-- 
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

To unsubscribe, reply using "remove me" as the subject.

Reply via email to