posted 9 years ago
I'm trying to create a shopping cart that will be displayed in an alert dialog... But I can't understand why the listview is showing up only one custom item...
cart_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="8dp">
<ListView
android:layout_width="match_parent"
android:padding="6dp"
android:layout_weight="1"
android:background="#FFFFFF"
android:id="@+id/productTable"
android:layout_height="wrap_content"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:visibility="gone"
android:id="@+id/cartbtnhandle">
<Button
android:id="@+id/btnremove"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFF"
android:layout_weight="1"
android:text="Remove"
android:textColor="#228b22"
android:textStyle="bold" />
<Button
android:id="@+id/btnBuy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFF"
android:layout_weight="1"
android:text="Buy Now"
android:textColor="#228b22"
android:textStyle="bold" />
</LinearLayout>
cart_item.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/prodcheckbox"
android:layout_gravity="center_vertical"
android:visibility="visible"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1."
android:id="@+id/prodno"
android:layout_gravity="center_vertical"
android:padding="2dp"
android:layout_weight="0.1"/>
<ImageView
android:id="@+id/prodimg"
android:layout_width="0dp"
android:layout_height="50dp"
android:scaleType="fitXY"
android:src="@drawable/puppy_01"
android:layout_weight="3"
android:tag="prodimg"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product name"
android:layout_gravity="center_vertical"
android:padding="2dp"
android:id="@+id/prodname"
android:layout_weight="6"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rs. 100 "
android:id="@+id/prodamt"
android:layout_gravity="center_vertical"
android:padding="2dp" />
CartPg.java
final View cartView = (LayoutInflater.from(parentContext)).inflate(R.layout.tab_4, null);
final ListView prodtable = (ListView) cartView.findViewById(R.id.productTable);
// get all the products from db
prodList = new DatabaseHandler(parentContext).getAllProducts();
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
Iterator itr = prodList.iterator();//getting Iterator from arraylist to traverse elements
prodView=new View[prodList.size()];
try {
int j=0;
while (itr.hasNext() && j<prodList.size()) {
System.out.println("imgurl : " + prodList.get(j).getProduct_imgurl());
prodList.get(j).setImg(Drawable.createFromStream((InputStream) new URL(prodList.get(j).getProduct_imgurl()).getContent(), "srcname"));
j++;
}
}
catch(IOException e) {
e.printStackTrace();
}
}
@Override
protected void onPostExecute(Void unused) {
/**************** Create Custom Adapter *********/
Resources res = parentContext.getResources();
CustomProductAdapter adapter=new CustomProductAdapter( parentContext, prodList,res,(LayoutInflater.from(parentContext)).inflate(R.layout.cart_item, prodtable, false) );
prodtable.setAdapter( adapter );
System.out.println("Total no. of items : "+prodList.size());
}
}.execute();
CustomProductAdapter.java
public class CustomProductAdapter extends BaseAdapter implements View.OnClickListener {
private List<ArraysClass> data = new ArrayList<ArraysClass>();
private LayoutInflater inflater;
public Resources res;
ArraysClass tempValues=null;
View prodView;
int i=0;
Context parentContext;
public CustomProductAdapter(Context parentContext, List<ArraysClass> prodList, Resources res, View prodView) {
data = prodList;
this.res = res;
this.prodView = prodView;
this.parentContext = parentContext;
inflater = LayoutInflater.from(parentContext);
}
/******** Check Passed Arraylist Size ************/
public int getCount() {
if(data.size()<=0)
return 1;
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
/********* Create a holder Class to contain inflated xml file elements *********/
public static class ViewHolder{
CheckBox prodcheck;
TextView prodno;
ImageView prodimg;
TextView prodname;
TextView prodamt;
}
@Override
/****** Depends upon data size called for each row , Create each ListView row *****/
public View getView(int position, View convertView, ViewGroup viewGroup) {
System.out.println("position ########" + position);
View vi = convertView;
ViewHolder holder;
if(convertView==null){
/****** Inflate cart_item.xml file for each row ( Defined below ) *******/
//vi = prodView; // (LayoutInflater.from(parentContext)).inflate(R.layout.cart_item, prodtable, false)
vi = inflater.inflate(R.layout.cart_item,null);
/****** View Holder Object to contain tabitem.xml file elements ******/
holder = new ViewHolder();
holder.prodcheck = (CheckBox) vi.findViewById(R.id.prodcheckbox);
holder.prodno = (TextView) vi.findViewById(R.id.prodno);
holder.prodimg=(ImageView)vi.findViewById(R.id.prodimg);
holder.prodname=(TextView)vi.findViewById(R.id.prodname);
holder.prodamt=(TextView)vi.findViewById(R.id.prodamt);
/************ Set holder with LayoutInflater ************/
vi.setTag( holder );
// position++;
}
else
holder=(ViewHolder)convertView.getTag();
/***** Get each Model object from Arraylist ********/
tempValues = ( ArraysClass ) data.get(position);
/************ Set Model values in Holder elements ***********/
holder.prodno.setText("" + (position+1) );
holder.prodimg.setImageDrawable(tempValues.getImg());
// Todo : Set size Of image
holder.prodname.setText(tempValues.getProduct_name());
holder.prodamt.setText(tempValues.getProduct_price());
holder.prodcheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b) {
CartHandler.checkedProdList.add(tempValues);
}
else
if(CartHandler.checkedProdList.contains(tempValues)){
CartHandler.checkedProdList.remove(tempValues);
}
}
});
return vi;
}
@Override
public void onClick(View view) {
}
/********* Called when Item click in ListView ************/
private class OnItemClickListener implements View.OnClickListener {
private int mPosition;
OnItemClickListener(int position) {
mPosition = position;
//new ProductDescription(prodView ,parentContext ,data.get(position));
}
@Override
public void onClick(View arg0) {
// CartHandler sct = (CartHandler) activity;
/**** Call onItemClick Method inside CartHandler Class ( See Below )****/
// sct.onItemClick(mPosition);
}
}
}
I'm correctly getting the values from database... But in CustomProductAdapter the position in getView() is always printed zero... I can't get why is this? Also Note that my listview isn't present in scrollview.
Thanks in advance.