没有填充Listview,也没有调用getView函数

时间:2013-01-20 13:59:16

标签: android android-listview

我是Android编程的新手。我正在尝试购物车教程尝试使用listview填充目录视图的productAdapter。但是列表没有填充,并且getview函数也没有被调用。我附上了我的文件。请告诉我,我哪里出错了。

ProductAdapter.java

package com.example.helloshoppingcart;

import java.util.List;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

    public class ProductAdapter extends BaseAdapter {
    private class viewItem{
        ImageView productImageView;
        TextView productTitle;
        CheckBox productCheckbox;
    };

    private List<Product> mproductList;
    private LayoutInflater minflater;
    private Boolean mShowCheckbox;

    ProductAdapter(List<Product> productList, LayoutInflater inflater, Boolean showCheckbox)
    {
            this.mproductList = productList;
        this.minflater = inflater;
        this.mShowCheckbox = showCheckbox;              
    }

    @Override
    public View getView(int pos, View convertView, ViewGroup parent ){
        final viewItem item;

        if (convertView == null){
            convertView  = minflater.inflate(R.layout.item, null);
            item = new viewItem();

            item.productImageView = (ImageView) convertView.findViewById(R.id.ImageViewItem);
            item.productTitle= (TextView) convertView.findViewById(R.id.TextViewItem);
            //item.productCheckbox = (CheckBox) convertView.findViewById(R.id.CheckBoxSelected);
            convertView.setTag(item);
        }else{
            item = (viewItem) convertView.getTag();
        }

        Product prod = mproductList.get(pos);
        item.productImageView.setImageDrawable(prod.productImage);
        //item.productCheckbox.setChecked(prod.selected);
        item.productTitle.setText(prod.title);

        return convertView;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
}

CatalogActivity.java

package com.example.helloshoppingcart;

import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;

public class CatalogActivity extends Activity {
    private List<Product> mproductList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_catalog);

        //get the catalog and display all the records
        mproductList = shoppingCartHelper.getCatalog(getResources());
        ListView catalogListView = (ListView) findViewById(R.id.ListViewCatalog);
        ProductAdapter catalogListAdapter = new ProductAdapter(mproductList, getLayoutInflater(), false);
        catalogListView.setAdapter(catalogListAdapter);

        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.activity_catalog, menu);
        return true;
    }

}

activity_catalog.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" android:background="#ffffff">

    <!--<TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:textColor="#000000"
        android:textSize="24dip" android:layout_margin="5dip" android:text="Product Catalog"></TextView>-->
    <ListView android:layout_height="wrap_content"
        android:layout_weight="1" android:id="@+id/ListViewCatalog"
        android:layout_width="fill_parent" android:background="#ffffff"
        android:clickable="true" android:cacheColorHint="#ffffff">
    </ListView>
    <!-- <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_margin="5dip"
        android:layout_gravity="right" android:id="@+id/ButtonViewCart"
        android:text="View Shopping Cart"></Button>-->
</LinearLayout>

item.xml

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

    <ImageView android:id="@+id/ImageViewItem"
    android:layout_margin="5dip"
    android:layout_height="wrap_content"
    android:layout_width="100dip">
    </ImageView>

    <TextView android:id="@+id/TextViewItem"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="5dip"
    android:textSize="26dip"
    android:text="Phone Names"
    android:textColor="#000000"
    android:minLines="2"
    android:maxWidth="150dip">        
     </TextView>

    <TextView android:id="@+id/TextView01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1">        
    </TextView>

    <!-- <CheckBox android:layout_height="wrap_content"
        android:layout_margin="5dip" android:id="@+id/CheckBoxSelected"
        android:focusable="false" android:clickable="false"
        android:layout_gravity="center" android:layout_width="wrap_content">
    </CheckBox>-->

</LinearLayout>

2 个答案:

答案 0 :(得分:3)

在ProductAdapter中,getCount()方法返回与此ListView关联的Adapter所拥有的项目数。因此,它应该返回列表视图将显示的最大项目数,即您用作数据源的列表的大小:mproductList.size() 不会调用getView()方法,因为此函数在代码中返回0。

答案 1 :(得分:1)

只是快速浏览一下,但getCount()应该返回项目数 在适配器中,因此mproductList.size()