2013-01-20 110 views
1

我是Android編程的新手。我正在嘗試使用productAdapter填充目錄視圖的listview購物車教程。但是該列表沒有被填充,並且getview函數沒有被調用。我附上我的文件。請讓我知道,我哪裏出錯了。Listvew未被填充並且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> 

回答

1

只用了快一目瞭然,但getCount將()應在適配器返回項目 的數量,因此mproductList.size()

3

在ProductAdapter,利用getCount()方法返回的數量由與此ListView關聯的Adapter所擁有的項目。因此,它應該返回列表視圖將顯示的最大項目數,即您用作數據源的列表大小:mproductList.size() getView()方法沒有被調用,因爲此函數返回0在你的代碼中。