2014-09-29 65 views
1

情況

我有一個多選擇模式ListView,有兩種視圖類型 - 普通和標題。我通過檢索包含名稱和電子郵件的遊標並使用AlphabetIndexer類來加載聯繫人應用程序中的數據。我的適配器擴展了SimpleCursorAdapter類並實現了SectionIndexer。此外,我重寫適配器的getCount()方法,以便它返回遊標的計數+部分的計數。另外,根據用戶操作,我的佈局是正確的,並且項目在onListItemClickListener中突出顯示。setListItemChecked沒有突出顯示所有列表項

問題

但是,我想強調所有與檢查項目的所有按鈕,但在程式碼無法做到這一點。

for (int i = 0; i <= adapter.getCount() - 1; i++) { 
      adapter.getItem(i); 
      if (adapter.getItemViewType(i) == InviteContactListAdapter.TYPE_NORMAL) { 
       listView.setItemChecked(i, true); 
      } 

     } 

它正確地改變與比cursror.getCount()較小的位置,項目的佈局,然後拒絕與一個更大的標記,其餘index.I日誌ListView.getCheckedItemPositions() 這個列表包括所有項目,包括其佈局未被檢查的項目。

Log.d("checkedItems:", listView.getCheckedItemPositions()): 

所以他們的狀態改變了,但不是他們的佈局。

示例

我有一個包含55個聯繫人和20個節標題的列表。當我運行位置0 ... 55的選擇全部按鈕項目得到突出顯示。從56到75的項目只會被檢查,而不會突出顯示。

代碼

public class InviteContactListAdapter extends SimpleCursorAdapter implements 
    SectionIndexer { 

public static final int TYPE_HEADER = 1; 
public static final int TYPE_NORMAL = 0; 
public static final int TYPE_COUNT = 2; 

private AlphabetIndexer indexer; 

private int[] usedSectionNumbers; 
private Map<Integer, Integer> sectionToPosition; 
private Context context; 
private HashMap<Integer, Integer> sectionToOffset; 

public InviteContactListAdapter(Context context, int layout, Cursor c, 
     String[] from, int[] to) { 
    super(context, layout, c, from, to, 0); 

    ArrayList<String> stringCollection = new ArrayList<String>(); 
    Character firstLetter; 
    String firstLetterAsString; 
    while (c.moveToNext()) { 
     firstLetter = c.getString(
       c.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)) 
       .charAt(0); 
     firstLetter = Character.toUpperCase(firstLetter); 
     firstLetterAsString = firstLetter.toString(); 
     if (!stringCollection.contains(firstLetterAsString) 
       && Character.isLetter(firstLetter)) { 
      stringCollection.add(firstLetterAsString); 
     } 
    } 
    Collections.sort(stringCollection); 
    String alphabet = " "; 
    for (String s : stringCollection) { 
     alphabet = alphabet + s; 
    } 
    c.moveToFirst(); 
    Log.d("length", "" + alphabet.length()); 

    this.context = context; 
    indexer = new AlphabetIndexer(c, 
      c.getColumnIndexOrThrow(ContactsContract.Data.DISPLAY_NAME), 
      alphabet); 
    sectionToPosition = new TreeMap<Integer, Integer>(); 
    sectionToOffset = new HashMap<Integer, Integer>(); 

    final int count = super.getCount(); 

    int i; 
    for (i = count - 1; i >= 0; i--) { 
     sectionToPosition.put(indexer.getSectionForPosition(i), i); 
    } 

    i = 0; 
    usedSectionNumbers = new int[sectionToPosition.keySet().size()]; 

    for (Integer section : sectionToPosition.keySet()) { 
     sectionToOffset.put(section, i); 
     usedSectionNumbers[i] = section; 
     i++; 
    } 

    for (Integer section : sectionToPosition.keySet()) { 
     sectionToPosition.put(section, sectionToPosition.get(section) 
       + sectionToOffset.get(section)); 
    } 
    Log.d("", ""); 
} 

@Override 
public int getCount() { 
    if (super.getCount() != 0) { 
     return super.getCount() + usedSectionNumbers.length; 
    } 

    return 0; 
} 

@Override 
public Object getItem(int position) { 
    if (getItemViewType(position) == TYPE_NORMAL) { 
     return super.getItem(position 
       - sectionToOffset.get(getSectionForPosition(position)) - 1); 
    } 
    return null; 
} 

@Override 
public int getPositionForSection(int section) { 
    if (!sectionToOffset.containsKey(section)) { 
     int i = 0; 
     int maxLength = usedSectionNumbers.length; 

     while (i < maxLength && section > usedSectionNumbers[i]) { 
      i++; 
     } 
     if (i == maxLength) 
      return getCount(); 

     return indexer.getPositionForSection(usedSectionNumbers[i]) 
       + sectionToOffset.get(usedSectionNumbers[i]); 
    } 
    return indexer.getPositionForSection(section) 
      + sectionToOffset.get(section); 
} 

@Override 
public int getSectionForPosition(int position) { 
    int i = 0; 
    int maxLength = usedSectionNumbers.length; 
    while (i < maxLength 
      && position >= sectionToPosition.get(usedSectionNumbers[i])) { 
     i++; 
    } 
    return usedSectionNumbers[i - 1]; 
} 

@Override 
public Object[] getSections() { 
    return indexer.getSections(); 
} 

// nothing much to this: headers have positions that the sectionIndexer 
// manages. 
@Override 
public int getItemViewType(int position) { 
    if (position == getPositionForSection(getSectionForPosition(position))) { 
     return TYPE_HEADER; 
    } 
    return TYPE_NORMAL; 
} 

@Override 
public int getViewTypeCount() { 
    return TYPE_COUNT; 
} 

// return the header view, if it's in a section header position 
@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    final int type = getItemViewType(position); 
    LayoutInflater inflater = LayoutInflater.from(context); 
    if (type == TYPE_HEADER) { 
     if (convertView == null) { 
      convertView = inflater.inflate(
        R.layout.list_item_alphabet_section_header, parent, 
        false); 
     } 
     ((TextView) convertView.findViewById(R.id.letter_header)) 
       .setText((String) getSections()[getSectionForPosition(position)]); 

     return convertView; 
    } 
    return super.getView(
      position - sectionToOffset.get(getSectionForPosition(position)) 
        - 1, convertView, parent); 
} 

// these two methods just disable the headers 
@Override 
public boolean areAllItemsEnabled() { 
    return false; 
} 

@Override 
public boolean isEnabled(int position) { 
    if (getItemViewType(position) == TYPE_HEADER) { 
     return false; 
    } 
    return true; 
} 

回答

0

編輯

我認爲這個問題可能是您所呼叫的super方法所有的地方,在我看來意味着,SimpleCursorAdapter的默認行爲有(其中包括突出顯示選項)僅適用於第一個size of the cursor項目。

換句話說,突出顯示所選的行不是默認Adapter的「功能」,而是一些你必須明確地實現。

雖然SimpleCursorAdapter具有高亮顯示「內置」,它只做所以一些項目等於Cursor的大小。

我看不出如何改變這種情況,除非你自己在getView方法中手動管理視圖(即通過更改視圖的背景來自己突出顯示)。

+0

謝謝波格丹,但事實並非如此。我三重檢查了它,這是絕對正確的。我想在基類的某個地方,代碼使用遊標的數量而不是適配器的數量。但是,我找不到它。 – dTod 2014-09-29 07:48:04

+0

@dTod我已閱讀我對此的看法,我希望它現在可以幫助:) – 2014-09-29 12:03:32