2

This is what I have so far:

The Custom Object:

class ItemObject {
    List<String> name;
    List<String> total;
    List<String> rating;

public ItemObject(List<ItemObject> io) {
    this.total = total;
    this.name = name;
    this.rating = rating;
 }
}

The Call to the Adapter:

List<String> names, ratings, totals;

ItemObject[] io= new ItemObject[3];
io[0] = new ItemObject(names);
io[1] = new ItemObject(rating);
io[2] = new ItemObject(totals);

adapter = new ItemAdapter(Items.this, io);
setListAdapter(adapter);

Assuming the above looks ok, my questions is how would I set up the ItemAdapter, it's constructor, and unwrap the three List's from the object. And then, in the getView, assign these things:

each matching position to:

    TextView t1 = (TextView) rowView.findViewById(R.id.itemName);
    TextView t2 = (TextView) rowView.findViewById(R.id.itemTotal);
    RatingBar r1 = (RatingBar) rowView.findViewById(R.id.ratingBarSmall);

For example, position 0 in the array "names" to t1. Position 0 in the array "totals" to t1. Position 0 in the array "ratings" to r1.

EDIT: I don't want someone to write the entire Adapter. I just need to know how to unwrap the Lists from the Custom Object so I can use the data. (Something not even brought up or asked about in another question)

5
  • This is awfully similar to your question yesterday: Passing Multiple Lists<String> into ArrayAdapter Commented Jul 21, 2012 at 15:56
  • The other question was simply asking on how (or what method to use) to do something. In fact, I am still researching HOW to do that. This question is referring to one SPECIFIC way of doing that. Also, a way NOT brought up at all in that question. I am still deciding on how to handle this. And I still thank everyone for their help in the answers that were provided. I very well may go back to one of those ideas. Commented Jul 21, 2012 at 16:05
  • A common way of thanking people is to upvote the meaningful answers and to mark the best answer as correct. Commented Jul 21, 2012 at 16:09
  • I take a while but I always go back to it once I know what's what. Although I should upvote sooner... (the answer isn't chosen, until I actually have one that I was able to use and implement) Commented Jul 21, 2012 at 16:12
  • Awesome. Luksprog has pointed you in the right direction, I'm not too sure anyone can significantly improve on it. Commented Jul 21, 2012 at 16:28

1 Answer 1

11

Your code will not work in its actual form. Do you really need lists of data in the ItemObject? My guess is no and you simply want a ItemObject that holds 3 Strings corresponding to the 3 views from your row layout. If this is the case:

class ItemObject {
    String name;
    String total;
    String rating;// are you sure this isn't a float

public ItemObject(String total, String name, String rating) {
    this.total = total;
    this.name = name;
    this.rating = rating;
 }
}

Then your lists will be merged into a list of ItemObject:

List<String> names, ratings, totals;
ItemObject[] io= new ItemObject[3];
// use a for loop
io[0] = new ItemObject(totals.get(0), names.get(0), ratings(0));
io[1] = new ItemObject(totals.get(1), names.get(1), ratings(1));
io[2] = new ItemObject(totals.get(2), names.get(2), ratings(2));
adapter = new ItemAdapter(Items.this, io);
setListAdapter(adapter);

And the adapter class:

public class ItemAdapter extends ArrayAdapter<ItemObject> {

        public ItemAdapter(Context context,
                ItemObject[] objects) {
            super(context, 0, objects);         
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // do the normal stuff
            ItemObject obj = getItem(position);
            // set the text obtained from obj
                    String name = obj.name; //etc       
                    // ...

        }       

}
Sign up to request clarification or add additional context in comments.

1 Comment

Since this was voted to be closed (for some reason) I am going to vote this as correct before its too late. Wanted to make sure you get credit and this was what I was looking for!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.