Creating a 2-column list or table on the fly

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

Creating a 2-column list or table on the fly

Postby iaindownie » Mon Jan 18, 2010 1:07 pm

Hi,
I have an application that does some calculations based on input to a form, which takes up about 1/2 a screen. I want to populate a ListView or some sort of table in the bottom half of the screen depending on the choices in the top half.

At the moment I'm getting static data output when I hit submit using the following:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public void setSplits() {
  2.  
  3.                 lv = (ListView) findViewById(R.id.ListView01);
  4.  
  5.                 ListAdapter birds = (ListAdapter) ArrayAdapter.createFromResource(this,
  6.  
  7.                                 R.array.species, android.R.layout.simple_list_item_1);
  8.  
  9.                 lv.setAdapter(birds);
  10.  
  11.                 lv.setTextFilterEnabled(true);/**/
  12.  
  13.         }
  14.  
  15.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.                 <ListView android:id="@+id/ListView01"
  2.  
  3.                 android:layout_width="wrap_content"
  4.  
  5.                 android:layout_height="wrap_content"
  6.  
  7.                 android:layout_below="@+id/EditText03a" />
  8.  
  9.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


This creates a nice, scrollable ListView in the bottom half of the screen which wasn't there when app loaded, but it's from a resource (xml array). I need it to take values I feed the ListView from an ArrayList after calculations from the form, or some other similar Java container which is empty to begin with.

1. Any sample code of populating a ListView NOT from "createFromResource"
2. Anyone recommend a better View for this rather than a ListView? I want it to be two equal columns, each column left justified with text only, and scrollable.

Thanks in advance
Iain
User avatar
iaindownie
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Mon Dec 29, 2008 4:08 pm
Location: Thetford, Norfolk, UK

Top

Postby iaindownie » Tue Jan 19, 2010 5:06 pm

Update on my miserable attempts to fix this:

I tried to get the ListView showing a String Array using

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public void setSplits() {
  2.  
  3.                 lv = (ListView) findViewById(R.id.ListView01);
  4.  
  5.                 String[] mStrings = new String[]{"Android", "Google", "Eclipse"};
  6.  
  7.                 ArrayAdapter a = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings);
  8.  
  9.                 lv.setAdapter((ListAdapter)a);
  10.  
  11.                 lv.setTextFilterEnabled(true);
  12.  
  13.         }
  14.  
  15.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


instead of getting the data from an XML like (this works):

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public void setSplits() {
  2.  
  3.                 lv = (ListView) findViewById(R.id.ListView01);
  4.  
  5.                 ListAdapter birds = (ListAdapter) ArrayAdapter.createFromResource(this,
  6.  
  7.                                 R.array.species, android.R.layout.simple_list_item_1);
  8.  
  9.                 lv.setAdapter(birds);
  10.  
  11.                 lv.setTextFilterEnabled(true);
  12.  
  13.         }
  14.  
  15.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


but I get the following error:

01-19 15:55:38.811: ERROR/AndroidRuntime(811): java.lang.RuntimeException: Unable to start activity ComponentInfo{bto.android/bto.android.PaceCalc}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

Can anyone help me work out what I'm doing wrong? Do I have to have the lv object created originally in onCreate method? This isn't needed for the second, resource populated method.

Really struggling, hope someone can help.
Iain
User avatar
iaindownie
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Mon Dec 29, 2008 4:08 pm
Location: Thetford, Norfolk, UK

Postby iaindownie » Tue Jan 19, 2010 5:39 pm

OK, cracked populating a ListView on the fly from an ArrayList rather than an XML file.

Will continue to think on a better, scrollable view than basic ListView.

Cheers
Iain
User avatar
iaindownie
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Mon Dec 29, 2008 4:08 pm
Location: Thetford, Norfolk, UK

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Majestic-12 [Bot] and 4 guests