1

Good Morning,

I seem to be missing one piece of information (still learning on my first Android app) connecting an ArrayAdapter to a view. I create a Class we'll call MyInfoClass which is just some properties for holding data. I populate the class with rows of data and in the end I have an ArrayList(MyInfoClass>. So far so good, everything looks great, and values are as I expect them in the Debug perspective.

I also have an XML layout that has some TextViews on it. I want to use this layout for displaying the different "sets of data" held in ArrayList(MyInfoClass>. I understand that to do this I would use an ArrayAdapter and I do so with the following line:

ArrayAdapter<MyInfoClass> X = new ArrayAdapter<MyInfoClass>(this, R.layout.main, R.id.txtvw_PrevLift, ld_data);

(ld_data is the ArrayList(MyInfoClass> object)

My confusion comes from...well from not knowing what I'm doing, but anyway it comes from not understanding how to connect the properties of MyInfoClass to their corresponding TextView. The ArrayAdapter documentation doesn't show a constructor that takes a List of TextView id's????

Thank You for your help. JB

To help clear up my confusion I don't bind to a list....maybe I should? I was just wanting to have an entire view represent one row of data. I've trimmed alot of code for readability but here is my view. Some of the text views are static labels and other would get populated. The buttons are for navigation back and forth.

<RelativeLayout....>

    <ImageView
        android:id=.../>

    <TextView
        android:id=.../>

    <ImageView
        android:id=.../>


    <TextView
        android:id=.../>

    <TextView
        android:id=.../>

    <TextView
        android:id=.../>

    <Button
        android:id=.../>

    <Button
        android:id=.../>

</RelativeLayout>
GPGVM
  • 5,515
  • 10
  • 56
  • 97

2 Answers2

3

In your case best to use SimpleAdapter

You define an XML file which represents your list item. This XML file will contain TextViews in which you will display your data from your MyInfoClass. Say it's my_list_item.xml

<LinearLayout...>
<TextView android:id=name.../>
<TextView android:id=gender.../>
<TextView android:id=occupation.../>
</LinearLayout>

Suppose you have the data for them as MyInfoClass.getName(), .getGender(), .getOccupation()

You create a List of Maps each Map represents the data for one list item:

List<HashMap<String, String>> maps = new ArrayList<HashMap<String, String>>();

You fill this list with your information:

for(int i=0; i<myArray.size(); ++i) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("name", myArray.get(i).getName()); 
        map.put("gender", myArray.get(i).getGender()); 
        map.put("occupation", myArray.get(i).getOcupation()); 
        maps.add(map); // add this entry to the list
    );

You also create 2 arrays : one with the names of the keys( "name", "occupation", "gender") another one with the ids of corresponding resources. They are String[] from, int[] to, so the data in the map with a key from[n] gets displayed in a view with id to[n]

String[] from = new String[] {"name", "gender", "occupation" };
int[] to = {R.id.name, R.id.gender, R.id.occupation }

Then you create a SimpleAdapter using that list and you XML file:

SimpleAdapter mAdapter = new SimpleAdapter(YourActivity.this, maps, R.layout.my_list_item, from, to);

then you set this adapter in your ListActivity and the list will be displayed:

setListAdapter(mAdapter);

If you want to display something other than Strings you will need to subclass SimpleAdapter and redefine some functions like setViewText, setViewImage but I don't know much about that

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
  • Ironically I followed the more complex part about creating a hash map and passing it. Where I stumbled was with the "property methods" Do I have to create a get / set accessor method? I have in my class: public String AcctName; In c# the compiler would have turned the shorthand into fully accessible properties with read / write capabilities? – GPGVM May 14 '12 at 16:59
  • OK so I declare my Data class and I do have the accessor methods so my problem is navigating the ArrayList to the specific item in the collection. – GPGVM May 14 '12 at 17:03
  • No, you don't need getters/setters necessarily. You can do map.add("account", myObject.acctName) or you can do map.add("account", "my bank account") – Alexander Kulyakhtin May 14 '12 at 17:04
  • Interesting. So I use MODE_WORLD_READABLE with get and then my individual properties show. I'm going to need to read up a bit more. – GPGVM May 14 '12 at 17:05
  • You have ArrayList of your objects, you traverse the array list and add fields from every object to the map. One map per one object as many entries in that map as there are object fields you want to display. – Alexander Kulyakhtin May 14 '12 at 17:06
  • MODE_WORLD_READABLE? You must be making fun of me, sir. I advise you do that simpler, forget about your getters/setters just put some strings like "1" "2" "3" in your maps values for a while then you see them in the list you feel then more comfortable – Alexander Kulyakhtin May 14 '12 at 17:07
  • NO!!! I am not making fun of you!!! I was experimenting with the get() off the ArrayList and it was something that Intellisense (or whatever they call is in Eclipse) suggested. I tried it to see what would happen. Please understand this is all new to me so if there was some humor to that it was completely unintentional. – GPGVM May 14 '12 at 18:15
1

I think you are looking for a Simple List Adapter.

http://developer.android.com/reference/android/widget/SimpleAdapter.html

It allows you to specify which fields to get the data from (from[] in the constructor) and where that data will be placed (to[] in the constructor)

I don't know if a Simple List Adapter works with data objects, but I know it works with Hashmaps. I use a custom adapter that extends the BaseAdapter type when I work with data object lists.

See this tutorial for how to use a simple adapter with your listview: http://eureka.ykyuen.info/2010/01/03/android-simple-listview-using-simpleadapter/

Edit: if you prefer to stick with data objects, here is another QA that shows an easy implementation of a custom SimpleAdapter using data objects How to use ArrayAdapter<myClass>

Edit #2:: Sorry, I thought you were binding to a list. Someone else had this problem recently and I suggested to use the basic infrastructure of a listview, but modify it so there is only one item that spans the entire size of the listview. The item would essentially be the layout with all of your textviews. In that example, you'll need to keep track of which item to display (which data object to get the information from) and use that. You can look at my suggested solution here: Android - wizard type activity with adapter backed data

Community
  • 1
  • 1
Gophermofur
  • 2,101
  • 1
  • 14
  • 14
  • But is it a Simple list adapter if I don't actually have a list? My xml view is a "page" of information about XYZ with a previous and next button. When the person hits next I want them to see the next "page" of information. The layout is all the same just the info / numbers change as they move back and forth through the dataset. So in a way I don't have a list to bind and yet I do have a "list" of pages. In essence my whole "page" represents one row....? – GPGVM May 14 '12 at 16:38
  • Ah sorry, I thought you were just binding to a listview. I've edited my answer with a solution I'm pretty sure would work, however, it's probably stretching the concept of a listview to its limits :) – Gophermofur May 14 '12 at 18:15
  • Your edit makes sense except I am running into compile error. Eclipse doesn't like anything "inside" the ListView. . Would it be better for me to just handle the button clicks and repopulate the textviews then redraw? – GPGVM May 14 '12 at 20:12
  • Read the tutorial on how to use a listview with an adapter. There is no such thing as . Your layout with all the textview's is going to be the layout for the item inside your listview (we'll call it, lv_item.xml). When you create your listview adapter, you need to specify what layout *each* row/item will use to display the information for that row; that's where you would enter *lv_item.xml*. If you are using a custom adapter, all you need to do is inflate the layout (lv_item.xml) in the GetView method and populate it with the correct data. – Gophermofur May 15 '12 at 13:16
  • Oh yeah I understand now. You'd probably get a good laugh at all the dumb things I've thought / tried while learning to dev for Android. – GPGVM May 15 '12 at 21:11
  • Not unexpected. I've had a lot of head scratching moments myself in the past 4 months while learning :). It's not a bad thing to struggle a bit, it usually ends up with people trying lots of different things and *really* learning how the android framework works. If my answer was the one you were looking for, please mark it as answered. If not, continue onwards :) – Gophermofur May 16 '12 at 13:23