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<treking...@gmail.com> wrote:
On Thu, Jan 13, 2011 at 3:41 AM, gonzobrains<gonzobra...@gmail.com> 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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en