Is There A Way To Use an ArrayAdapter With 2 or More Arrays?

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

Is There A Way To Use an ArrayAdapter With 2 or More Arrays?

Postby illiniwatcher » Thu Jan 01, 2009 5:07 am

To all:

I see that Android has facilities for using a SimpleCursorAdapter for data structures involving more than one column from a database table. But what if you want to "bind" an ArrayAdapter object to two, three or more parallel arrays? Is it possible to do that? Or is it even suggested?

Here's what I have defined for an ArrayAdapter:

ArrayAdapter<String> adpEntries =
new ArrayAdapter<String>(this,
R.layout.twoline_entry,
R.id.txtEntry,
debits);

In my layouts for "twoline_entry" I have the following:

<?xml version="1.0" encoding="UTF-8"?>

<LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical">

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0.00" android:textColor="@color/white" android:textStyle="bold" android:textSize="18px" android:id="@+id/txtEntry"></TextView>

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/txtDetails" android:text="Details" android:textColor="@color/white"></TextView>

</LinearLayout>


There are two TextView objects in this layout, txtEntry and txtDetails. This line of code:


setListAdapter(adpEntries);


attaches the ListAdapter to the ArrayAdapter called adpEntries, but only fills in the first output field, txtEntry. I have a second array called debitdetails that I don't know how to attach. And it seems that setListAdapter can only accept one ArrayAdapter, meaning only one array can be used at a time.

Does anyone have an idea of how to use multiple, parallel arrays with one or more ArrayAdapters? It would be helpful not to have to use a database for everything.

Thanks,
Charles
illiniwatcher
Junior Developer
Junior Developer
 
Posts: 15
Joined: Mon Nov 03, 2008 8:09 am

Top

Postby MrSnowflake » Fri Jan 02, 2009 2:04 pm

You only can do this by, either, merging the arrays into one, or making your own arrayadapter.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Multiple Arrays Can Be Done With Lists

Postby illiniwatcher » Mon Jan 05, 2009 4:15 pm

MrSnowflake wrote:You only can do this by, either, merging the arrays into one, or making your own arrayadapter.


I actually did find a way to do it, after some curious experimentation. It turns out that merging of arrays is not necessary, nor defining your own special ArrayAdapter. See the example below.

It seems the key to it is to use the "put" method of a HashMap object more than two times, and to specify additional key values (as strings) in a SimpleAdapter (rather than an ArrayAdapter). This was originally what I'd wanted when I posed my original question, and this approach works beautifully.

The Java looks looks like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package my.twoarray;
  3.  
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6.  
  7. import android.app.Activity;
  8. import android.app.ListActivity;
  9. import android.os.Bundle;
  10. import android.widget.ArrayAdapter;
  11. import android.widget.SimpleAdapter;
  12. import android.widget.Toast;
  13.  
  14. public class TwoArray extends ListActivity {
  15.  
  16.         //**********************************************************************
  17.         // NOTE: You CAN make this work for three or more arrays, but I'm not
  18.         // sure if it's because of a bug or oversight in the definition of
  19.         // HashMaps or not.
  20.         //
  21.         // Typically HashMaps and Maps only let you use a single
  22.         // key and value combo (you can't do <K,V,V> or <K,V,V,V> or the like).
  23.         // The ArrayList is a one-dimensional deal.
  24.         //
  25.         // HOWEVER: Oddly, I was able to use the "put" method of HashMap to
  26.         // superload it with a third hash key that the list amazingly recognized.
  27.         // So it can work, but with some care.
  28.         //**********************************************************************
  29.        
  30.        
  31.         // Declare what connects the first two parallel arrays
  32.         // (Note: for the third, fourth, etc. arrays, don't worry about this;
  33.         // see below.)
  34.         ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
  35.  
  36.         // Define a simple adapter
  37.         SimpleAdapter adapEntries;
  38.        
  39.         // Define your arrays (minimum of 2, but you can use 3 or more)
  40.         private String[] cities;
  41.         private String[] states;
  42.         private String[] areacodes;
  43.        
  44.        
  45.     @Override
  46.     public void onCreate(Bundle savedInstanceState) {
  47.        
  48.         Integer n;
  49.        
  50.         super.onCreate(savedInstanceState);
  51.         setContentView(R.layout.main);
  52.        
  53.         // Get the arrays locally
  54.         cities = this.getResources().getStringArray(R.array.cities);
  55.         states = this.getResources().getStringArray(R.array.states);
  56.         areacodes = this.getResources().getStringArray(R.array.areacodes);
  57.        
  58.         // Add them to the HashMap
  59.         for (n = 0; n < cities.length; n++) {
  60.                 AddEntry(cities[n], states[n], areacodes[n]);
  61.         }
  62.        
  63.         RefreshList();
  64.        
  65.     }
  66.  
  67.    
  68.     public void AddEntry(String cityarg, String statearg, String areacodearg) {
  69.    
  70.         // Add a local array entry to the ArrayAdapter using a
  71.         // new, ad hoc HashMap.  Note that the keys are not equal
  72.         // to the control names in the XML layout.
  73.        
  74.         // WATCH THIS CODE in the future; this may be a bug or it may not
  75.         // be.  If using "put" more than twice causes weird activity,
  76.         // and Google corrects it, this may stop working for the third and
  77.         // later arguments.
  78.        
  79.                 HashMap<String,String> newitem = new HashMap<String,String>();
  80.                
  81.                 newitem.put( "city", cityarg );
  82.                 newitem.put( "state", statearg );
  83.                 newitem.put( "areacode", areacodearg );
  84.                
  85.                 list.add( newitem );            // "list" is defined globally; see above
  86.        
  87.     }
  88.    
  89.    
  90.     public void RefreshList() {
  91.        
  92.         // NOTE: the key names below MUST match the names in the "put"
  93.         // statement in the routine above, element for element (if there
  94.         // are three key names above, there must be three key names below).
  95.        
  96.         SimpleAdapter adapEntries = new SimpleAdapter(
  97.                         this,
  98.                         list,
  99.                         R.layout.threeline,
  100.                         new String[] { "city", "state", "areacode" },
  101.                         new int[] { R.id.txtCity, R.id.txtState, R.id.txtAreaCode }
  102.                         );
  103.  
  104.                 setListAdapter(adapEntries);
  105.                
  106.     }
  107.    
  108.    
  109. }
  110.  
Parsed in 0.055 seconds, using GeSHi 1.0.8.4



And the R.layout.threeline XML with three views for the output values looks like this:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="UTF-8"?>
  3.  
  4. <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical">
  5.  
  6. <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="City" android:textSize="20px" android:id="@+id/txtCity"></TextView>
  7.  
  8. <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="State" android:id="@+id/txtState"></TextView>
  9.  
  10. <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/txtAreaCode" android:text="Area Code"></TextView>
  11.  
  12. </LinearLayout>
  13.  
  14.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4



Finally, a member arrays.xml containing all the values might look something like this:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <resources>
  4.     <string-array name="cities">
  5.         <item>Chicago</item>
  6.         <item>New York</item>
  7.         <item>Austin</item>
  8.         <item>Dallas</item>
  9.         <item>Houston</item>
  10.         <item>Atlanta</item>
  11.         <item>Miami</item>
  12.     </string-array>
  13.     <string-array name="states">
  14.         <item>Illinois</item>
  15.         <item>New York</item>
  16.         <item>Texas</item>
  17.         <item>Texas</item>
  18.         <item>Texas</item>
  19.         <item>Georgia</item>
  20.         <item>Florida</item>
  21.     </string-array>
  22.     <string-array name="areacodes">
  23.         <item>312</item>
  24.         <item>212</item>
  25.         <item>512</item>
  26.         <item>214</item>
  27.         <item>713</item>
  28.         <item>404</item>
  29.         <item>303</item>
  30.     </string-array>
  31. </resources>    
  32.  
  33.  
Parsed in 0.006 seconds, using GeSHi 1.0.8.4



...although you could also read the values into the arrays, using your own code, from an external file, if you didn't want hard-coded values in the arrays.xml file.
illiniwatcher
Junior Developer
Junior Developer
 
Posts: 15
Joined: Mon Nov 03, 2008 8:09 am

Postby Steph » Wed Nov 04, 2009 9:26 am

Hi!
I try the same as you but I have an exception when i try to get resources :String array resource id #0x7F0400000

Do you know why?
Thanks

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.           String[] myShowName = this.getResources().getStringArray(R.raw.seriesname);
  3.          
  4.           Log.i("US Finder", "serie "+myShowName[0]);
  5.          
  6.           //for each element in xml create button
  7.           for (int i=0; i<myShowName.length; i++)
  8.           {
  9.                Log.i("US Finder", "serie "+myShowName[i]);
  10.                _btn[i]= new Button(this);
  11.                _btn[i].setText(myShowName[i]);
  12.                _btn[i].setGravity(Gravity.CENTER_HORIZONTAL);
  13.                _btn[i].setOnClickListener(this);
  14.                myLayout.addView(_btn[i]);
  15.           }
  16.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <resources>
  3.     <string-array name="showname">
  4.         <item>HowIMetYourMother</item>
  5.         <item>SLT</item>
  6.     </string-array>
  7. </resources>    
  8.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
Steph
Experienced Developer
Experienced Developer
 
Posts: 90
Joined: Mon Oct 20, 2008 1:48 pm
Location: France

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 2 guests