ListView滚动显示错误的数据

时间:2013-08-11 20:54:57

标签: android listview android-edittext

    import android.text.Editable;
    import android.text.TextWatcher;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.View.OnClickListener;
    import android.widget.BaseAdapter;
    import android.widget.EditText;
    import android.widget.LinearLayout;
    import android.widget.ListView;
    import android.widget.TextView;

    public class MaterialList extends Activity {
        ListView itemList;
        PEngine engine;
        Context context;
        String Tag = "SOrderFormActivity";
        AppConst appCon;
        TextView txtMyTotal;
        ArrayList <Items> arrItem = new ArrayList<Items>(); 
        double disCountNumber = 0;
        double RowTotal = 0;    
        ViewHolder holder ;
        int position;
        View v;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.material_list_activity);
            itemList = (ListView) findViewById(R.id.listView1);
            engine = new PEngine(this);
            context = this;
            appCon = new AppConst();

            Items item = new Items();
            item.setName("Fan");
            item.setRate(1200);             
            arrItem.add(item);

            item = new Items();
            item.setName("Ceiling Fan");
            item.setRate(2200);     
            arrItem.add(item);

            item= new Items();
            item.setName("Table Fan");
            item.setRate(2780);     
            arrItem.add(item);

            item = new Items();
            item.setName("Ceiling Fans");
            item.setRate(2200);     
            arrItem.add(item);

            item= new Items();
            item.setName("LED Light Bulbs");
            item.setRate(2780);     
            arrItem.add(item);

            item = new Items();
            item.setName("CFL Light Bulbs");
            item.setRate(1400);     
            arrItem.add(item);

            item= new Items();
            item.setName("Halogen Light Bulbs");
            item.setRate(500);      
            arrItem.add(item);

            item = new Items();
            item.setName("Switch Plates");
            item.setRate(800);      
            arrItem.add(item);

            item= new Items();
            item.setName("Outlet Plates");
            item.setRate(950);      
            arrItem.add(item);

            item= new Items();
            item.setName("Combination Plates");
            item.setRate(1500);     
            arrItem.add(item);

            item= new Items();
            item.setName("Blank Plates");
            item.setRate(1050);     
            arrItem.add(item);

            item= new Items();
            item.setName("Solar Power Charger");
            item.setRate(5500);     
            arrItem.add(item);

            item= new Items();
            item.setName("Wind Turbine");
            item.setRate(25800);        
            arrItem.add(item);

            item= new Items();
            item.setName("Dimmers");
            item.setRate(1600);     
            arrItem.add(item);

            item= new Items();
            item.setName("Switches");
            item.setRate(1375);     
            arrItem.add(item);

            item= new Items();
            item.setName("Timers");
            item.setRate(1175);     
            arrItem.add(item);

            item= new Items();
            item.setName("Incandescents Light Bulbs ");
            item.setRate(1200);     
            arrItem.add(item);

            item= new Items();
            item.setName("Motion Sensors");
            item.setRate(2300);     
            arrItem.add(item);

            item= new Items();
            item.setName("Wall Plates");
            item.setRate(250);      
            arrItem.add(item);
            itemList.setAdapter(new UserItemAdapter(MaterialList.this, R.layout.shoplist_rows,arrItem));                
        }

        public class UserItemAdapter extends BaseAdapter {  
            ArrayList<Items> arrItem = new ArrayList<Items>();
            private LayoutInflater mInflater;
            public UserItemAdapter(Context context, int textViewResourceId,ArrayList<Items> arrItem) {          
                this.arrItem = arrItem;         
                mInflater = LayoutInflater.from(context);
            }       

            public int getCount() {         
                return arrItem.size();
            }

            public Object getItem(int position) {       
                return arrItem.get(position);
            }

            public long getItemId(int position) {           
                return position;
            }

            public View getView(int pos, View v1, ViewGroup parent) {
                position = pos;
                v= v1;
                Items item= (Items) getItem(position);
                if (v == null) {
                    /*
                     * LayoutInflater class is used to instantiate layout XML file into its corresponding View objects
                     */             
                    v = mInflater.inflate(R.layout.material_list, parent, false);                       
                    holder = new ViewHolder();
                    holder.txtName=(TextView)v.findViewById(R.id.txtName);
                    holder.txtRate=(TextView)v.findViewById(R.id.txtRate);
                    holder.txtTotAmt=(TextView)v.findViewById(R.id.txtMainTotal);
                    holder.edtQty=(EditText)v.findViewById(R.id.edtQty);
                    holder.txtRowTotal=(TextView)v.findViewById(R.id.txtFinal);
                    holder.txtDis=(TextView)v.findViewById(R.id.txtDiscount);               
                    v.setTag(holder);
                }

                    holder = (ViewHolder)v.getTag();                


                try{
                    holder.txtName.setText("Material Name : "+item.getName());                              
                }catch(Exception e){
                    holder.txtName.setText("");
                    Log.e(Tag,"Itemlist getview "+e);
                }
                try{
                    holder.txtTotAmt.setText("Material Total : "+item.getAmt());                    
                }catch(Exception e){
                    holder.txtTotAmt.setText("");
                    Log.e(Tag,"Itemlist getview "+e);
                }   
                try{
                    holder.txtRate.setText("Material Rate/Unit : "+item.getRate());                                 
                }catch(Exception e){
                    holder.txtRate.setText(""); 
                    Log.e(Tag,"Itemlist getview "+e);
                }                   
                try{
                    holder.txtDis.setText("Discount Offered : "+item.getDiscount());                        
                }catch(Exception e){
                    holder.txtDis.setText("");  
                    Log.e(Tag,"Itemlist getview "+e);
                }                                   


                try{
                    holder.edtQty.setText(""+item.getQty());                                
                }catch(Exception e){
                    holder.edtQty.setText("");  
                    Log.e(Tag,"Itemlist getview "+e);
                }   
                holder.edtQty.addTextChangedListener(new TextWatcher() {

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                    }

                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        Items i = arrItem.get(position);            
                        if(s.length()==0){
                            holder.txtTotAmt.setText("Total : 0 ");
                            holder.txtRowTotal.setText("Total : 0");
                            holder.txtDis.setText("Discount Offered : 0 ");
                            i.setQty(0);
                            i.setRate(Double.valueOf(i.getRate()));
                            i.setAmt(0);
                            i.setName(i.getName());
                            i.setDiscount("0");
                            i.setMainTotal(0);
                            arrItem.set(position, i);
                            v.setTag(holder);
                        }else if(s.length()>0){
                            String snum  =  holder.edtQty.getText().toString();                                 
                            int num  =  Integer.valueOf(snum);                              
                            double discount1  = 0.05;
                            double discount2  = 0.07;
                            double discount3  = 0.08;
                            double rate = Double.valueOf(i.getRate());
                            if (num>0){
                                double total = num * rate;
                                if (total > 4000 && total <= 8500){
                                    disCountNumber = discount1 * total ; 
                                    holder.txtTotAmt.setText("Total : Rs "+Formatter(total));
                                    holder.txtDis.setText("Discount Offered : Rs "+Formatter(disCountNumber));
                                    RowTotal = total - disCountNumber;
                                    holder.txtRowTotal.setText("Grand Total : Rs "+Formatter(RowTotal));    
                                    i.setQty(num);
                                    i.setRate(rate);
                                    i.setAmt(0);
                                    i.setName(i.getName());
                                    i.setDiscount(""+disCountNumber);
                                    i.setMainTotal(RowTotal);
                                    arrItem.set(position, i);
                                    v.setTag(holder);
                                }else if (total > 8500 && total <= 12800){
                                    disCountNumber = discount2 * total ; 
                                    holder.txtTotAmt.setText("Total : Rs "+Formatter(total));
                                    holder.txtDis.setText("Discount Offered : Rs "+Formatter(disCountNumber));
                                    RowTotal = total - disCountNumber;
                                    holder.txtRowTotal.setText("Grand Total : Rs "+Formatter(RowTotal));    
                                    i.setQty(num);
                                    i.setRate(rate);
                                    i.setAmt(0);
                                    i.setDiscount(""+disCountNumber);
                                    i.setMainTotal(RowTotal);
                                    arrItem.set(position, i);
                                    v.setTag(holder);       
                                }else if (total > 12800){                   
                                    disCountNumber = discount3 * total ; 
                                    holder.txtTotAmt.setText("Total : Rs "+Formatter(total));
                                    holder.txtDis.setText("Discount Offered : Rs "+Formatter(disCountNumber));
                                    RowTotal = total - disCountNumber;
                                    holder.txtRowTotal.setText("Grand Total : Rs "+Formatter(RowTotal));        
                                    i.setQty(num);
                                    i.setRate(rate);
                                    i.setAmt(0);
                                    i.setName(i.getName());
                                    i.setDiscount(""+disCountNumber);
                                    i.setMainTotal(RowTotal);
                                    arrItem.set(position, i);
                                    v.setTag(holder);
                                }else {                                     
                                    RowTotal = total ;
                                    holder.txtTotAmt.setText("Total : Rs "+Formatter(total));
                                    holder.txtDis.setText("Discount Offered : Rs "+Formatter(disCountNumber));
                                    RowTotal = total - disCountNumber;
                                    holder.txtRowTotal.setText("Grand Total : Rs "+Formatter(RowTotal));
                                    i.setQty(num);
                                    i.setRate(rate);
                                    i.setAmt(0);
                                    i.setName(i.getName());
                                    i.setDiscount(""+disCountNumber);
                                    i.setMainTotal(RowTotal);
                                    arrItem.set(position, i);
                                    v.setTag(holder);
                                }

                            }else {
                                holder.txtTotAmt.setText("Total : Rs 0");
                                holder.txtDis.setText("Discount Offered : Rs 0");                   
                                holder.txtRowTotal.setText("Grand Total : Rs 0");   
                                i.setQty(0);
                                i.setName(i.getName());
                                i.setRate(Double.valueOf(i.getRate()));
                                i.setAmt(0);
                                i.setDiscount("0");
                                i.setMainTotal(0);
                                arrItem.set(position, i);
                                v.setTag(holder);
                            }               
                        }
                    }
                });
                return v;           
            }
        }

        static class ViewHolder{
            TextView txtName;
            TextView txtRate;   
            TextView txtTotAmt;
            TextView txtRowTotal;
            TextView txtDis;
            EditText edtQty;

        }

        public class TextWat implements TextWatcher{
            int pos;
            View vh; 
            TextWat(int pos,View vh){
                this.pos = pos;
                this.vh = vh;
            }
            @Override
            public void afterTextChanged(Editable s) {
                ViewHolder holder = (ViewHolder) vh.getTag();
                Items i = arrItem.get(pos);         
                if(s.length()==0){
                    holder.txtTotAmt.setText("Total : 0 ");
                    holder.txtRowTotal.setText("Total : 0");
                    holder.txtDis.setText("Discount Offered : 0 ");
                    i.setQty(0);
                    i.setRate(Double.valueOf(i.getRate()));
                    i.setAmt(0);
                    i.setName(i.getName());
                    i.setDiscount("0");
                    i.setMainTotal(0);
                    arrItem.set(pos, i);
                    vh.setTag(holder);
                }else if(s.length()>0){
                    String snum  =  holder.edtQty.getText().toString();                                 
                    int num  =  Integer.valueOf(snum);                              
                    double discount1  = 0.05;
                    double discount2  = 0.07;
                    double discount3  = 0.08;
                    double rate = Double.valueOf(i.getRate());
                    if (num>0){
                        double total = num * rate;
                        if (total > 4000 && total <= 8500){
                            disCountNumber = discount1 * total ; 
                            holder.txtTotAmt.setText("Total : Rs "+Formatter(total));
                            holder.txtDis.setText("Discount Offered : Rs "+Formatter(disCountNumber));
                            RowTotal = total - disCountNumber;
                            holder.txtRowTotal.setText("Grand Total : Rs "+Formatter(RowTotal));    
                            i.setQty(num);
                            i.setRate(rate);
                            i.setAmt(0);
                            i.setName(i.getName());
                            i.setDiscount(""+disCountNumber);
                            i.setMainTotal(RowTotal);
                            arrItem.set(pos, i);
                            vh.setTag(holder);
                        }else if (total > 8500 && total <= 12800){
                            disCountNumber = discount2 * total ; 
                            holder.txtTotAmt.setText("Total : Rs "+Formatter(total));
                            holder.txtDis.setText("Discount Offered : Rs "+Formatter(disCountNumber));
                            RowTotal = total - disCountNumber;
                            holder.txtRowTotal.setText("Grand Total : Rs "+Formatter(RowTotal));    
                            i.setQty(num);
                            i.setRate(rate);
                            i.setAmt(0);
                            i.setDiscount(""+disCountNumber);
                            i.setMainTotal(RowTotal);
                            arrItem.set(pos, i);
                            vh.setTag(holder);                  
                        }else if (total > 12800){                   
                            disCountNumber = discount3 * total ; 
                            holder.txtTotAmt.setText("Total : Rs "+Formatter(total));
                            holder.txtDis.setText("Discount Offered : Rs "+Formatter(disCountNumber));
                            RowTotal = total - disCountNumber;
                            holder.txtRowTotal.setText("Grand Total : Rs "+Formatter(RowTotal));        
                            i.setQty(num);
                            i.setRate(rate);
                            i.setAmt(0);
                            i.setName(i.getName());
                            i.setDiscount(""+disCountNumber);
                            i.setMainTotal(RowTotal);
                            arrItem.set(pos, i);
                            vh.setTag(holder);
                        }else {                                     
                            RowTotal = total ;
                            holder.txtTotAmt.setText("Total : Rs "+Formatter(total));
                            holder.txtDis.setText("Discount Offered : Rs "+Formatter(disCountNumber));
                            RowTotal = total - disCountNumber;
                            holder.txtRowTotal.setText("Grand Total : Rs "+Formatter(RowTotal));
                            i.setQty(num);
                            i.setRate(rate);
                            i.setAmt(0);
                            i.setName(i.getName());
                            i.setDiscount(""+disCountNumber);
                            i.setMainTotal(RowTotal);
                            arrItem.set(pos, i);
                            vh.setTag(holder);
                        }

                    }else {
                        holder.txtTotAmt.setText("Total : Rs 0");
                        holder.txtDis.setText("Discount Offered : Rs 0");                   
                        holder.txtRowTotal.setText("Grand Total : Rs 0");   
                        i.setQty(0);
                        i.setName(i.getName());
                        i.setRate(Double.valueOf(i.getRate()));
                        i.setAmt(0);
                        i.setDiscount("0");
                        i.setMainTotal(0);
                        arrItem.set(pos, i);
                    }               
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                // TODO Auto-generated method stub

            }

        }

        // EditTextWacther  Implementation

        public double Formatter(Double n){
            try{
                String str = String.format("%.1f", n);
                n = Double.valueOf(str);
            }catch(Exception e){
                Log.e(Tag,""+e);
            }
            return n;
        }

        public void btnCreateOrder(View v){

        }

        public void btnViewOrder(View v){

        }

    }

我正在尝试创建一个自定义列表,每当我输入EditText框时,它应该反映在该行的总数中。下面是我的代码,在我的代码中,即使我键入{{1盒子。请帮我修复bug。

2 个答案:

答案 0 :(得分:0)

像这样添加其他块

if (v == null) {           
    v = mInflater.inflate(R.layout.material_list, parent,false);                       
        holder = new ViewHolder();
        holder.txtName=(TextView)v.findViewById(R.id.txtName);
        holder.txtRate=(TextView)v.findViewById(R.id.txtRate);
        holder.txtTotAmt=(TextView)v.findViewById(R.id.txtMainTotal);
        holder.edtQty=(EditText)v.findViewById(R.id.edtQty);
        holder.txtRowTotal=(TextView)v.findViewById(R.id.txtFinal);
        holder.txtDis=(TextView)v.findViewById(R.id.txtDiscount);               
        v.setTag(holder);
}else{
        holder = (ViewHolder)v.getTag();                
      }

答案 1 :(得分:0)