ListActivity and Custom Layout

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

ListActivity and Custom Layout

Postby wirelessdesigns » Mon Jan 04, 2010 11:20 pm

Hi all -

I'm implementing a very basic ListActivity, but something is hiding in my code that's keeping it from working. I have the following layout, called category.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout
  5.  
  6.  xmlns:android="http://schemas.android.com/apk/res/android"
  7.  
  8.  android:layout_width="fill_parent"
  9.  
  10.  android:layout_height="fill_parent"
  11.  
  12. >
  13.  
  14.         <TextView
  15.  
  16.                 android:id="@+id/vehiclesLabel"
  17.  
  18.                 android:layout_width="fill_parent"
  19.  
  20.                 android:layout_height="wrap_content"
  21.  
  22.                 android:gravity="center_horizontal"
  23.  
  24.                 android:text="Manage Your Trip Categories"
  25.  
  26.         />
  27.  
  28.         <ListView
  29.  
  30.                 android:id="@android:id/list"
  31.  
  32.                 android:layout_width="fill_parent"
  33.  
  34.                 android:layout_height="fill_parent"
  35.  
  36.                 android:drawSelectorOnTop="false"
  37.  
  38.                 android:background="#00FF00"
  39.  
  40.         />
  41.  
  42.         <TextView
  43.  
  44.                 android:id="@android:id/empty"
  45.  
  46.                 android:layout_width="fill_parent"
  47.  
  48.                 android:layout_height="fill_parent"
  49.  
  50.                 android:background="#FF0000"
  51.  
  52.                 android:text="No Categories"
  53.  
  54.         />
  55.  
  56. </LinearLayout>
  57.  
  58.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


The code for the ListActivity is:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class CategoryViewController extends ListActivity {
  3.  
  4.        
  5.  
  6.         String[] FAKE_DATA = {"String 1","String 2","String 3"};
  7.  
  8.        
  9.  
  10.         /** Called when the activity is first created. */
  11.  
  12.     @Override
  13.  
  14.     public void onCreate(Bundle savedInstanceState) {
  15.  
  16.         super.onCreate(savedInstanceState);
  17.  
  18.         setContentView(R.layout.category);
  19.  
  20.        
  21.  
  22.         setListAdapter(new ArrayAdapter<String>(this,
  23.  
  24.                                                 android.R.layout.simple_list_item_1,
  25.  
  26.                                                 FAKE_DATA));
  27.  
  28.     }
  29.  
  30. }
  31.  
  32.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


I do not get any exceptions at runtime, so I'm pretty sure the activity recognizes that there is a reference to android.R.id.list in the XML layout. However, the ListView ALWAYS comes up blank instead of having the String[] information populated. I just get a screen with the TextView on top an that's all.

When I remove the setContentView(R.layout.category) line, everything works. So what link am I missing to the custom layout that is keeping the data from showing up?

Has anyone seen this before? Is there somewhere in the manifest or project settings that I should be looking?

Thanks in advance.
wirelessdesigns
Developer
Developer
 
Posts: 25
Joined: Fri Dec 18, 2009 3:27 pm
Location: Littleton, CO

Top

Postby jwei512 » Tue Jan 05, 2010 11:24 am

Hmm.... this part seems questionable to me:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <ListView
  3.  
  4.          android:id="@android:id/list"
  5.  
  6.          android:layout_width="fill_parent"
  7.  
  8.          android:layout_height="fill_parent"
  9.  
  10.          android:drawSelectorOnTop="false"
  11.  
  12.          android:background="#00FF00"
  13.  
  14.     />
  15.  
  16.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


Try changing it to:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <ListView
  3.  
  4.          android:id="@android:id/android:list"
  5.  
  6.          android:layout_width="fill_parent"
  7.  
  8.          android:layout_height="fill_parent"
  9.  
  10.          android:drawSelectorOnTop="false"
  11.  
  12.          android:background="#00FF00"
  13.  
  14.     />
  15.  
  16.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4
- jwei

http://thinkandroid.wordpress.com
Examples, Code, and Tutorials
jwei512
Senior Developer
Senior Developer
 
Posts: 135
Joined: Mon Jul 27, 2009 9:35 pm
Location: Palo Alto, CA

Postby wirelessdesigns » Tue Jan 05, 2010 4:12 pm

Thanks for the input, but there was no change. I have tried all of the following with no difference in results

android:id="@android:id/list"
android:id="@id/android:list"
android:id="@android:id/android:list"

This is what makes me believe that it is somewhere else in the project that one wouldn't normally think to look...
wirelessdesigns
Developer
Developer
 
Posts: 25
Joined: Fri Dec 18, 2009 3:27 pm
Location: Littleton, CO

Postby achie1266 » Tue Jan 05, 2010 6:29 pm

The id that you are using for list looks fine.

That layout is normally used when you have only a listview with id as you have and your second textview with id empty, which will be displayed when there are no elements in the listview.

But since you have a textview apart from the listView [the top one] you need to take a different approach.
You can either
1. use just a listView and take off the textView. Then add it as the header for the listView.
2. make you activity extend Activity and not the ListActivity and use normal ids and not "@android:id/list" ie., like
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.                 <ListView android:id="@+id/my_list"
  2.  
  3.                 android:layout_width="fill_parent"
  4.  
  5.                 android:layout_height="wrap_content"
  6.  
  7.                 android:layout_weight="1.0" />
Parsed in 0.001 seconds, using GeSHi 1.0.8.4
And then Access the ListView after you inflate that xml[call setContentView(R.layout.category); ].
You will then be able to set the adapter on it.
3. Just use the normal listlayout and add the TextView as a childView as the first child from your java code.

But it would be more helpful if you use logcat and post your error as it says.
Because apart from all that I have said above the other reason might be that you do not have a Correct ListItem layout but that is not so in your case as you are using the default R.layout.simple_list_item_1 layout.
achie
User avatar
achie1266
Master Developer
Master Developer
 
Posts: 223
Joined: Mon Nov 09, 2009 10:56 pm
Location: Denver

Postby wirelessdesigns » Tue Jan 05, 2010 6:54 pm

Achie -

Thanks very much for the advice. In this case, I took your suggestion and removed the first TextView (which was just a header label), and set the string to the Activity title instead.

There actually was no useful logcat output, as no exceptions were being generated (hence, my confusion). "Activity start" and "Activity display" were all that showed up :wink:

I'm still not 100% sure why the following also failed:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <LinearLayout
  4.  
  5.  xmlns:android="http://schemas.android.com/apk/res/android"
  6.  
  7.  android:layout_width="fill_parent"
  8.  
  9.  android:layout_height="fill_parent"
  10.  
  11. >
  12.  
  13.      <TextView
  14.  
  15.          android:id="@+id/vehiclesLabel"
  16.  
  17.          android:layout_width="fill_parent"
  18.  
  19.          android:layout_height="wrap_content"
  20.  
  21.          android:gravity="center_horizontal"
  22.  
  23.          android:text="Manage Your Trip Categories"
  24.  
  25.     />
  26.  
  27.      <ListView
  28.  
  29.          android:id="@android:id/list"
  30.  
  31.          android:layout_width="fill_parent"
  32.  
  33.          android:layout_height="fill_parent"
  34.  
  35.          android:drawSelectorOnTop="false"
  36.  
  37.          android:background="#00FF00"
  38.  
  39.     />
  40.  
  41. </LinearLayout>
  42.  
  43.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4

since I have seen that done in numerous examples without issue. However, with changes made based on your suggestion, my list works and I still have the empty marker (which I was hoping to keep).

Thanks again! Glad to know there is an experienced Android developer local to me!
wirelessdesigns
Developer
Developer
 
Posts: 25
Joined: Fri Dec 18, 2009 3:27 pm
Location: Littleton, CO

Postby achie1266 » Tue Jan 05, 2010 8:26 pm

cool atlast someone from CO. :D
I was trying to see if there are any meetups for android devs few months back when I started developing on android but none at that time and gave up on it. I am not a very experienced developer but I guess I can get my way around things for now than I was earlier :wink:

Anyway coming to your listView I think it might be the same reason that it might not be allowing another textview with id other than empty[I am not sure on this one]

And I cannot really advice you or tell you as you said your logcat does not say anything.

your java code for the activity and the layout of your listitem may help me if you post them.
achie
User avatar
achie1266
Master Developer
Master Developer
 
Posts: 223
Joined: Mon Nov 09, 2009 10:56 pm
Location: Denver

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 7 guests