2012-07-28 107 views
-1

我有一個listview與三個不同的自定義行的重度通貨膨脹。從這些行我必須收集數據並填充數據庫,並最終獲得該數據也。現在的問題是,數創建的行取決於用戶它可能高達100.而我的列表變得非常緩慢的滾動。現在我想知道的是它將有助於把我所有的列表運行在Async任務我的意思是它的創建以及它的執行。下面是我的代碼片段。listview在android中滾動很慢

  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); 


        } 
        } 

        } 
+0

setAdapter()不需要在線程中,其適配器類中的getView()需要時間填充列表 – 2012-07-28 06:50:35

+0

上的視圖,那麼在這種情況下我還能做些什麼來使用異步任務? – Rahulkapil 2012-07-28 06:56:57

回答

0

首先,您需要在您的適配器getView()中使用convertView。這會讓你使用視圖持有者。目前,您只將值分配給viewHolder,但不會將其設置爲標記或檢索值(以避免多餘的findViewById調用)。

結帳this Google I/O video from Romain Guy並且還從該會話幻燈片中籤出the code