当前位置: 首页 > 面试题库 >

Android:具有两个不同视图的EfficientAdapter

陆卓
2023-03-14
问题内容

我正在使用基于SDK演示示例中EfficientAdapter示例的BaseAdapter扩展版本。

我的数据基本上是一个对象(ListPlaces),其中包含一个ArrayList,其中包含实际的地点列表,可通过访问listPlaces.getValues()。此ArrayList数据按范围排序,ArrayList由一些特殊项目(分隔符)组成,没有数据,但separator标志设置为true

现在,每当我EfficientAdapter得到一个作为分隔符的数据对象时,它都会返回并根据当前数据对象是由真实数据组成还是仅仅是分隔符虚拟物false来使两个不同的布局膨胀。public boolean isEnabled(int position)``public View getView(int position, View convertView, ViewGroup parent)

如果我每次都增加布局,效果很好。但是,每次都放大布局并进行调用findViewById会使ListView速度几乎变慢。

因此,我尝试将EfficientAdapter与ViewHolder方法配合使用。但是,由于我尝试访问两种不同的视图,因此无法立即使用。因此,每当我convertView != null(其他情况下)通过我们访问布局上的项目时,ViewHolder并且当前一个View是分隔符时,它当然都无法访问在那里只能在“真实”项目布局上使用的TextView。

因此,getView()不仅在时convertView == null,而且在先前的listRow与当前的listRow
不同时,我还迫使我增加布局:if (convertView == null || (listRow != listRow_previous)) { [....] }

现在,这似乎几乎可以正常工作。或者至少它从一开始就不会崩溃。但是它仍然崩溃,我不知道该怎么做。我已经尝试研究convertView.getID()convertView.getResources(),但是到目前为止这并没有真正的帮助。也许其他人有一个主意,我该如何检查我当前的内容是否convertView与列表项布局或列表分隔符布局匹配。谢谢。

这是代码。daccess-ods.un.org daccess-ods.un.org无论在哪里,我都会删除一些不太重要的代码,以使其更易于阅读和理解:

private class EfficientAdapter extends BaseAdapter {
  private LayoutInflater mInflater;
  private ListPlaces listPlaces;

  private ListRow listRow;
  private ListRow listRow_previous;


  public EfficientAdapter(Context context, ListPlaces listPlaces) {
      // Cache the LayoutInflate to avoid asking for a new one each time.
      mInflater = LayoutInflater.from(context);

      // Data
      this.listPlaces = listPlaces;
  }

  /**
    * The number of items in the list is determined by the number of items
    * in our ArrayList
    *
    * @see android.widget.ListAdapter#getCount()
    */
  public int getCount() {
      return listPlaces.getValues().size();
  }

  /**
    * Since the data comes from an array, just returning the index is
    * sufficent to get at the data. If we were using a more complex data
    * structure, we would return whatever object represents one row in the
    * list.
    *
    * @see android.widget.ListAdapter#getItem(int)
    */
  public Object getItem(int position) {
      return position;
  }

  /**
    * Use the array index as a unique id.
    *
    * @see android.widget.ListAdapter#getItemId(int)
    */
  public long getItemId(int position) {
      return position;
  }

  @Override
  public  boolean isEnabled(int position) {
      // return false if item is a separator:
      if(listPlaces.getValues().get(position).separator >= 0)
          return false;
      else
          return true;
  }

  @Override
  public boolean  areAllItemsEnabled() {
      return false;         
  }



  /**
    * Make a view to hold each row.
    *
    * @see android.widget.ListAdapter#getView(int, android.view.View,
    *      android.view.ViewGroup)
    */
  public View getView(int position, View convertView, ViewGroup parent) {

      // Get the values for the current list element
      ListPlacesValues curValues = listPlaces.getValues().get(position);
      if (curValues.separator >= 0) 
          listRow = ListRow.SEPARATOR;
      else
          listRow = ListRow.ITEM;
      Log.i(TAG,"Adapter: getView("+position+") " + listRow + " (" + listRow_previous + ") -> START");

      // A ViewHolder keeps references to children views to avoid unneccessary calls
      // to findViewById() on each row.
      ViewHolder holder;

      // When convertView is not null, we can reuse it directly, there is no need
      // to reinflate it. We only inflate a new View when the convertView supplied
      // by ListView is null.
      if (convertView == null || (listRow != listRow_previous)) {
          Log.i(TAG, "--> (convertView == null) at position: " + position);
          // Creates a ViewHolder and store references to the two children views
          // we want to bind data to.
          holder = new ViewHolder();

          if (listRow == ListRow.SEPARATOR) {
              convertView = mInflater.inflate(R.layout.taxonomy_list_separator, null);
              holder.separatorText = (TextView) convertView.findViewById(R.id.separatorText);
              convertView.setTag(holder);
              Log.i(TAG,"\tCREATE SEPARATOR: convertView ID: " + convertView.getId() + " Resource: " + convertView.getResources());

          }
          else {

              convertView = mInflater.inflate(R.layout.taxonomy_listitem, null);
              holder.name = (TextView) convertView.findViewById(R.id.name);
              holder.category = (TextView) convertView.findViewById(R.id.category);
              // [...]

              convertView.setTag(holder);

              Log.i(TAG,"\tCREATE ITEM: convertView ID: " + convertView.getId() + " Resource: " + convertView.getResources());
          }
      } else {
          // Get the ViewHolder back to get fast access to the TextView
          // and the ImageView.
          Log.i(TAG,"\tconvertView ID: " + convertView.getId() + " Resource: " + convertView.getResources());

          holder = (ViewHolder) convertView.getTag();
          convertView.setAnimation(null);
      }

      /* Bind the data efficiently with the holder */
      if (listRow == ListRow.SEPARATOR) {
          String separatorText;
          switch (curValues.separator) {
          case 0: separatorText="case 0"; break;
          case 1: separatorText="case 1"; break;
          case 2: separatorText="case 2"; break;
          // [...]
        default: separatorText="[ERROR]"; break;
          }
          holder.separatorText.setText(separatorText);
      } 
      else {
          // Set the name:
          holder.name.setText(curValues.name);
          // Set category
          String cat = curValues.classification.toString();
          cat = cat.substring(1,cat.length()-1);    // removing "[" and "]"
          if (cat.length() > 35) {
              cat = cat.substring(0, 35);
              cat = cat + "...";
          }
          holder.category.setText(cat);

          // [...] (and many more TextViews and ImageViews to be set)

      }

      listRow_previous = listRow;
      Log.i(TAG,"Adapter: getView("+position+") -> DONE");
      return convertView;
  }

  private class ViewHolder {
      TextView name;
      TextView category;
      // [...] -> many more TextViews and ImageViews

      TextView separatorText;
  }
}

这是我的 Logcat 输出:

  755     ListPlaces_Activity  I  onPostExecute: notifyDataSetChanged()                                                                                                
  755     ListPlaces_Activity  I  Adapter: getView(0) SEPARATOR (null) -> START                                                                                        
  755     ListPlaces_Activity  I  --> (convertView == null) at position: 0                                                                                             
  755     ListPlaces_Activity  I        CREATE SEPARATOR: convertView ID: 2131296317 Resource: android.content.res.Resources@437613e0                                  
  755     ListPlaces_Activity  I  Adapter: getView(0) -> DONE                                                                                                          
  755     ListPlaces_Activity  I  Adapter: getView(1) ITEM (SEPARATOR) -> START                                                                                        
  755     ListPlaces_Activity  I  --> (convertView == null) at position: 1                                                                                             
  755     ListPlaces_Activity  I        CREATE ITEM: convertView ID: 2131296317 Resource: android.content.res.Resources@437613e0                                       
  755     ListPlaces_Activity  I  Adapter: getView(1) -> DONE                                                                                                          
  755     ListPlaces_Activity  I  Adapter: getView(2) SEPARATOR (ITEM) -> START                                                                                        
  755     ListPlaces_Activity  I  --> (convertView == null) at position: 2                                                                                             
  755     ListPlaces_Activity  I        CREATE SEPARATOR: convertView ID: 2131296317 Resource: android.content.res.Resources@437613e0                                  
  755     ListPlaces_Activity  I  Adapter: getView(2) -> DONE                                                                                                          
  755     ListPlaces_Activity  I  Adapter: getView(3) ITEM (SEPARATOR) -> START                                                                                        
  755     ListPlaces_Activity  I  --> (convertView == null) at position: 3                                                                                             
  755     ListPlaces_Activity  I        CREATE ITEM: convertView ID: 2131296317 Resource: android.content.res.Resources@437613e0                                       
  755     ListPlaces_Activity  I  Adapter: getView(3) -> DONE                                                                                                          
  755     ListPlaces_Activity  I  Adapter: getView(4) ITEM (ITEM) -> START                                                                                             
  755     ListPlaces_Activity  I        convertView ID: 2131296317 Resource: android.content.res.Resources@437613e0                                                    
  755     ListPlaces_Activity  I  Adapter: getView(4) -> DONE                                                                                                          
  755     ListPlaces_Activity  I  Adapter: getView(5) ITEM (ITEM) -> START                                                                                             
  755     ListPlaces_Activity  I        convertView ID: 2131296317 Resource: android.content.res.Resources@437613e0                                                    
  755     ListPlaces_Activity  I  Adapter: getView(5) -> DONE                                                                                                          
  755     ListPlaces_Activity  I  Adapter: getView(6) ITEM (ITEM) -> START                                                                                             
  755     ListPlaces_Activity  I        convertView ID: 2131296317 Resource: android.content.res.Resources@437613e0                                                    
  755     ListPlaces_Activity  I  Adapter: getView(6) -> DONE                                                                                                          
  755     ListPlaces_Activity  I  Adapter: getView(0) SEPARATOR (ITEM) -> START                                                                                        
  755     ListPlaces_Activity  I  --> (convertView == null) at position: 0                                                                                             
  755     ListPlaces_Activity  I        CREATE SEPARATOR: convertView ID: 2131296317 Resource: android.content.res.Resources@437613e0                                  
  755     ListPlaces_Activity  I  Adapter: getView(0) -> DONE                                                                                                          
  755     ListPlaces_Activity  I  Adapter: getView(1) ITEM (SEPARATOR) -> START                                                                                        
  755     ListPlaces_Activity  I  --> (convertView == null) at position: 1                                                                                             
  755     ListPlaces_Activity  I        CREATE ITEM: convertView ID: 2131296317 Resource: android.content.res.Resources@437613e0                                       
  755     ListPlaces_Activity  I  Adapter: getView(1) -> DONE                                                                                                          
  755     ListPlaces_Activity  I  Adapter: getView(2) SEPARATOR (ITEM) -> START                                                                                        
  755     ListPlaces_Activity  I  --> (convertView == null) at position: 2                                                                                             
  755     ListPlaces_Activity  I        CREATE SEPARATOR: convertView ID: 2131296317 Resource: android.content.res.Resources@437613e0                                  
  755     ListPlaces_Activity  I  Adapter: getView(2) -> DONE                                                                                                          
  755     ListPlaces_Activity  I  Adapter: getView(3) ITEM (SEPARATOR) -> START                                                                                        
  755     ListPlaces_Activity  I  --> (convertView == null) at position: 3                                                                                             
  755     ListPlaces_Activity  I        CREATE ITEM: convertView ID: 2131296317 Resource: android.content.res.Resources@437613e0                                       
  755     ListPlaces_Activity  I  Adapter: getView(3) -> DONE                                                                                                          
  755     ListPlaces_Activity  I  Adapter: getView(4) ITEM (ITEM) -> START                                                                                             
  755     ListPlaces_Activity  I        convertView ID: 2131296317 Resource: android.content.res.Resources@437613e0                                                    
  755     ListPlaces_Activity  I  Adapter: getView(4) -> DONE                                                                                                          
  755     ListPlaces_Activity  I  Adapter: getView(5) ITEM (ITEM) -> START                                                                                             
  755     ListPlaces_Activity  I        convertView ID: 2131296317 Resource: android.content.res.Resources@437613e0                                                    
  755          AndroidRuntime  D  Shutting down VM                                                                                                                     
  755                dalvikvm  W  threadid=3: thread exiting with uncaught exception (group=0x4001aa28)                                                                
  755          AndroidRuntime  E  Uncaught handler: thread main exiting due to uncaught exception                                                                      
  755          AndroidRuntime  E  java.lang.NullPointerException                                                                                                       
  755          AndroidRuntime  E        at com.tato.main.ListPlaces_Activity$EfficientAdapter.getView(ListPlaces_Activity.java:330)                                    
  755          AndroidRuntime  E        at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:191)                                                
  755          AndroidRuntime  E        at android.widget.AbsListView.obtainView(AbsListView.java:1255)                                                                
  755          AndroidRuntime  E        at android.widget.ListView.makeAndAddView(ListView.java:1658)                                                                  
  755          AndroidRuntime  E        at android.widget.ListView.fillDown(ListView.java:637)                                                                         
  755          AndroidRuntime  E        at android.widget.ListView.fillFromTop(ListView.java:694)                                                                      
  755          AndroidRuntime  E        at android.widget.ListView.layoutChildren(ListView.java:1502)                                                                  
  755          AndroidRuntime  E        at android.widget.AbsListView.onLayout(AbsListView.java:1112)                                                                  
  755          AndroidRuntime  E        at android.view.View.layout(View.java:6569)                                                                                    
  755          AndroidRuntime  E        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)                                                           
  755          AndroidRuntime  E        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)                                                           
  755          AndroidRuntime  E        at android.widget.LinearLayout.onLayout(LinearLayout.java:918)                                                                 
  755          AndroidRuntime  E        at android.view.View.layout(View.java:6569)                                                                                    
  755          AndroidRuntime  E        at android.widget.FrameLayout.onLayout(FrameLayout.java:333)                                                                   
  755          AndroidRuntime  E        at android.view.View.layout(View.java:6569)                                                                                    
  755          AndroidRuntime  E        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)                                                           
  755          AndroidRuntime  E        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)                                                           
  755          AndroidRuntime  E        at android.widget.LinearLayout.onLayout(LinearLayout.java:918)                                                                 
  755          AndroidRuntime  E        at android.view.View.layout(View.java:6569)                                                                                    
  755          AndroidRuntime  E        at android.widget.FrameLayout.onLayout(FrameLayout.java:333)                                                                   
  755          AndroidRuntime  E        at android.view.View.layout(View.java:6569)                                                                                    
  755          AndroidRuntime  E        at android.view.ViewRoot.performTraversals(ViewRoot.java:979)                                                                  
  755          AndroidRuntime  E        at android.view.ViewRoot.handleMessage(ViewRoot.java:1613)                                                                     
  755          AndroidRuntime  E        at android.os.Handler.dispatchMessage(Handler.java:99)                                                                         
  755          AndroidRuntime  E        at android.os.Looper.loop(Looper.java:123)                                                                                     
  755          AndroidRuntime  E        at android.app.ActivityThread.main(ActivityThread.java:4203)                                                                   
  755          AndroidRuntime  E        at java.lang.reflect.Method.invokeNative(Native Method)                                                                        
  755          AndroidRuntime  E        at java.lang.reflect.Method.invoke(Method.java:521)                                                                            
  755          AndroidRuntime  E        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)                                             
  755          AndroidRuntime  E        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)                                                                
  755          AndroidRuntime  E        at dalvik.system.NativeStart.main(Native Method)

问题答案:

您忘记了需要重写的几个方法:getViewTypeCount()和getItemViewType()。所有行都相同的列表不需要这些,但是它们对于您的方案非常重要。正确实现这些功能,Android将为标题和明细行维护单独的对象池。

或者,您可以看一下:

  • Jeff Sharkey的原始SeparatedListAdapter(GPL)
  • 我的更新演绎(GPL)
  • 我的MergeAdapter,也可以用于这种情况(Apache)


 类似资料:
  • 问题内容: 为TextView和ImageView设置相同的ID是否正确?由于它们属于一个实体,因此我给了他们两个相同的ID。如果是..那我怎么能分别通过id找到这些视图呢? 问题答案: 为TextView和ImageView设置相同的ID是否正确? 简短答案: 否 长答案:使用相同的ID 是 不 正确的,因为这样做可能会导致运行时错误。考虑下面的例子 layout.xml LocationAct

  • 问题内容: 为TextView和ImageView设置相同的ID是否正确?由于它们属于一个实体,因此我给了他们两个相同的ID。如果是..那我怎么能分别通过id找到这些视图呢? 问题答案: 为TextView和ImageView设置相同的ID是否正确? 简短答案: 否 长答案:使用相同的ID 是 不 正确的,因为这样做可能会导致运行时错误。考虑下面的例子 layout.xml LocationAct

  • 谁能帮我创建一个视图,连接两个没有重复的相同列的表? 例: 我有两张桌子 T1 和 T2 一种网络的名称(传输率可达1.54mbps) T2 航站楼 我的输出视图应该如下所示 下面是我正在尝试的查询 但是我得到了重复的记录。

  • 问题内容: 我有两个表(表A和表B)。 它们具有不同的列数-假设表A具有更多列。 如何合并这两个表,并为表B没有的列获取空值? 问题答案: 为具有较少列的表添加额外的列作为null

  • 在我们的应用程序中,我们使用RabbitMQ和spring-amqp(1.4.3.发行版)。我们那里排了两个队。它们都配置了TTL(60000和100000)。当我们启动应用程序时,它会给出以下错误: [pool-4-thread-1]错误org.springFramework.amqp.rabbit.connection.cachingConnectionFactory-通道关闭:通道错误;协议

  • 问题内容: 我必须在Android中构建更复杂的自定义视图。最终布局应如下所示: 但是,我只想在XML文件中定义它(不定义SomeView,SomeOtherView等): 这在Android中可行吗?如果可以,那么最干净的方法是什么?我想到的可能解决方案是“重写addView()方法”和“删除所有视图并在以后再次添加它们”,但是我不确定该走哪条路… 在此先感谢您的帮助!:) 问题答案: 绝对有可