Click here to Skip to main content
15,174,182 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my application there is an ordering page . I have a quantity and a price column which are totaled at the end.

Initially, after selecting an items the items are stored in the ArrayList. Using for loop, I added all price values in the ArrayList and the total in a variable x. My problem is if I change the value for qty, the price is getting changed correctly but total value is not changed. Ideally, as soon as I change the qty, the total has to be changed dynamically.
public class CustomAdapter extends BaseAdapter {

    public static Double x = 0.0;
    public static Double z;
    ArrayList<integer> selectprice = new ArrayList<integer>();
    public static ArrayList<string> arr1 = new ArrayList<string>();
    public static ArrayList<string> itemprice = new ArrayList<string>();
    public static ArrayList<bitmap> itemimage = new ArrayList<bitmap>();
    ArrayList<integer> total = new ArrayList<integer>();
    public Context Context;
    private LayoutInflater inflater;

    HashMap<string,> map = new HashMap<string,>();


    public CustomAdapter(Context context, ArrayList<string> arr,
        ArrayList<string> price, ArrayList<bitmap> image) {
        Context = context;
        inflater = LayoutInflater.from(context);
        arr1 = arr;
        itemprice = price;

        itemimage = image;
        System.out.println(itemprice);
        System.out.println("arr: " + arr.size());

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

            x = x + Double.parseDouble(price.get(i));
            //here only I added all prices before changing qty..

        }

    }

    public int getCount() {
        // TODO Auto-generated method stub
        return arr1.size();

    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return arr1.get(position);
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.selecteditemlistview, null);
            holder = new ViewHolder();

            holder.textViewSelectedText = (TextView) convertView.findViewById(R.id.selectedtext);
            holder.price = (TextView) convertView.findViewById(R.id.selectitemprice);
            holder.image = (ImageView) convertView.findViewById(R.id.selectitemimage);
            holder.qty = (EditText) convertView.findViewById(R.id.selectqty);
            holder.total = (TextView) convertView.findViewById(R.id.price);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String amount = holder.qty.getText().toString();
        final Double price1 = Double.parseDouble(itemprice.get(position));
        int qut = Integer.parseInt(holder.qty.getText().toString());
        Double total = (price1 * qut);
        System.out.println(total);
        holder.textViewSelectedText.setText(arr1.get(position));
        holder.price.setText(itemprice.get(position));
        holder.image.setImageBitmap(itemimage.get(position));
        holder.total.setText(String.valueOf(total));

        holder.qty.setOnFocusChangeListener(new OnFocusChangeListener() {

            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub
                if (!hasFocus) {

                    int position = v.getId();
                    final EditText Caption = (EditText) v;
                    Caption.setFocusable(true);
                    holder.qty.setFocusable(true);
                    int q = Integer.parseInt(holder.qty.getText().toString());
                    double result = (price1 * q);
                     Double y = x - total;
                z = y + result;
                     x=z;//here only  I added all total before changing..
                    // x=result+q;
                    // System.out.println(x);
                    System.out.println(result);
                    holder.total.setText(String.valueOf(result));
                    // holder.total.setText(""+array[result].toString()+"");

                }

            }
        });

        return convertView;
    }


public class SelectedItem extends Activity  {
    private ListView lv;
    private CustomAdapter adapter;
    int tableid;here only I added all prices before changing
    String id;
    ArrayList<string> arr=new ArrayList<string>();
    ArrayList<string> price=new ArrayList<string>();
    ArrayList<bitmap> image=new ArrayList<bitmap>();
    Double amountvalue;
    TextView totalamount;
    Button order;
    String dtotalamount;

    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        arr=getIntent().getStringArrayListExtra("itemname");
        price=getIntent().getStringArrayListExtra("itemprice");


        image=getIntent().getExtras().getParcelableArrayList("itemimage");

        setContentView(R.layout.selecteditem);
        lv = (ListView) findViewById(R.id.selecteditemlist);
        adapter = new CustomAdapter(this, arr,price,image);
        lv.setAdapter(adapter);
        amountvalue=CustomAdapter.x;

        System.out.println(amountvalue);
        totalamount=(TextView)findViewById(R.id.amount);
        totalamount.setText(String.valueOf(amountvalue));
        order=(Button)findViewById(R.id.order);
        order.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                tableid=Login.tableid;
                id=Integer.toString(tableid);
                System.out.println(id);
                String x=amountvalue.toString();
                System.out.println(x);
            }
        });
    }
}
Posted
Updated 17-May-12 23:19pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900