2013-07-26 24 views
2

我试图创建一个列表视图平滑地滚动用下面的代码:滚动时列表视图崩溃“应用程序可能在其主线程上做了太多工作。”

<ListView 
     android:id="@+id/list2" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:listSelector="@android:color/transparent" /> 

// TT是和x(对象)的阵列,其具有100个对象。

x[] tt= response.getX(); 

ItemsAdapter myListAdapter = new ItemsAdapter(getActivity(),R.layout.mylist, tt); 
// setListAdapter(myListAdapter); 
list2.setAdapter(myListAdapter); 

//适配器列表视图

public class ItemsAdapter extends BaseAdapter { 
    Context myContext; 
    private int customLayoutId = -1; 
    private X[] items; 

    public ItemsAdapter(Context context, int resource, X[] x) { 

     customLayoutId = resource; 
     myContext = context; 
     this.items = x; 

    } 

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

     // View row = convertView; 

     try { 
      LayoutInflater inflator = ((Activity) myContext) 
        .getLayoutInflater(); 

      xdata= items[position]; 

      if (convertView == null) { 
       // row = inflater.inflate(customLayoutId, null); 
       convertView = inflator.inflate(customLayoutId, null); 
       DataViewHolder viewHolder1 = null; 

       viewHolder1 = new DataViewHolder(); 

       viewHolder1.numberTV = (TextView) convertView 
         .findViewById(R.id.numberTV); 
       viewHolder1.costTV = (TextView) convertView 
         .findViewById(R.id.costTV); 
       viewHolder1.reserveButton = (Button) convertView 
         .findViewById(R.id.reserveButton); 
       viewHolder1.reserveButton 
         .setBackgroundResource(R.drawable.reserve_button); 

       viewHolder1.position = position; 

       convertView.setTag(viewHolder1); 
      } 


      DataViewHolder viewHolder1 = (DataViewHolder) convertView 
        .getTag(); 


      viewHolder1.numberTV.setText(xdata.getMsisdn()); 

      viewHolder1.costTV.setText(etrData.getPrice()); 

      viewHolder1.reserveButton 
        .setOnClickListener(new OnClickListener() { 

         @Override 
         public void onClick(View v) { 



        }); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return convertView; 
    } 

} 

    @Override 
    public int getCount() { 

     return this.items.length; 
    } 

    @Override 
    public Object getItem(int position) { 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 
} 

static class DataViewHolder { 
    public TextView numberTV; 
    public TextView costTV; 
    public Button reserveButton; 
    public int position; 
} 

当我滚动这个列表,这里的应用程序崩溃是什么,是在logcat中:

07-27 02:50:26.756: I/Choreographer(24450): Skipped 46 frames! The application may be  doing too much work on its main thread. 
07-27 02:50:27.497: I/Choreographer(24450): Skipped 41 frames! The application may be doing too much work on its main thread. 
07-27 02:50:28.698: I/Choreographer(24450): Skipped 32 frames! The application may be doing too much work on its main thread. 

07-27 02:50:34.724: D/dalvikvm(24450): GC_CONCURRENT freed 1120K, 12% free 13993K/15815K, paused 6ms+6ms, total 35ms 

07-27 02:50:35.655: I/Choreographer(24450): Skipped 41 frames! The application may be doing too much work on its main thread. 
07-27 02:50:45.685: I/Choreographer(24450): Skipped 58 frames! The application may be doing too much work on its main thread. 
07-27 02:50:46.936: I/Choreographer(24450): Skipped 69 frames! The application may be doing too much work on its main thread. 
07-27 02:50:57.677: I/Choreographer(24450): Skipped 31 frames! The application may be doing too much work on its main thread. 
07-27 02:50:59.639: I/Choreographer(24450): Skipped 53 frames! The application may be doing too much work on its main thread. 
07-27 02:51:03.953: D/TextLayoutCache(24450): Cache value 0x54a260f0 deleted, size = 280 
07-27 02:51:03.963: D/TextLayoutCache(24450): Cache value 0x41002fd0 deleted, size = 400 
07-27 02:51:03.963: D/TextLayoutCache(24450): Cache value 0x40f05908 deleted, size = 360 
07-27 02:51:04.113: D/TextLayoutCache(24450): Cache value 0x413aa480 deleted, size = 384 
07-27 02:51:04.123: D/TextLayoutCache(24450): Cache value 0x413a6200 deleted, size = 408 

应该如何这些错误与处理?我究竟做错了什么?

+2

向我们展示您的ItemsAdapter中的getView()方法和logcat中的异常stacktrace。 – SimonSays

回答

2

首先,

我认为这个问题是在getView当您使用

LayoutInflater inflator = ((Activity) myContext).getLayoutInflater(); 

没有必要将它转换为(活动),并使用该layoutInflater。

,当我看到你在你的适配器的custructor传递上下文,你可以只使用

LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

此外,如果你要正确地贯彻ViewHolder百通,你应该做这样的:

 DataViewHolder viewHolder1 = null; 
     if (convertView == null) { 
      // row = inflater.inflate(customLayoutId, null); 
      convertView = inflator.inflate(customLayoutId, null); 
      viewHolder1 = new DataViewHolder(); 
      viewHolder1.numberTV = (TextView) convertView.findViewById(R.id.numberTV); 
      viewHolder1.costTV = (TextView) convertView.findViewById(R.id.costTV); 
      viewHolder1.reserveButton = (Button)convertView.findViewById(R.id.reserveButton);  
      viewHolder1.reserveButton.setBackgroundResource(R.drawable.reserve_button); 
      viewHolder1.position = position; 
      convertView.setTag(viewHolder1); 
     } 
     else{ 
      viewHolder1 = (DataViewHolder) convertView.getTag(); 
     } 

更新:

一般情况下,为了使一个ListView更快,你应该尽量让getView()SI多和快,并与我的意思是,你应该几乎用它来打印你的数据给用户预填充值。

为了做到这一点,您应该正确地实现视图模式,并且 从不访问数据库或从getView中获取互联网中的值。

之后,唯一可能的原因您的问题,我可以在你的代码找到的是如果方法

xdata.getMsisdn() and etrData.getPrice() 

做背景不是让喜欢访问互联网或数据库中的值以外的东西?

+0

我们现在编写视图持有者模式,但其同样的性能列表视图并不顺利,并在滚动此列表2次后立即崩溃应用程序。 –

+0

您是否在使用模拟器来测试您的应用程序或设备? – ManosProm

+0

我使用设备S2 advance –

0

我认为这个问题可能是模拟器中的一个错误。你使用模拟器还是实际设备?我看到这种行为使用Manos描述的模式 - 好的提示! - 带有硬编码字符串的20项数组。

如果我使用我的2.3.3模拟器,也没有问题。如果我使用2.2.3或4.1.1实际设备,则不存在任何问题。

相关问题