Resource id for ArrayAdapter

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

Resource id for ArrayAdapter

Postby henri51 » Wed Jun 10, 2009 4:35 pm

I need to use a ArrayAdapter class but the constructor needs the id ressource of a layout.
I construct my layout dynamically (not in a xml file) and i don't know to get the id.
Any idea ?
henri51
Junior Developer
Junior Developer
 
Posts: 11
Joined: Wed Jun 10, 2009 4:10 pm

Top

Postby tbi » Wed Jun 10, 2009 5:58 pm

Hey,

The answer to your question should be using these simple getter/setter methods:

http://developer.android.com/reference/ ... tml#getId()
http://developer.android.com/reference/ ... #setId(int)

(You'll need to add the part in brackets manually, apparently the forum doesn't handle brackets in URIs to well)

I'd strongly advise the use of xml files though, unless there really is no other way of handling whatever it is you're doing.

Greets,
/TBI
A la fin, il y aura seulement de la beauté.
tbi
Junior Developer
Junior Developer
 
Posts: 12
Joined: Fri May 29, 2009 8:22 am
Location: Marburg, Germany

Postby henri51 » Wed Jun 10, 2009 6:10 pm

I tried to use the setID and getID methods but it doesn't work, I 've a ResourceNotFoundException
henri51
Junior Developer
Junior Developer
 
Posts: 11
Joined: Wed Jun 10, 2009 4:10 pm

Postby tbi » Wed Jun 10, 2009 6:25 pm

To be honest, I always worked with xml layouts when it came to Android apps, so I never used these methods myself. And, seeing as I don't have any code snippet of yours, I can only guess the problem.
Android doesn't assign ids automatically. If you add a View directly in Java (or in XML without specifying the android:id attribute), that View has no id, and getId() returns View.NO_ID (which has a value of -1, if you want to check). That means, before you use getId() for the first time, you have to set an id (my guess is that is has to be positive (i.e. >= 0) to be a valid id, but like I said, I'm not too sure).
If that information didn't help, be so kind as to post the relevant piece of code.

Greets,
/TBI
A la fin, il y aura seulement de la beauté.
tbi
Junior Developer
Junior Developer
 
Posts: 12
Joined: Fri May 29, 2009 8:22 am
Location: Marburg, Germany

Postby henri51 » Wed Jun 10, 2009 6:30 pm

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         LinearLayout ll = new LinearLayout(this);
  3.  
  4.         ll.setOrientation(LinearLayout.VERTICAL);
  5.  
  6.         ListView lv = new ListView(this);
  7.  
  8.         ll.addView(lv, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
  9.  
  10.         LinearLayout ll2 = new LinearLayout(this);
  11.  
  12.         ll2.setOrientation(LinearLayout.VERTICAL);
  13.  
  14.         TextView tv = new TextView(this);
  15.  
  16.         ll2.addView(tv, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
  17.  
  18.         ArrayList<String> al = new ArrayList<String>();
  19.  
  20.         al.add("value1");
  21.  
  22.         al.add("value2");
  23.  
  24.         al.add("value3");
  25.  
  26.         ll2.setId(25);
  27.  
  28.         ArrayAdapter<String> aa  = new ArrayAdapter<String>(this,ll2.getId(),al);
  29.  
  30.         lv.setAdapter(aa);
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
henri51
Junior Developer
Junior Developer
 
Posts: 11
Joined: Wed Jun 10, 2009 4:10 pm

Postby tbi » Wed Jun 10, 2009 6:42 pm

If you look at the constructor summary at http://developer.android.com/reference/android/widget/ArrayAdapter.html, you'll notice that the constructor asks for a TextView to put the values in. Additionally, a layout resource can be specified if you defined the layout containing the TextView in a seperate XML file. But it always asks for "int textViewResourceId." So, all you have to do is assign the id to the TextView instead of the LinearLayout:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         tv.setId(25);
  2.         ArrayAdapter<String> aa  = new ArrayAdapter<String>(this,tv.getId(),al);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


If that doesn't help (I'm doing this by heart at the moment, so don't blame me), try defining an id for your Layout and the TextView and use one of the constructors which take 2 id params:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         ll2.setId(25);
  2.         tv.setId(26);
  3.         ArrayAdapter<String> aa  = new ArrayAdapter<String>(this, ll2.getId(), tv.getId(), al);
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Hopefully, that should do it.

Greets,
/TBI
A la fin, il y aura seulement de la beauté.
tbi
Junior Developer
Junior Developer
 
Posts: 12
Joined: Fri May 29, 2009 8:22 am
Location: Marburg, Germany

Top

Postby henri51 » Thu Jun 11, 2009 11:13 am

:cry: I tried with yours two solutions, but same result :

06-11 10:11:51.889: ERROR/AndroidRuntime(797): Uncaught handler: thread main exiting due to uncaught exception
henri51
Junior Developer
Junior Developer
 
Posts: 11
Joined: Wed Jun 10, 2009 4:10 pm

Postby tbi » Thu Jun 11, 2009 2:28 pm

That doesn't really say anything about what exactly went wrong. Like I said, I'd advise using an XML layout file--I don't see any reason not to do so in your code snippet.
The only thing I can think of (and that's quite a long shot) is that maybe your ids are invalid. I don't know if there are any further restrictions to id values in Android, but the ids in my xml files are quite big (p.e. "2131165219 [0x7f070023]" or "2130903047 [0x7f030007]").
Sorry I can't help further.

Greets,
/TBI
A la fin, il y aura seulement de la beauté.
tbi
Junior Developer
Junior Developer
 
Posts: 12
Joined: Fri May 29, 2009 8:22 am
Location: Marburg, Germany

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 8 guests