Thursday, December 6, 2012

[android-developers] How to Remove Products From ListView...

Hi,
I have a ListView with the Chekboxes. I want to remove   the selected items from the ListView..for example: i selecting the multiple items with  Checkboxes and clicking the single delete button to delete all the files... i have made changes in below CheckOutClass but  after selecting checkboxes when i click on remove Button this code giving me logcat messages like:

 java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -1
at java.util.Vector.removeElementAt(vector.java:806)
at com.test.CheckOutClass.onClick(CheckOutClass.java:166)

Any help will be much appreciated. Thank you....

Here is my code:

CheckOutClass.java

package com.test;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import com.google.zxing.client.android.R;

import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class CheckOutClass extends Activity implements OnClickListener{
public  static int finalPrice = 0;
int unitPrice=0 ;
int quantity =0 ;
int[] totalPrice = null;
CheckBox[] chkbx = null;
LinearLayout ll = null;
ScrollView sv  =null;
Button btnremove;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sv = new ScrollView(this);
ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
LoadUi();
}

public void LoadUi(){

int lengthOfRecords = helper.vctrcategoryIds.size();
chkbx = new CheckBox[lengthOfRecords];

for(int i = 0; i < helper.vctrcategoryIds.size(); i++) {

ImageView imgView = new ImageView(this);
imgView.setImageBitmap(helper.vctrImages.elementAt(i));
ll.addView(imgView);
TextView txtDescription=  new TextView(this);
txtDescription.setText(helper.vctrdescription.elementAt(i).toString());
ll.addView(txtDescription);
TextView txtPrice=  new TextView(this);
txtPrice.setText("Unit Price: " + helper.vctrprice.elementAt(i));
ll.addView(txtPrice);
TextView txtQuantity=  new TextView(this);
txtQuantity.setText("Quantity: "+ helper.vctrQuantity.elementAt(i));
ll.addView(txtQuantity);
//remove button

  chkbx[i]=  new CheckBox(this);
  chkbx[i].setId(i);
 //btnRemove[i].setOnClickListener(this);
  ll.addView(chkbx[i]);
  
 btnremove = new Button(this);
 btnremove.setText("Remove");
 ll.addView(btnremove);
 btnremove.setOnClickListener(this); 

}

totalPrice = new int[helper.vctrprice.size()];
for(int i=0;i<helper.vctrprice.size();i++){
   
   String strPrice1 = helper.vctrprice.elementAt(i).toString();
   unitPrice = Integer.parseInt(strPrice1);
   String strQuantity1 = helper.vctrQuantity.get(i).toString();
   quantity = Integer.parseInt(strQuantity1);
   
   totalPrice[i] = unitPrice*quantity;
   
   System.out.println("Total price: per product: " + totalPrice[i]);
  }
int finalPrice = 0;
for(int i=0;i<totalPrice.length;i++){
  
  finalPrice =  finalPrice + totalPrice[i];
//
 
}
System.out.println("final Price: " + finalPrice);
   //images.finalPrice = finalPrice;
   helper.finalprice = finalPrice;
   
   String str =  Integer.toString(finalPrice);
   TextView txtfinalprice=  new TextView(this);
   txtfinalprice.setText("Total Price: " + str);
   ll.addView(txtfinalprice);
   ImageView btnAddMore = new ImageView(this);
   btnAddMore.setImageResource( R.drawable.moreproducts );
   btnAddMore.setMaxHeight(35);
   btnAddMore.setMaxWidth(15);
   btnAddMore.setAdjustViewBounds(true);
   ll.addView(btnAddMore);
   
   btnAddMore.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent("com.test.spinnerdem",null);
startActivity(i);
}
});

// Proceed to check out
ImageView procedCheckout = new ImageView(this);
procedCheckout.setImageResource( R.drawable.proceed );
procedCheckout.setMaxHeight(35);
procedCheckout.setMaxWidth(15);
procedCheckout.setAdjustViewBounds(true);
ll.addView(procedCheckout);
procedCheckout.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(helper.userLoggedIn == null){
Intent i = new Intent("com.test.androidlogin",null);
startActivity(i);
}
else {
   Toast.makeText(CheckOutClass.this, "User Logged In", Toast.LENGTH_LONG).show();
   Intent i = new Intent("com.test.billing", null);
   startActivity(i);
   
}
}
});
this.setContentView(sv);
}


public void onClick(View v) {
// TODO Auto-generated method stub
for(int i = 0; i < helper.vctrcategoryIds.size(); i++) {
if(chkbx[i].isChecked())
{
ll.removeAllViews();
int Id = v.getId();
System.out.println("Button Clicked"+ v.getId());
helper.vctrIds.removeElementAt(Id);
helper.vctrshopIds.removeElementAt(Id);
helper.vctrcategoryIds .removeElementAt(Id);
helper.vctrproducts.removeElementAt(Id);
helper.vctrprice.removeElementAt(Id);
helper.vctrdescription.removeElementAt(Id);
helper.vctrImages.removeElementAt(Id);
helper.vctrQuantity.removeElementAt(Id);

}
else
{
Toast.makeText(CheckOutClass.this, "Select Items", Toast.LENGTH_LONG).show();
}
LoadUi();
}}}

helper.java

package com.test;

import java.util.Vector;

import android.graphics.Bitmap;

public class helper {


public static String selectedRecord = null;
public static long shopId;
public static int catId;

public static String userName = null;
public static String addreess = null;

public static String userLoggedIn = null;
public static Vector vctrIds = new Vector();
public static Vector vctrshopIds = new Vector();
public static Vector vctrcategoryIds = new Vector();
public static Vector vctrproducts = new Vector();
public static Vector vctrprice = new Vector();
public static Vector vctrdescription = new Vector();
//public static Vector images= new Vector();
public static Vector<Bitmap> vctrImages = new Vector<Bitmap>();
public static Vector vctrQuantity = new Vector();
public static String strTempIds = null;
public static String strshopIds = null;
public static String strTempcategoryIds =null;
public static String strTempproducts = null;
public static String strTempprice = null;
public static String strTempdescription =null;
public static Bitmap imgTemp = null;



public static int finalprice = 0;
//public static Vector images= new Vector();
//public static Vector<Bitmap> vctrImages = new Vector<Bitmap>();
//public static Vector vctrQuantity = new Vector();



public static void logout(){
userLoggedIn = null;
userName = null;
addreess = null;
finalprice = 0;
strTempIds = null;
strshopIds = null;
strTempcategoryIds =null;
strTempproducts = null;
strTempprice = null;
strTempdescription =null;
imgTemp = null;
vctrIds.removeAllElements();
vctrshopIds.removeAllElements();
vctrcategoryIds .removeAllElements();
vctrproducts.removeAllElements();
vctrprice.removeAllElements();
vctrdescription.removeAllElements();
vctrImages.removeAllElements();
vctrQuantity.removeAllElements();
//vctrdescription .removeAllElements();
//vctrproducts .removeAllElements();
//vctrprice .removeAllElements();
//vctrdescription .removeAllElements();
Constants.vctrCategoryId .removeAllElements();
Constants.vctrCategory.removeAllElements();
Constants.vctrImagePath.removeAllElements();
}}


--
Sobia Awan
  Bs(cs)
 
 

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment