2014-12-19 55 views
0

我已经设置了一个列表视图我从网上获取一些数据,然后调用我的方法handleData来使用数据并将其放入我的列表视图中,但即使我已经检索到数据并且handleData被称为I getView不被调用。这里是我的代码,我已经把一堆日志语句看到它有多远。ListView GetView不叫

private void handleData(LinkedHashMap<String, List<String>> data) { 

     Log.i(TAG, "handle data called"); 


     ArrayList<String> myKeys = new ArrayList<>(data.keySet()); 
     String[] desiredOrder = {"Entrée", "Entree"}; 

     int putPosition = 0; 

     for (String currentString : desiredOrder) { 
      for (int j = 0; j < myKeys.size(); j++) { 
       if (myKeys.get(j).equals(currentString)) { 
        myKeys.remove(j); 
        myKeys.add(putPosition, currentString); 
        putPosition++; 
       } 

      } 
     } 

     LinkedHashMap<String, List<String>> linkedHashMap = new LinkedHashMap<String, List<String>>(); 


     for (int i = 0 ; i < myKeys.size() ; i++) { 
      linkedHashMap.put(myKeys.get(i), data.get(myKeys.get(i))); 

     } 


     mLinkedHashMap = linkedHashMap; 

     Log.i(TAG, mLinkedHashMap + ""); 


     mHeaderListView = (HeaderListView)findViewById(R.id.HeaderListView_MainActivity); 

     mHeaderListView.setAdapter(new SectionAdapter() { 


      @Override 
      public int numberOfSections() { 

       return mLinkedHashMap.keySet().toArray().length; 
      } 

      @Override 
      public int numberOfRows(int section) { 

       if(section >=0){ 
        String sectionKey = (String)mLinkedHashMap.keySet().toArray()[section]; 
        int numOfRows = mLinkedHashMap.get(sectionKey).size(); 
        return numOfRows; 
       }else{ 
        return 0; 
       } 
      } 

      @Override 
      public boolean hasSectionHeaderView(int section) { 
       return true; 
      } 

      @Override 
      public View getRowView(int section, int row, View convertView, ViewGroup parent) { 

       Log.i(TAG, "GOT HERE"); 

       ViewHolder holder = null; 
       if (convertView == null) { 
        holder = new ViewHolder(); 
        LayoutInflater mLayoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        convertView = mLayoutInflater.inflate(R.layout.item_cell_view, parent, false); 
        holder.textView = (TextView)convertView.findViewById(R.id.text); 
        convertView.setTag(holder); 
       } else { 
        holder = (ViewHolder)convertView.getTag(); 
       } 
       String sectionKey = (String)mLinkedHashMap.keySet().toArray()[section]; 
       holder.textView.setText(mLinkedHashMap.get(sectionKey).get(row)); 
       return convertView; 

      } 

      @Override 
      public Object getRowItem(int section, int row) { 

//    return ((String[])mLinkedHashMap.keySet().toArray()[section])[row]; 

       return mLinkedHashMap.keySet().toArray()[section]; 

      } 

      @Override 
      public View getSectionHeaderView(int section, View convertView, 
              ViewGroup parent) { 
       ViewHolder holder = null; 
       LayoutInflater mLayoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       if (convertView == null) { 
        holder = new ViewHolder(); 
        convertView = mLayoutInflater.inflate(R.layout.header_cell_view, parent, false); 
        holder.textView = (TextView)convertView.findViewById(R.id.textSeparator); 
        convertView.setBackgroundColor((colorBar)); 


        convertView.setTag(holder); 
       } else { 
        holder = (ViewHolder)convertView.getTag(); 
       } 
       holder.textView.setText((String)(mLinkedHashMap.keySet().toArray()[section])); 

       return convertView; 
      } 

      @Override 
      public void onRowItemClick(AdapterView<?> parent, View view, int section, int row, long id) { 
       super.onRowItemClick(parent, view, section, row, id); 

       String sectionName = getRowItem(section, row).toString(); 
       String itemText = mLinkedHashMap.get(sectionName).get(row); 

       Toast.makeText(DiningItemsActivity.this, "Section " + section + " Row " + row + "Name: " + itemText, 
         Toast.LENGTH_SHORT).show(); 
      } 
     }); 


    } 

    public static class ViewHolder { 
     public TextView textView; 
    } 

而我没有看到GOT HERE,但我看到我的数据填充数据,处理数据不被调用?

不知道为什么发生这种情况我有这一切在TabHost,如果我把它像这样

myTabHost.setCurrentTab(1); 

或本

myTabHost.setCurrentTab(2); 

我看到我的数据,但是当它不存在或设置为此

myTabHost.setCurrentTab(0); 

我看不到数据我和GOT这里没有叫我很困惑。

下面是部分适配器

public abstract class SectionAdapter extends BaseAdapter implements OnItemClickListener { 

private int mCount = -1; 

public abstract int numberOfSections(); 

public abstract int numberOfRows(int section); 

public abstract View getRowView(int section, int row, View convertView, ViewGroup parent); 

public abstract Object getRowItem(int section, int row); 

public boolean hasSectionHeaderView(int section) { 
    return false; 
} 

public View getSectionHeaderView(int section, View convertView, ViewGroup parent) { 
    return null; 
} 

public Object getSectionHeaderItem(int section) { 
    return null; 
} 

public int getRowViewTypeCount() { 
    return 1; 
} 

public int getSectionHeaderViewTypeCount() { 
    return 1; 
} 

/** 
* Must return a value between 0 and getRowViewTypeCount() (excluded) 
*/ 
public int getRowItemViewType(int section, int row) { 
    return 0; 
} 

/** 
* Must return a value between 0 and getSectionHeaderViewTypeCount() (excluded, if > 0) 
*/ 
public int getSectionHeaderItemViewType(int section) { 
    return 0; 
} 

@Override 
/** 
* Dispatched to call onRowItemClick 
*/ 
public final void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    onRowItemClick(parent, view, getSection(position), getRowInSection(position), id); 
} 

public void onRowItemClick(AdapterView<?> parent, View view, int section, int row, long id) { 

} 

@Override 
/** 
* Counts the amount of cells = headers + rows 
*/ 
public final int getCount() { 
    if (mCount < 0) { 
     mCount = numberOfCellsBeforeSection(numberOfSections()); 
    } 
    return mCount; 
} 

@Override 
public boolean isEmpty() { 
    return getCount() == 0; 
} 

@Override 
/** 
* Dispatched to call getRowItem or getSectionHeaderItem 
*/ 
public final Object getItem(int position) { 
    int section = getSection(position); 
    if (isSectionHeader(position)) { 
     if (hasSectionHeaderView(section)) { 
      return getSectionHeaderItem(section); 
     } 
     return null; 
    } 
    return getRowItem(section, getRowInSection(position)); 
} 

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

@Override 
/** 
* Dispatched to call getRowView or getSectionHeaderView 
*/ 
public final View getView(int position, View convertView, ViewGroup parent) { 
    int section = getSection(position); 
    if (isSectionHeader(position)) { 
     if (hasSectionHeaderView(section)) { 
      return getSectionHeaderView(section, convertView, parent); 
     } 
     return null; 
    } 
    return getRowView(section, getRowInSection(position), convertView, parent); 
} 

/** 
* Returns the section number of the indicated cell 
*/ 
protected int getSection(int position) { 
    int section = 0; 
    int cellCounter = 0; 
    while (cellCounter <= position && section <= numberOfSections()) { 
     cellCounter += numberOfCellsInSection(section); 
     section++; 
    } 
    return section - 1; 
} 

/** 
* Returns the row index of the indicated cell Should not be call with 
* positions directing to section headers 
*/ 
protected int getRowInSection(int position) { 
    int section = getSection(position); 
    int row = position - numberOfCellsBeforeSection(section); 
    if (hasSectionHeaderView(section)) { 
     return row - 1; 
    } else { 
     return row; 
    } 
} 

/** 
* Returns true if the cell at this index is a section header 
*/ 
protected boolean isSectionHeader(int position) { 
    int section = getSection(position); 
    return hasSectionHeaderView(section) && numberOfCellsBeforeSection(section) == position; 
} 

/** 
* Returns the number of cells (= headers + rows) before the indicated 
* section 
*/ 
protected int numberOfCellsBeforeSection(int section) { 
    int count = 0; 
    for (int i = 0; i < Math.min(numberOfSections(), section); i++) { 
     count += numberOfCellsInSection(i); 
    } 
    return count; 
} 

private int numberOfCellsInSection(int section) { 
    return numberOfRows(section) + (hasSectionHeaderView(section) ? 1 : 0); 
} 

@Override 
public void notifyDataSetChanged() { 
    mCount = numberOfCellsBeforeSection(numberOfSections()); 
    super.notifyDataSetChanged(); 
} 

@Override 
public void notifyDataSetInvalidated() { 
    mCount = numberOfCellsBeforeSection(numberOfSections()); 
    super.notifyDataSetInvalidated(); 
} 

@Override 
/** 
* Dispatched to call getRowItemViewType or getSectionHeaderItemViewType 
*/ 
public final int getItemViewType(int position) { 
    int section = getSection(position); 
    if (isSectionHeader(position)) { 
     return getRowViewTypeCount() + getSectionHeaderItemViewType(section); 
    } else { 
     return getRowItemViewType(section, getRowInSection(position)); 
    } 
} 

@Override 
/** 
* Dispatched to call getRowViewTypeCount and getSectionHeaderViewTypeCount 
*/ 
public final int getViewTypeCount() { 
    return getRowViewTypeCount() + getSectionHeaderViewTypeCount(); 
} 

@Override 
/** 
* By default, disables section headers 
*/ 
public boolean isEnabled(int position) { 
    return (disableHeaders() || !isSectionHeader(position)) && isRowEnabled(getSection(position), getRowInSection(position)); 
} 

public boolean disableHeaders() { 
    return false; 
} 

public boolean isRowEnabled(int section, int row) { 
    return true; 
} 

}

感谢,然后提前帮助。

+0

添加SectionAdapter类的代码。 – 2014-12-19 07:22:46

+0

@PankajKumar请参阅编辑 – iqueqiorio 2014-12-19 07:24:27

+0

将日志添加到'getCount()'以确保此功能正常工作。如果它返回正确的项目数,则表示意思。我认为它在任何情况下都返回0。 – 2014-12-19 07:32:14

回答

0

如果您重新定义了getView方法是这样的:

@Override 
/** 
* Dispatched to call getRowView or getSectionHeaderView 
*/ 
public final View getView(int position, View convertView, ViewGroup parent) { 
    Log.i(TAG, "GETVIEW \o/"); 
    int section = getSection(position); 
    if (isSectionHeader(position)) { 
     if (hasSectionHeaderView(section)) { 
      return getSectionHeaderView(section, convertView, parent); 
     } 
     return null; 
    } 
    return getRowView(section, getRowInSection(position), convertView, parent); 
} 

getRowView应该叫。

+0

当我添加此我得到编译错误'getView无法重写段适配器getView' – iqueqiorio 2014-12-19 22:32:18

+0

oups我没有看到getView是在SectionAdapter中定义的。这是最后的方法。 – Groco 2014-12-20 00:31:14

+0

我认为这个问题可能与我的标签主机,如果你不介意看看这个问题,我会在12小时后发布赏金时间http://stackoverflow.com/questions/27559989/listview -with三个选项上,同样的活动 – iqueqiorio 2014-12-20 06:10:26

0

当我在片段中使用listView时,遇到过这种情况。

确保您的片段是完全相同的片段。这意味着片段堆栈应该始终是相同的 - 没有新的片段。

可以使用FragmentTransation.hide().show().commit()切换片段

如果你不这样做,新的片段将阻止getView()。

但是,对不起,我不知道为什么...