Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:ListVew中的重复项。可能调用getView()的次数太多了?_Android_Listview_Listadapter - Fatal编程技术网

Android:ListVew中的重复项。可能调用getView()的次数太多了?

Android:ListVew中的重复项。可能调用getView()的次数太多了?,android,listview,listadapter,Android,Listview,Listadapter,我试图创建一个简单的程序,显示一个“购物” 购物车”项目列表,以及下面的几个按钮,用于管理 推车 最大的问题是,项目在数据库中得到了重复的条目 列表视图。也就是说,对于我想输入的每个项目,我都会看到它出现 在列表视图中执行两次。有什么问题吗?此外,可滚动的 我的购物车面积不够大。我如何设置它,使它是 更大,但我仍然可以看到我的按钮?也许我应该把这个 购物车上方的按钮 以下是我的购物车的布局XML: <?xml version="1.0" encoding="utf-8"?> <

我试图创建一个简单的程序,显示一个“购物” 购物车”项目列表,以及下面的几个按钮,用于管理 推车

最大的问题是,项目在数据库中得到了重复的条目 列表视图。也就是说,对于我想输入的每个项目,我都会看到它出现 在列表视图中执行两次。有什么问题吗?此外,可滚动的 我的购物车面积不够大。我如何设置它,使它是 更大,但我仍然可以看到我的按钮?也许我应该把这个 购物车上方的按钮

以下是我的购物车的布局XML:

<?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>

以下是各行项目的布局:

<?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>

以下是购物车活动的java代码:

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;
        }
    }   
}
package com.sellbackyourbook.sellback;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.Iterator;
导入android.app.Activity;
//导入android.app.ListActivity;
导入android.content.Context;
导入android.content.Intent;
导入android.net.Uri;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.ListView;
导入android.widget.TextView;
公共类cart扩展活动
{
私有ListView m_bookListView;
私人图书适配器m_适配器;
//私有静态字符串[]数据=新字符串[]={“”
/**在首次创建活动时调用*/
创建时的公共void(Bundle savedInstanceState)
{
ShoppingCartSingleton shoppingCart=ShoppingCartSingleton.getInstance();
super.onCreate(savedInstanceState);
setContentView(R.layout.shoppingcart);
this.m_adapter=新BookAdapter(this,R.layout.cartitem,
shoppingCart.m_books);
m_bookListView=(ListView)findViewById(R.id.BookList);
m_bookListView.setAdapter(这个.m_适配器);
//setListAdapter(此.m_适配器);
if(shoppingCart.m_books!=null&&shoppingCart.m_books.size()>0)
{
//m_adapter.notifyDataSetChanged();
尝试
{
//m_适配器。清除();

//对于(inti=0;i我正在做完全相同的事情,并且根本不调用我的getView。 以下是我灵感来源的剧本,也许会有所帮助:

移除我使用的循环修复了问题:

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

似乎
BookAdapter
构造函数已经负责填充它了,所以我使用
add()
方法复制了这些项。

是的,您在
BookAdapter
的构造函数方面是对的。但是
BookAdapter
getView()
方法是错误的。请看一看(来自谷歌)要获得如何使用
ListView

的正确方法,您应该在java代码中添加相关部分。我想我可以回答您问题的另一半。您可以将ScrollView布局高度设置为填充父级,然后在其上添加布局权重=“1”。我已更新了我的问题。谢谢。有点无关,但…(a)摆脱ScrollView。ScrollView中的ListView不好。(b)布局宽度和布局高度不使用绝对像素值,尽可能使用换行内容或匹配父项。如果需要固定的数字尺寸,请使用dp而不是px。(c)由于您在cart.xml中有一个LinearLayout,请将您的ListView的布局高度设置为0dp,布局权重设置为1。不要为其他视图指定布局权重。这样做会拉伸ListView以占用所有可用空间,而不会将其他视图推离屏幕。我看到了该教程,尽管我认为最后我一次又一次地结束了建模一个。我仍然看不出我到底做错了什么。我成功地使用了上面的教程。在查看了您的代码后,我看到了一些奇怪的事情:您应该在getView中从Adapter而不是onCreate中将项目添加到Adapter中。因为您的列表在创建时已经传递给了Adapter:this.m_Adapter=new BookAdapter(this,R.layout.cartim,shoppingCart.m_books);…m_bookListView.setAdapter(this.m_adapter);我正在尝试帮助您,但我是新手。