Kostya,

Sarcasm aside, I appreciate your post as I believe it solved my
problem.  I didn't realize that the BookAdapter constructor added the
books to the adapter.  I was modeling after a few tutorials I had seen
and must have combined their code without realizing I was repeating
tasks.  I didn't catch this in the BookAdapter's documentation I was
reading.

In any case, I understand your comment on the data member for holding
data in BookAdapter, but that's not exactly what I am doing, as
m_books is just a singleton I created so I can access the books from
multiple activities.  Perhaps there is a better way to do it, but this
was my first shot at trying it:

public class ShoppingCartSingleton
{
        private static ShoppingCartSingleton instance;

        // data goes here as non-static variables
        public ArrayList<Book> m_books = null;

        // constructor
        public ShoppingCartSingleton()
        {
                m_books = new ArrayList<Book>();
                Log.i("ARRAY", "" + m_books.size());
        }

        public static ShoppingCartSingleton getInstance()
        {
                if (instance == null)
                {
                        instance = new ShoppingCartSingleton();
                }
                return instance;

        }

}

I'm just showing you this for your reference.  I needed a quick and
dirty way to get access to the books added from one activity, to
another activity which displays them.  I'm up for better suggestions.

In any case, I was using the debugger, actually, but I just didn't
realize the constructor did any initial population.  When I stepped
over the code I wasn't looking at the initialization part.  Had I
known it did the adding I would have checked that.  In any case,
thanks for helping.

gb



On Jan 13, 11:57 am, Kostya Vasilyev <[email protected]> wrote:
> First you do:
>
> new BookAdapter(this, R.layout.cartitem,
>                 shoppingCart.m_books);
>
> which stores the array of books from shoppingCart.m_books in the
> adapter's "books" variable.
>
> Then you run a for loop, calling
>
> m_adapter.add(shoppingCart.m_books.get(i));
>
> adding books to the adapter again.
>
> I'd say, keep one or the other (unless your motto is "Too much is never
> enough").
>
> Also, you don't need a data member for holding data in your BookAdapter,
> since ArrayAdapter already can maintain an array of items. Instead of
> adapter.books.get(i), do adapter.getItem(i).
>
> Agree with TreKing though - learn how to use the debugger. Development
> tools have improved a lot since the punchcard era.
>
> -- Kostya
>
> 13.01.2011 22:01,gonzobrainsпишет:
>
>
>
>
>
>
>
>
>
> > Yeah, I kind of get the fact that I need to only add them once, but I
> > don't see how it is that I'm adding them twice right now.
>
> > Anyway, here is the code:
>
> > package com.sellbackyourbook.sellback;
>
> > import java.io.IOException;
> > import java.util.ArrayList;
> > import java.util.Iterator;
>
> > import android.app.Activity;
> > //import android.app.ListActivity;
> > import android.content.Context;
> > import android.content.Intent;
> > import android.net.Uri;
> > import android.os.Bundle;
> > import android.util.Log;
> > import android.view.LayoutInflater;
> > import android.view.View;
> > import android.view.ViewGroup;
> > import android.widget.ArrayAdapter;
> > import android.widget.Button;
> > import android.widget.ListView;
> > import android.widget.TextView;
>
> > public class cart extends Activity
> > {
> >    private ListView m_bookListView;
> >    private BookAdapter m_adapter;
>
> >    //private static String[] data = new String[] = { ""
>
> >    /** Called when the activity is first created. */
> >      public void onCreate(Bundle savedInstanceState)
> >      {
> >            ShoppingCartSingleton shoppingCart =
> > ShoppingCartSingleton.getInstance();
>
> >          super.onCreate(savedInstanceState);
> >          setContentView(R.layout.shoppingcart);
>
> >          this.m_adapter = new BookAdapter(this, R.layout.cartitem,
> >            shoppingCart.m_books);
>
> >          m_bookListView = (ListView) findViewById(R.id.BookList);
> >          m_bookListView.setAdapter(this.m_adapter);
>
> >          //setListAdapter(this.m_adapter);
>
> >            if (shoppingCart.m_books != null&&  shoppingCart.m_books.size()>
> > 0)
> >            {
> >              //m_adapter.notifyDataSetChanged();
>
> >                    try
> >                    {
>
> >                    //m_adapter.clear();
> >                            //for(int i=0;i<1;i++)
>
> >                    Log.i("ARRAY", "m_books.size() before loop" +
> > shoppingCart.m_books.size());
>
> >                    int size = shoppingCart.m_books.size();
>
> >              for(int i=0;i<size;i++)
> >              {
> >                    Log.i("ARRAY", "size in loop" + size);
> >                    Log.i("ARRAY", "adding item to adapter" + i);
> >                    m_adapter.add(shoppingCart.m_books.get(i));
> >              }
>
> >            } catch (RuntimeException e) {
> >                    e.printStackTrace();
> >          }
>
> >          //m_adapter.notifyDataSetChanged();
> >            }
>
> >          Button buttonAddAnother = (Button)
> > findViewById(R.id.AddAnother);
> >          buttonAddAnother.setOnClickListener(new
> > View.OnClickListener()
> >          {
> >              public void onClick(View view)
> >              {
> >                  Intent intent = new Intent();
> >                  setResult(RESULT_OK, intent);
> >                  finish();
> >              }
> >          });
>
> >          // TODO: only show this button if the shopping cart is not
> > empty
>
> >          Button buttonCheckout = (Button) findViewById(R.id.Checkout);
> >          buttonCheckout.setOnClickListener(new View.OnClickListener()
> >          {
> >              public void onClick(View view)
> >              {
> >                    // TODO: open sellbackyourbook website using book ISBNs
>
> >                    ShoppingCartSingleton shoppingCart =
> > ShoppingCartSingleton.getInstance();
>
> >                    String isbnList = "";
> >                    String checkoutURL = "http://www.sellbackyourbook.com/
> > androidcart.php?isbn=";
>
> >                    for (Iterator<Book>  i = shoppingCart.m_books.iterator();
> > i.hasNext();  )
> >                    {
> >                            Book currentBook = (Book) i.next();
> >                            isbnList = isbnList + currentBook.getBookISBN() 
> > + ",";
> >                    }
>
> >                    checkoutURL = checkoutURL + isbnList;
> >                    Log.i("CHECKOUT URL", "checkout URL to submit: " +
> > checkoutURL);
>
> >                    Intent myIntent = new Intent(Intent.ACTION_VIEW);
> >                    myIntent.setData(Uri.parse(checkoutURL));
> >                    startActivity(myIntent);
> >              }
> >          });
>
> >      }
>
> >      private class BookAdapter extends ArrayAdapter<Book>  {
>
> >          private ArrayList<Book>  books;
>
> >          public BookAdapter(Context _context, int _textViewResourceId,
> > ArrayList<Book>  _books)
> >          {
> >                  super(_context, _textViewResourceId, _books);
> >                  this.books = _books;
> >          }
>
> >          @Override
> >          public View getView(int position, View convertView, ViewGroup
> > parent)
> >          {
>
> >            System.out.println("getView " + position + " " +
> > convertView);
>
> >            View v = convertView;
>
> >                  if (v == null) {
> >                      LayoutInflater vi =
> > (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
> >                      v = vi.inflate(R.layout.cartitem, null);
> >                  }
>
> >                  Book b = books.get(position);
>
> >                  if (b != null)
> >                  {
> >                    TextView bTitle = (TextView)
> > v.findViewById(R.id.BookTitle);
> >                      TextView bPrice = (TextView)
> > v.findViewById(R.id.BookPrice);
>
> >                          if (bTitle != null)
> >                          {
> >                            bTitle.setText(b.getBookTitle());
> >                          }
>
> >                          if (bPrice != null)
> >                          {
> >                            bPrice.setText(b.getBookPrice());
> >                          }
> >                  }
>
> >                  return v;
> >          }
> >      }
>
> > }
>
> > On Jan 13, 10:35 am, TreKing<[email protected]>  wrote:
> >> On Thu, Jan 13, 2011 at 3:41 AM,gonzobrains<[email protected]>  wrote:
> >>> What am I doing wrong?
> >> Possible adding the items twice.
>
> >>>    How can I correct it?
> >> Only add them once.
>
> >> Post the code where you're adding to the adapter and where you getView(), 
> >> if
> >> you overrode it.
>
> >> ---------------------------------------------------------------------------
> >>  ----------------------
> >> TreKing<http://sites.google.com/site/rezmobileapps/treking>  - Chicago
> >> transit tracking app for Android-powered devices
>
> --
> Kostya Vasilyev -- WiFi Manager + pretty widget 
> --http://kmansoft.wordpress.com

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to