【问题标题】:Android ListView ArrayAdapter data updates Best PracticesAndroid ListView ArrayAdapter 数据更新最佳实践
【发布时间】:2014-04-28 19:42:47
【问题描述】:

我有一个包含多个列表视图的活动,这些视图不断从套接字线程接收新值,另一个线程解析数据并更新数组适配器,然后 ui 线程调用 notifyDataSetChanged() 以使列表刷新。

我的问题是我每秒刷新所有列表几次,这会导致 UI 在某些动画需要发生时非常滞后。

我想知道最好的方法是每秒更新多个值更改的多个列表?

谢谢, 图腾。

【问题讨论】:

    标签: android listview adapter


    【解决方案1】:

    我肯定会遵循他们今年在 Google IO 上提供的指导方针。

    Google IO listview Video

    【讨论】:

      【解决方案2】:

      您应该使用游标(如果需要内容提供程序)和 ListActivity。 UI 会在发生更改时自动更新,如果数据集为空,列表会自动显示相关视图。

      以下示例使用内容提供者解决它:

      main.xml:

      <ListView android:id="@id/android:list" android:layout_width="fill_parent"
              android:layout_height="wrap_content"></ListView>
          <TextView android:id="@id/android:empty" android:layout_width="fill_parent"
              android:gravity="center" android:layout_height="wrap_content"
              android:text="No data, please refresh!" />
      

      注意 android:list 和 android:empty 标签。这些是列表活动所必需的。

      在 onCreate() 方法中:

      mCursor = getContentResolver().query(SOME_URI,
                      null, null, null, null);
      
              ListView mListView = (ListView) findViewById(android.R.id.list);
              mListView.setAdapter(new CustomCusorAdapter(getApplicationContext(),
                      mCursor));
      

      如果您的视图是直截了当的,您可以使用 SimpleCursorAdapter。由于视图复杂,我由自己的适配器创建:

      private class CustomCusorAdapter extends CursorAdapter {
      
              public CustomCusorAdapter(Context context, Cursor c) {
                  super(context, c);
              }
      
              @Override
              public void bindView(View view, Context context, Cursor cursor) {
      
      
                  Holder holder = (Holder) view.getTag();
      
                  holder.tv.setText(cursor.getString(cursor
                          .getColumnIndex(COL1_NAME)));
      
      
              }
      
              @Override
              public View newView(Context context, Cursor cursor, ViewGroup parent) {
                  View v = getLayoutInflater().inflate(
                          R.layout.layout_xml, null);
      
                  Holder holder = new Holder();
      
                  holder.tv = (TextView) v
                          .findViewById(R.id.tv);
                  holder.cb= (CheckBox) v
                          .findViewById(R.id.cb);
      
                  v.setTag(holder);
                  return v;
              }
      
          }
      
          private class Holder {
              TextView tv;
              CheckBox cb;
          }
      

      【讨论】:

        【解决方案3】:

        是的,该视频非常有帮助。最大的收获之一是您应该回收传递给列表适配器 getView 方法的 convertView:

            public View getView(int position, View convertView, ViewGroup parent){
                if (convertView == null) {
                    convertView = mInflator.inflate(resource, parent, false);
                } 
                //do any view bindings for the row
                return convertView;
            }
        

        另一个有用的地方是使用 ViewHolder 类进行视图回收。视频开始时间约为 10:30。

        【讨论】:

          猜你喜欢
          • 2023-03-13
          • 1970-01-01
          • 2012-09-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-10-11
          • 1970-01-01
          相关资源
          最近更新 更多