Android隐藏了BaseExpandableListAdapter中的childView

时间:2015-04-15 15:31:21

标签: android android-listview

我在我的应用程序中使用BaseExpandableListAdapter来显示公司名称列表。当您单击公司名称时,应显示第二个公司分支列表。

列表后面的数据结构是一个String数组[公司] [分支]。

由于数组的选择,我必须在使用之前对其进行实例化。每个公司永远不会有超过10个分支,所以我将其声明为arr [companyListLength] [10];

问题在于,当公司的分支少于10个时,子列表中会有空行。

如果行中的数据为空,我已尝试将convertView设置为null。

我也尝试过设置convertView.setVisibility(View.Gone)

但都不起作用。

如果没有数据,如何隐藏子列表中的行?

public class ExpAdapter extends BaseExpandableListAdapter {

      private Context myContext;
      public ExpAdapter(Context context) {
       myContext = context;
      }
      @Override
      public Object getChild(int groupPosition, int childPosition) {
       return null;
      }

      @Override
      public long getChildId(int groupPosition, int childPosition) {
       return 0;
      }

      @Override
      public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {

       if (convertView == null) {
        LayoutInflater inflater =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.child_row, null);
       }

       TextView tvPlayerName = (TextView) convertView.findViewById(R.id.tvPlayerName);
       tvPlayerName.setText(arrChildelements[groupPosition][childPosition]);

       if(arrChildelements[groupPosition][childPosition] == null){

           Log.e(TAG, "child null");
       }else{
         Log.e(TAG, "child not null");
       }

       return convertView;
      }

      @Override
      public int getChildrenCount(int groupPosition) {
       return arrChildelements[groupPosition].length;
      }

      @Override
      public Object getGroup(int groupPosition) {
       return null;
      }

      @Override
      public int getGroupCount() {
       return arrGroupelements.length;
      }

      @Override
      public long getGroupId(int groupPosition) {
       return 0;
      }

      @Override
      public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {

       if (convertView == null) {
        LayoutInflater inflater =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.group_row, null);
       }

       TextView tvGroupName = (TextView) convertView.findViewById(R.id.tvGroupName);
       tvGroupName.setText(arrGroupelements[groupPosition]);

       return convertView;
      }

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

      @Override
      public boolean isChildSelectable(int groupPosition, int childPosition) {
       return true;
      }
     }

1 个答案:

答案 0 :(得分:1)

在getChildrenCount(int groupPosition)中返回非空子项的计数,如:

@Override
public int getChildrenCount(int groupPosition) {
  int count = 0;
  for (int i = 0; i < arrChildelements[groupPosition].length; i++)
    count += arrChildelements[groupPosition][i] != null ? 1 : 0;
  return count;
}