Reputation: 11
I am new to Android
programming. I'm trying Shopping cart tutorial trying to populate the listview
of catalog view using the productAdapter
. But the list is not getting populated and also getview
function is not getting called. I'am attaching my files. Please let me know,where I'am going wrong.
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>
Upvotes: 1
Views: 166
Reputation: 977
In ProductAdapter, the getCount() method returns the number of items owned by the Adapter associated with this ListView. So, it should return the maximum number of items that the listview will display i.e. the size of the list you are using as a data source : mproductList.size() The getView() method is not getting called because this function is returning 0 in your code.
Upvotes: 3
Reputation: 9419
Only took a fast glance, but getCount() should return the number of items in the adapter, and therefore mproductList.size()
Upvotes: 1