我是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>