Using ListAdapter to store more than one field per list item

Put your problem here if it does not fit any of the other categories.

Using ListAdapter to store more than one field per list item

Postby kidalex » Fri Jun 25, 2010 12:34 am

It seems that the List Adapter only stores an array and lets you access one item per line. But what if my ListView has 3 or more items that are obtained separately from the DB and are 3 different fields. How do I store them into the List Adapter and then access them?
kidalex
Developer
Developer
 
Posts: 42
Joined: Tue Jun 22, 2010 10:03 am

Top

Re: Using ListAdapter to store more than one field per list

Postby anarche » Fri Jun 25, 2010 5:32 am

Use a CursorAdapter instead of an ArrayAdapter, then map each datatype in the Cursor to a View within the ListView's row
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Re: Using ListAdapter to store more than one field per list

Postby achie1266 » Fri Jun 25, 2010 5:55 am

The best option would be to overwrite the ArrayAdapter or even better BaseAdapter.
Look at the source code to see the methods in those and it will be more easier on you.

Here is an example which extends the BaseAdapter.
http://developer.android.com/resources/ ... List8.html.
You will also fing more custom listviews in the developer site which will help you better or are more useful for your requirements.
achie
User avatar
achie1266
Master Developer
Master Developer
 
Posts: 223
Joined: Mon Nov 09, 2009 10:56 pm
Location: Denver

Re: Using ListAdapter to store more than one field per list

Postby kidalex » Fri Jun 25, 2010 10:00 am

Since I"m getting my data from the DB I'd like to use the CursorAdaptor however in this case I won't be able to use the ConvertView in order to cache my views with a wrapper. How can I implement both?
kidalex
Developer
Developer
 
Posts: 42
Joined: Tue Jun 22, 2010 10:03 am

Re: Using ListAdapter to store more than one field per list

Postby padde » Fri Jun 25, 2010 10:37 am

What makes you think you cant use "ConvertView" with the CursorAdapter?!
SimpleCursorAdapter does that innately. All you need is SimpleCursorAdapter (maybe with
a custom ViewBinder) or your own CursorAdapter derived from SimpleCursorAdapter.
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Re: Using ListAdapter to store more than one field per list

Postby Benzebuth » Fri Jun 25, 2010 4:31 pm

on my part, i had kind the same problem.
SO i decided not to use a listadapter, but my own adapter based on a simple view.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. //this is the parent of all, containing a table layout inside a relativelayout
  2. RelativeLayout table = (RelativeLayout)getLayoutInflater().inflate(R.layout.mybigtab, null);
  3.  
  4. for( Object arow : anArray) {
  5. //myown will build my view,"upon" a layout. see the class MyOwnAdapter
  6. MyOwnAdapter myown = new MyOwnAdapter(tabHost.getContext(), arow);
  7.  
  8. //then i insert it into my tablelayout, as a row
  9. //note that getView actually builds the view : put images, text etc.. where it as to
  10. ((TableLayout)table.findViewById(R.id.thetabletodisplay)).addView(myown.getView());
  11.  
  12. }
  13.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


and my adapter is as follow
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class MyOwnAdapter extends View {
  2. public MyOwnAdapter(Context context) {
  3.                 super(context);
  4.         }
  5. //to get the view
  6. public View getView() {
  7.  //inflate it
  8.         LayoutInflater li = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  9.         view = li.inflate(R.layout.annonce, null);
  10. //do some stuff to build the view...
  11. //... and then
  12. return view;
  13. }
  14.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4



and that's it , i've got a beautifull list, which elements i can access individually (to modify etc)...
don't forgot to keep some kind of index for each of your row element

i hope this helps, this is my very first post on this forum, and i wish to help you as much as this forum helpd (and still do !!) me
Image
User avatar
Benzebuth
Freshman
Freshman
 
Posts: 6
Joined: Fri May 28, 2010 11:50 am

Top

Re: Using ListAdapter to store more than one field per list

Postby kidalex » Fri Jun 25, 2010 8:34 pm

padde wrote:What makes you think you cant use "ConvertView" with the CursorAdapter?!
SimpleCursorAdapter does that innately. All you need is SimpleCursorAdapter (maybe with
a custom ViewBinder) or your own CursorAdapter derived from SimpleCursorAdapter.


I know I can't use the convertView with the CursorAdapter. That was my whole question. Because I'm trying to recycle the list items but at the same time I need the right way of passing my database results into the list adaptor.
kidalex
Developer
Developer
 
Posts: 42
Joined: Tue Jun 22, 2010 10:03 am

Re: Using ListAdapter to store more than one field per list

Postby padde » Fri Jun 25, 2010 8:46 pm

I totally dont get it. SimpleCursorAdapter DOES recycle the views. Maybe i dont get your problem.. please try
to explain as detailed as possible what you want.. because right now i dont see why a SimpleCursorAdapter
cant fit your needs.
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Re: Using ListAdapter to store more than one field per list

Postby kidalex » Fri Jun 25, 2010 9:33 pm

padde wrote:I totally dont get it. SimpleCursorAdapter DOES recycle the views. Maybe i dont get your problem.. please try
to explain as detailed as possible what you want.. because right now i dont see why a SimpleCursorAdapter
cant fit your needs.


I'm very new to this so please bare with me.

So, originally I was taught that the ListAdapter allows you to recycle the list item frames that are off the visual pane of the screen by having the convertView paramter passed to it. If one is passed than it spares the code to do an "expensive" operation of inflating a view. Correct me if I'm wrong at this.

However, once I started using the SQLite database I realized that in order for me to populate more than one field in each row - I have to somehow get the cursor into the adaptor and then exract the fields for each row that way. Correct me if I'm wrong at this point.

However, my dilemma is that with the CursorAdaptor I'm no longer able to receive that recycled row which means I'll have to inflate all of the rows and waste a lot of CPU and cache.

Thank you so much for your answer!
kidalex
Developer
Developer
 
Posts: 42
Joined: Tue Jun 22, 2010 10:03 am

Re: Using ListAdapter to store more than one field per list

Postby padde » Sat Jun 26, 2010 12:05 am

Okay now i get the problem here. You can tell the SimpleCursorAdapter to bind as many fields
as you want to the same amount of views in your listrow layout. It takes care of the hole recycle
stuff internally.. no need to worry about that.
Out of the box you can bind fields to textviews or imagesviews. If you use other views in your
listrow layout you can extend the functionality with a ViewBinder (without loss of recycling view
optimization).

If you can give me more details about what kind of data your rows (db rows) consists of and to
what kind of views you want to bind that data.. i could give you sample code.

Or take a look at this post: post42009.html#p42009
There i use a more complex SimpleCursorAdapter with a custom ViewBinder.. maybe this
is enough to get the idea behind that stuff :)
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Re: Using ListAdapter to store more than one field per list

Postby kidalex » Sat Jun 26, 2010 12:41 am

The data is very simple and it has three fields for each row in the database table ( plus the "_id" column which I understand is required ). My View is a row.xml file that has an image to the left and two rows of text to the right of it. Does this help?
kidalex
Developer
Developer
 
Posts: 42
Joined: Tue Jun 22, 2010 10:03 am

Re: Using ListAdapter to store more than one field per list

Postby padde » Sat Jun 26, 2010 12:54 am

Yes .. just one more thing.. from where do you get your images?! Resources or a blob in the database or (as recommended by google) as a uri string thats pointing to a file?
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Re: Using ListAdapter to store more than one field per list

Postby kidalex » Sat Jun 26, 2010 12:58 am

padde wrote:Yes .. just one more thing.. from where do you get your images?! Resources or a blob in the database or (as recommended by google) as a uri string thats pointing to a file?


images for now but that'll change
kidalex
Developer
Developer
 
Posts: 42
Joined: Tue Jun 22, 2010 10:03 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 14 guests