ListView Questions

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

ListView Questions

Postby cjules86 » Thu Apr 30, 2009 7:17 am

Hello all,

I was wondering if anyone could help me out. I'm developing an app that gets stock quotes. Now I want to have a layout that shows your owned stocks (in a listview that they can click through to see more details). I also want another layout to have a different listview for the user's "watched" stocks.

I saw many examples where the ListView is on the main layout but I cannot figure out how to get it to work when I first change the layout and then attempt to fill the list.

Also, the examples I was going by instructed me to make the id of the listview "android:id="@android:id/list"". Does this mean that you can only have one listview?

Can anyone point me in the right direction for creating this?

Thanks.
cjules86
Freshman
Freshman
 
Posts: 5
Joined: Wed Apr 15, 2009 8:42 pm

Top

Postby mangaluve » Thu Apr 30, 2009 9:01 am

Are you using a ListActivity? It already contains a Layout and a ListView, so if you're setting your own Layout, I think the ListView in it must have a special name (at least if you're using the ListActivity.setAdapter method). But it's really easy to create an Activity with two lists. You can do something like this:
main.xml:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.         android:orientation="vertical" android:layout_width="fill_parent"
  6.  
  7.         android:layout_height="fill_parent">
  8.  
  9.         <ListView android:id="@+id/mylist1" android:layout_width="fill_parent"
  10.  
  11.                 android:layout_height="fill_parent" android:layout_weight="2" />
  12.  
  13.         <ListView android:id="@+id/mylist2" android:layout_width="fill_parent"
  14.  
  15.                 android:layout_height="fill_parent" android:layout_weight="2" />
  16.  
  17. </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


MyListActivity.java:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.app.Activity;
  2.  
  3. import android.os.Bundle;
  4.  
  5. import android.widget.ArrayAdapter;
  6.  
  7. import android.widget.ListView;
  8.  
  9.  
  10.  
  11. public class MyListActivity extends Activity {
  12.  
  13.        
  14.  
  15.     /** Called when the activity is first created. */
  16.  
  17.     @Override
  18.  
  19.     public void onCreate(Bundle savedInstanceState) {
  20.  
  21.         super.onCreate(savedInstanceState);
  22.  
  23.         setContentView(R.layout.main);
  24.  
  25.        
  26.  
  27.         ListView list1 = (ListView) findViewById(R.id.mylist1);
  28.  
  29.         list1.setAdapter(new ArrayAdapter<String>(this,
  30.  
  31.                 android.R.layout.simple_list_item_1, LIST_CONTENT1));
  32.  
  33.        
  34.  
  35.         ListView list2 = (ListView) findViewById(R.id.mylist2);
  36.  
  37.         list2.setAdapter(new ArrayAdapter<String>(this,
  38.  
  39.                 android.R.layout.simple_list_item_1, LIST_CONTENT2));
  40.  
  41.     }
  42.  
  43.  
  44.  
  45.     static final String[] LIST_CONTENT1 = new String[] {"aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh", "ii", "jj"
  46.  
  47.          };
  48.  
  49.  
  50.  
  51.     static final String[] LIST_CONTENT2 = new String[] {"AA", "BB", "CC", "DD", "EE", "FF", "GG", "HH", "II", "JJ"
  52.  
  53.          };
  54.  
  55.  
  56.  
  57. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


The ListActivity is nothing magic, it's just an Activity that has a default-layout with a list, and some convinience methods for setting the Adapter for that list. So if you want to create a own layout and use in a ListActivity (perhaps put the ListView in some LinearLayouts with some Buttons and stuff), you have to give at least one of your ListViews that special id @android:id/list.
mangaluve
Experienced Developer
Experienced Developer
 
Posts: 82
Joined: Mon Mar 23, 2009 8:59 pm

Postby cjules86 » Thu Apr 30, 2009 5:29 pm

Oh, wow... I feel silly now. Thanks for clearing that up. I got it working.
cjules86
Freshman
Freshman
 
Posts: 5
Joined: Wed Apr 15, 2009 8:42 pm

Postby mangaluve » Thu Apr 30, 2009 10:11 pm

To save some time in the future (perhaps), the same reasoning goes for tabs and the TabActivity.
mangaluve
Experienced Developer
Experienced Developer
 
Posts: 82
Joined: Mon Mar 23, 2009 8:59 pm

Postby kali » Sat May 02, 2009 1:19 pm

hai friend
I have used listview there are four items in it, when i click under that blank list i get list deactivated
what to do to prevent it?
kali
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Tue Jan 27, 2009 1:31 pm

Postby cjules86 » Sat May 02, 2009 1:29 pm

mangaluve wrote:To save some time in the future (perhaps), the same reasoning goes for tabs and the TabActivity.

That did help, thanks!

But, back to ListViews...

I am experimenting with custom layouts and have a few questions that I was not able to answer myself from the documentation.

Suppose I have an XML setup defined as:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
  2.    android:layout_width="fill_parent"
  3.    android:layout_height="wrap_content"
  4.    android:orientation="horizontal">
  5.    
  6.     <TextView
  7.        android:id="@+id/list_symbol"
  8.        android:layout_width="wrap_content"
  9.        android:layout_height="wrap_content"
  10.        android:textSize="15sp"
  11.                 android:textStyle="bold"
  12.                 android:layout_marginLeft="5px"/>
  13.                
  14.         <TextView
  15.        android:id="@+id/list_price"
  16.        android:layout_width="wrap_content"
  17.        android:layout_height="wrap_content"
  18.        android:textSize="15sp"
  19.                 android:textStyle="bold"
  20.                 android:layout_marginLeft="5px"/>
  21. </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4

First, if I want to set the different fields of layout (i.e. list_symbol/list_price), how do i fill in the fields for each list item? I understand how to fill one of the fields for each list item:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. pl.setAdapter(new ArrayAdapter<String>(this,R.layout.listrow,R.id.list_symbol,symbols));
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

But I am unsure how I would set both list_symbol and list_price textviews.

Next, When I set up the OnItemClickListener for the ListView such as:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. pl.setOnItemClickListener(new
  2.                 AdapterView.OnItemClickListener()
  3.                 {
  4.                         public void onItemClick(AdapterView<?> parent, View v, int position,long id)
  5.                         {
  6.                                 String symbol = "";
  7.                                 String s = (String)(((TextView)v).getText());
  8.                                
  9.                                 //parse the string to get the stock symbol
  10.                                 int x = s.indexOf('n');
  11.                                 if(x != -1)
  12.                                 {
  13.                                         symbol = s.substring(0,x);
  14.                                 }
  15.                         }
  16.                 });
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

How do I specify different fields of the predefined xml layout?

PS - The above OnItemClickListener works if I use the android:R.id.simple_list_item_1 layout but fails if I use a custom layout. I assume this is because the android:R.id.simple_list_item_1 is simply just a file with <TextView></TextView> whereas my layout is wrapped in a <LinearLayout>
cjules86
Freshman
Freshman
 
Posts: 5
Joined: Wed Apr 15, 2009 8:42 pm

Top

Postby mangaluve » Sat May 02, 2009 2:31 pm

Let's try some pedagogy :)

The ArrayAdapter constructor has two arguments of interest when it comes to the layout, namely the ResourceId(R.layout.listrow in your case) and the textResourceId (R.id.list_symbol in your case).

What the ArrayAdapter does is the following: If no textResourceId is specified in the constructor (i.e. if your remove the R.id.list_symbol argument), it assumes that the entire Resource View (R.layout.listrow in your case) Is a TextView. It will then use the setText-method on that object. However, if you do specify the resource, it will look in the Resource for a textview with the id you specified and use that. You can see it pretty easily in

http://android.git.kernel.org/?p=platfo ... va;hb=HEAD

in the createViewFromResource-method (always good to look at the source). In the code, "resource" is the resource your specified, and mFieldId is the textResourceId. As you can see, if there is no textResourceId, it will assume that the entire layout is just a TextView. On the other hand, if you have provided it with a text-id, it will look in the layout for a TextView with the Id you specified (mFieldId).

That being said, the ArrayAdapter assumes that Either you provide it with just a listview, or a View containing a TextView with the textResourceId you supplied. Thus, it does not support more than one textfield. Now there are some other adapters, for instance the SimpleAdapter which gives you more control. There's a good example at
http://mylifewithandroid.blogspot.com/2 ... apter.html
where the creator makes a list where each entry consists of two TextViews. Another option is to write your own Adapter and make your own getView-method.

As for you OnClick-question, try to get the different TextViews by using
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. TextView symbol = (TextView)v.findViewById(R.id.list_symbol)
  3.  
  4. TextView price= (TextView)v.findViewById(R.id.list_price)
  5.  
  6.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

That is, you look in the View called v (which is the entire list-item, a R.layout.listrow in your case), for the different sub-views.


Hope it all made sense :)
mangaluve
Experienced Developer
Experienced Developer
 
Posts: 82
Joined: Mon Mar 23, 2009 8:59 pm

Postby cjules86 » Sat May 02, 2009 11:59 pm

Yes, it did indeed. Very helpful!

I ended up using the SimpleAdapter and it worked great. It was a little confusing at first to get the list set up for the SimpleAdapter but once I figured it out it was easy peasy. :)

Thanks for your help mangaluve!
cjules86
Freshman
Freshman
 
Posts: 5
Joined: Wed Apr 15, 2009 8:42 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 6 guests