listview在android中滚动很慢

时间:2012-07-28 06:08:25

标签: android listview

我有一个列表视图,其中包含三个不同自定义行的大量通货膨胀。从这些行我必须收集数据并填充数据库并最终获得该数据。现在问题是创建的行数取决于用户它可能高达100。我的列表在滚动时变得非常慢。现在我想知道的是将我的所有列表功能放在异步任务中是有帮助的,这意味着它的创建和执行。下面是我的代码片段。

         private class Async extends AsyncTask<String, Integer, String>{

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            runOnUiThread(new Runnable() {
                 public void run() {
                     lv.setAdapter(new EfficientAdapterAB(Measure_New.this));
                        new EfficientAdapterAB(Measure_New.this).notifyDataSetChanged();
            //stuff that updates ui

                }
            });

            return null;
        }

     }


    private   class EfficientAdapterAB extends BaseAdapter {
            private LayoutInflater mInflater;
            View[] view; 
            ViewHolderAB holderAB;
            /*  RadioButton[] ob=new RadioButton[n];
              RadioButton[] ra=new RadioButton[n];*/

             AndroidOpenDbHelper androidOpenDbHelperObj = new AndroidOpenDbHelper(Measure_New.this);
                SQLiteDatabase sqliteDatabase = androidOpenDbHelperObj.getReadableDatabase();
                String q="SELECT * FROM ab_measurement WHERE job_id=" +"\""+Settings.jobid +"\"";
                Cursor  cursor = sqliteDatabase.rawQuery(q,null);



             public EfficientAdapterAB(Context context) {
             mInflater = LayoutInflater.from(context);


             }

             public int getCount() {
                System.out.println("in getcount value of n is "+n);

                view = new View[n+2];
                return n+2;
             }

             public Object getItem(int position) {

             return position;
             }

             public long getItemId(int position) {

             return position;
             }


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

    final RadioButton[] ob=new RadioButton[n];
    final  RadioButton[] ra=new RadioButton[n];



     holderAB = new ViewHolderAB();
     final EditText edit_a_ft,edit_a_inch,edit_b_ft,edit_b_inch,edit_ab_a_ft,edit_ab_a_inch,edit_ab_b_ft,edit_ab_b_inch;

if (view[position] == null) {
        if(position==0){
             convertView = mInflater.inflate(R.layout.measure_upper_bar, null);
             txt_ft_mtr=(TextView)convertView.findViewById(R.id.textView_ft_mtr);
             txt_inch_cm=(TextView)convertView.findViewById(R.id.textView_inch_cm);
             if(Settings.metric_count==1){
                 txt_ft_mtr.setText(""+"Meter");
                 txt_inch_cm.setText(""+"Centimeter");
             }
             user_input= (EditText) convertView.findViewById(R.id.Num_edittxt_measure_new);
             gettingUserInput();
             new EfficientAdapterAB(Measure_New.this).notifyDataSetChanged();
             System.out.println("after gettinguserInput value of n is "+n);
              feet=(EditText)convertView.findViewById(R.id.Feet_edittxt);
              inch =(EditText)convertView.findViewById(R.id.Inches_Edittxt);

              feet.addTextChangedListener(new TextWatcher() {

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

                }

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

                }

                @Override
                public void afterTextChanged(Editable s) {
                    // TODO Auto-generated method stub
                    try{
                    val_feet_baseline=Integer.parseInt(feet.getText().toString());

                    }catch (Exception e) {
                        // TODO: handle exception
                    }

                }
            });

             inch.addTextChangedListener(new TextWatcher() {

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

                }

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


                }

                @Override
                public void afterTextChanged(Editable s) {
                    // TODO Auto-generated method stub
                    try{

                        val_inch_baseline=Integer.parseInt(inch.getText().toString());

                        if(val_inch_baseline>=ft_mtr){
                            inch_greater=true;
                            ft_baseline=val_feet_baseline+(val_inch_baseline/ft_mtr);
                            val_inch_baseline=val_inch_baseline%ft_mtr;
                            feet.setText(""+ft_baseline);
                            inch.setText(""+val_inch_baseline);
                            System.out.println("yes its working values of feet and inch is "+"feet"+ft_baseline+"inch"+val_inch_baseline);

                        }
                    }
                    catch (Exception e) {
                        // TODO: handle exception
                    }
                }
            });

             if(inch_greater==true){
                 baseline_length=String.valueOf(ft_baseline).concat(".").concat(String.valueOf(val_inch_baseline));
                 feet.setText(""+ft_baseline);
                 System.out.println("in the if part");
                 inch_greater=false;
             }
             else{
             baseline_length=String.valueOf(val_feet_baseline).concat(".").concat(String.valueOf(val_inch_baseline));
             System.out.println("in the else part");
             feet.setText(""+val_feet_baseline);
             }

             inch.setText(""+val_inch_baseline);
                if(val_feet_baseline==0){
                     feet.setText(""+"");
                }

                if(val_inch_baseline==0){

                    inch.setText(""+"");
                }
             user_input.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        user_input.setText("");
                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.showSoftInput(user_input, InputMethodManager.SHOW_IMPLICIT);

                        ((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);



                    }
                });

                rb_A = (RadioButton)convertView.findViewById(R.id.radio_A);
                rb_B = (RadioButton)convertView.findViewById(R.id.radio_B);
                rb_AB = (RadioButton)convertView.findViewById(R.id.radio_A_B);

                rb_A.setOnClickListener(Measure_New.this);
                rb_B.setOnClickListener(Measure_New.this);
                rb_AB.setOnClickListener(Measure_New.this); 




             view[position] = convertView; 

        }

         else if(position==n+1){

                convertView=mInflater.inflate( R.layout.details_continue_btn, null);
                convertView.findViewById(R.id.btn_Continue).setOnClickListener(Measure_New.this);
                convertView.findViewById(R.id.btn_exit_restart).setOnClickListener(Measure_New.this);

                view[position] = convertView;
           }
        else{   


                     if(optn_checkd==1){

                         setDatabase();

                     convertView = mInflater.inflate(R.layout.a_xml, null);
                     txt_A_ft_mtr=(TextView)convertView.findViewById(R.id.textView_A_ft_mtr);
                     txt_A_inch_cm=(TextView)convertView.findViewById(R.id.TextView_A_inch_cm);
                     if(Settings.metric_count ==1){
                         txt_A_ft_mtr.setText(""+"A(m.)");
                         txt_A_inch_cm.setText(""+"A(cm.)");
                     }


                     holderAB.text = (TextView) convertView.findViewById(R.id.num_txt);
                     holderAB.text.setText(""+(position));



                     edit_a_ft=(EditText) convertView.findViewById(R.id.editText_A_ft);
                     edit_a_inch=(EditText) convertView.findViewById(R.id.editText_A_inch);

                     if(position<=n){
                     textChangeListenerForFeet(edit_a_ft,position,al_edit_A_ft);
                     textChangeListenerForInch(edit_a_inch,edit_a_ft,position,al_edit_A_inch);
                     }

                     System.out.println("in else value of position before crashing is"+position+"  "+(position-1)+"  "+ n);
                     if(position<=n){
                     ob[position-1]=(RadioButton)convertView.findViewById(R.id.radio_Obstruction_a);
                     ob[position-1].setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            if(radio__ob_flag[position-1]==true){
                                ob[position-1].setButtonDrawable(R.drawable.redioon);
                                obstruction/*[position-1]*/="Y";

                                radio__ob_flag[position-1]=false;
                            }
                            else{
                                ob[position-1].setButtonDrawable(R.drawable.rediooff);
                                radio__ob_flag[position-1]=true;
                                obstruction/*[position-1]*/="N";

                            }
                             try{
                                    al_ob_a.add(position-1,obstruction/*[position-1]*/);

                                        al_ob_a.remove(position);


                                    }catch(Exception e){

                                    }

                            System.out.println("wooooow here i am ob"+al_ob_a);
                        }
                    });
                     }

                     if(position<=n){
                     ra[position-1]=(RadioButton)convertView.findViewById(R.id.radio_Raisedwall_a);
                     ra[position-1].setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub


                            if(radio__ra_flag[position-1]==true){
                                ra[position-1].setButtonDrawable(R.drawable.redioon);

                                radio__ra_flag[position-1]=false;
                                obstruction/*[position-1]*/="Y";

                            }
                            else{
                                ra[position-1].setButtonDrawable(R.drawable.rediooff);
                                radio__ra_flag[position-1]=true;
                                obstruction/*[position-1]*/="N";

                            }

                             try{
                                    al_ra_a.add(position-1,obstruction/*[position-1]*/);

                                    al_ra_a.remove(position);

                                    }catch(Exception e){

                                    }


                            System.out.println("wooooow here i am ra "+al_ra_a);
                        }
                    });
                     }

                     if(getttingFromDbForMeasure()==true){
                    if(position<=n){

                       retainingInput(al_edit_A_ft,position,edit_a_ft);
                       retainingInput(al_edit_A_inch,position,edit_a_inch);
                       retainingInputForRadio( al_ob_a,position,ob);
                       retainingInputForRadio(al_ra_a ,position,ra);


                    }
                     }

                     }

1 个答案:

答案 0 :(得分:0)

首先,您需要在Yours适配器getView()中使用convertView。这将允许您使用视图持有者。目前您只为viewHolder分配值,但不将其设置为标记或检索值(以避免冗余的findViewById调用)。

结帐this Google I/O video from Romain Guy以及该会话的结帐the code幻灯片。