android - Listview 未填充且 getView 函数也未被调用

标签 android android-listview

我是 Android 编程新手。我正在尝试使用 productAdapter 填充目录 View 的 listview 的购物车教程。但是列表没有被填充,而且 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>

最佳答案

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

关于android - Listview 未填充且 getView 函数也未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14425218/

相关文章:

android - 在 Android 多用户应用程序中存储设置

java - 在 fragment 中填充 JSON ListView

android - 从共享首选项数据填充列表项

android - 解析 API 本地数据存储 - pinInBackground()

安卓 : getChild() EditText values on button click in ExpandableListView

android - 微调器没有获得焦点

Android 设置壁纸与 URI

java - 如何在不失真的情况下将图像纹理到球体上

Android - 每次旋转设备时,RecyclerView 都会倍增

java - 安卓:后台listview