2011-01-13 10 views
1

Ich versuche, ein einfaches Programm zu erstellen, das eine "shopping Warenkorb" Liste von Elementen, zusammen mit ein paar Schaltflächen darunter, um den Einkaufswagen zu verwalten. Das größte Problem besteht darin, dass Elemente doppelte Einträge in der Listenansicht erhalten. Das heißt, für jedes Element, das ich eingeben möchte, sehe ich es zweimal in der Listenansicht erscheinen. Was ist das Problem? Auch der scrollbare Bereich meines Wagens ist nicht groß genug. Wie stelle ich es so ein, dass es größer ist, aber ich kann immer noch meine Tasten sehen? Vielleicht sollte ich die Buttons über den Warenkorb legen?Android: doppelte Einträge in ListVew. Vielleicht getView() zu oft aufgerufen?

Hier ist mein Warenkorb XML-Layout:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Shopping Cart" /> 

    <ScrollView android:id="@+id/ScrollView01" 
     android:layout_width="fill_parent" 
     android:layout_height="110px"> 

     <ListView 
      android:id="@+id/BookList" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
     </ListView> 

    </ScrollView> 

    <Button android:text="Add Another Book" 
     android:id="@+id/AddAnother" 
     android:layout_width="250px" 
     android:textSize="18px" 
     android:layout_height="55px"> 
    </Button> 

    <Button android:text="Checkout" 
     android:id="@+id/Checkout" 
     android:layout_width="250px" 
     android:textSize="18px" 
     android:layout_height="55px"> 
    </Button> 

</LinearLayout> 

Hier ist das Layout für einzelne Zeile Artikel:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:padding="8dip"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     android:layout_height="fill_parent"> 

     <TextView 
      android:id="@+id/BookTitle" 
      android:layout_width="fill_parent" 
      android:layout_height="0dip" 
      android:layout_weight="1" 
      android:singleLine="true" 
      android:gravity="center_vertical" 
     /> 

     <TextView 
      android:id="@+id/BookPrice" 
      android:layout_width="fill_parent" 
      android:layout_height="0dip" 
      android:layout_weight="1" 
      android:singleLine="true" 
      android:ellipsize="marquee" 
     /> 
    </LinearLayout> 

    <Button 
     android:id="@+id/buttonLine" 
     android:gravity="center" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_alignParentRight="true" 
     android:text="Delete" 
    /> 
</LinearLayout> 

hier ist der Java-Code für die Aktivität Warenkorb:

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; 
     } 
    } 
} 

Hier ist der Java-Code für meinen Einkaufswagen. Benutze ich den Singleton richtig? Ich wollte einfach nur einen schnellen und schmutzigen Weg, um mehreren Aktivitäten den Zugriff auf den Warenkorb zu ermöglichen, da eine andere Aktivität die Bücher tatsächlich vom Benutzer ergreift und diese Aktivität sie im Warenkorb anzeigt.

Ich hatte ein Problem durch die Bücher in onCreate() iterating. Die Funktion size() hat aus irgendeinem Grund in der Schleife weiter zugenommen, also habe ich den Code geändert und eine Variable "Größe" hinzugefügt, um zu vermeiden, dass der Aufruf von size() in der Schleife selbst erfolgt. Ich bin nicht wirklich sicher, worum es ging.

+1

sollten Sie relevant Teil Ihres Java-Codes –

+0

hinzufügen Ich denke, ich kann andere Hälfte Ihrer Frage beantworten. Sie können scrollView layout_height auf fill_parent setzen und dann ein layout_weight = "1" hinzufügen. – xandy

+0

Ich habe meine Frage aktualisiert. Vielen Dank. – gonzobrains

Antwort

0

die Schleife entfernen, wo ich hatte das Problem behoben:

m_adapter.add(shoppingCart.m_books.get(i)); 

Es scheint, wie der BookAdapter Konstruktor bereits kümmerten sie bevölkern , also kopierte ich die Artikel mit der add() Methode.

0

Ich mache genau das gleiche und mein getView wird überhaupt nicht aufgerufen. hier ist das Skript, das ich von vielleicht inspiriert würde es helfen: http://mfarhan133.wordpress.com/2010/10/14/list-view-tutorial-for-android/

+0

Ich habe dieses Tutorial gesehen, obwohl ich am Ende dachte, dass ich nach einem anderen modelliert habe. Ich kann immer noch nicht genau sehen, was ich falsch mache. – gonzobrains

+0

Ich hatte Erfolg mit dem obigen Tutorial. Nachdem ich Ihren Code angeschaut habe, sehe ich seltsame Dinge: Sie sollten Ihr Element zum Adapter in getView von Adapter hinzufügen, nicht in onCreate. Da Ihre Liste beim Erstellen bereits an den Adapter übergeben wurde: this.m_adapter = new BookAdapter (this, R.layout.cartitem, shoppingCart.m_books); ... m_bookListView.setAdapter (this.m_adapter); Ich versuche zu helfen, aber ich bin neu hier. – Spir

0

Ja, Sie sind genau an der Stelle BookAdapter 's Konstruktor. Aber die getView() Methode von BookAdapter ist falsch. Bitte werfen Sie einen Blick auf http://www.youtube.com/watch?v=wDBM6wVEO70 (von Google), um den richtigen Weg zu finden, wie man mit ListView arbeitet.