2013-02-27 77 views
10

我認爲這是一個專家的問題。Android ListView數組索引超出界限後過濾器

我得到調用getView()現在的位置出從的ListView數據列表範圍的。
當我使用適配器過濾器時會發生這種情況。過濾器publishResults()方法使用小於原始列表的過濾列表填充數據。
當新的過濾列表比先前的過濾列表短時,似乎會出現該錯誤。 我改變了getView()的代碼,以便返回一個虛擬convertView超出界限時,只是爲了查看發出了多少次這樣的調用。

這些都是相關的代碼和log消息我記錄:

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // No logs here to keep ListView performance good 
     Log.d(TAG, "+ getView(position=" + position + ")"); 
     ViewHolder holder; 

     if(position >= mData.size()) { 
      // This code allows to see how many bad calls I get 
      Log.w(TAG, "position out of bounds!"); 
      convertView = mInflater.inflate(mLayout, parent, false); 
      return convertView; 
     } 

     . . . // Normal getView code 

     return convertView; 
    } 

在所述過濾器(代碼複製如從ArrayAdapter源代碼)

 @Override 
     protected void publishResults(CharSequence constraint, FilterResults results) { 
      Log.pe(TAG, "+ publishResults(constraint:" + constraint + ", results.count:" + results.count + ")"); 
      //noinspection unchecked 
      mData = (ArrayList<String>) results.values; 
      if (results.count > 0) { 
       notifyDataSetChanged(); 
      } else { 
       notifyDataSetInvalidated(); 
      } 
      Log.px(TAG, "- publishResults()"); 
     } 

的日誌文件顯示後一過濾器有7個結果,有3個結果的配件商,但getView不斷接到7個項目的呼叫(我標有***的出界呼叫):

02-26 05:31:55.986: D/ViewerActivity(22857): + onQueryTextChange(newText:log) 
02-26 05:31:55.986: D/ViewerActivity(22857): - onQueryTextChange() 
02-26 05:31:56.029: D/LogScreenAdapter(22857): + performFiltering(prefix:log) 
02-26 05:31:56.113: D/dalvikvm(22857): GC_CONCURRENT freed 378K, 5% free 13577K/14215K, paused 0ms+1ms 
02-26 05:31:56.153: D/LogScreenAdapter(22857): - performFiltering() 
02-26 05:31:56.153: D/LogScreenAdapter(22857): + publishResults(constraint:log, results.count:7) 
02-26 05:31:56.167: D/LogScreenAdapter(22857): - publishResults() 
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView(position=0) 
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView(position=0) 
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView(position=0) 
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView(position=1) 
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView(position=2) 
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView(position=3) 
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView(position=4) 
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView(position=5) 
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView(position=6) 
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView(position=0) 
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView(position=1) 
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView(position=2) 
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView(position=3) 
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView(position=4) 
02-26 05:31:56.493: D/LogScreenAdapter(22857): + getView(position=5) 
02-26 05:31:56.503: D/LogScreenAdapter(22857): + getView(position=6) 
02-26 05:32:23.793: D/ViewerActivity(22857): + onQueryTextChange(newText:logs) 
02-26 05:32:23.793: D/ViewerActivity(22857): - onQueryTextChange() 
02-26 05:32:23.813: D/LogScreenAdapter(22857): + performFiltering(prefix:logs) 
02-26 05:32:23.854: D/dalvikvm(22857): GC_CONCURRENT freed 383K, 5% free 13577K/14215K, paused 0ms+0ms 
02-26 05:32:23.924: D/dalvikvm(22857): GC_CONCURRENT freed 388K, 5% free 13573K/14215K, paused 0ms+1ms 
02-26 05:32:23.974: D/LogScreenAdapter(22857): - performFiltering() 
02-26 05:32:23.983: D/LogScreenAdapter(22857): + publishResults(constraint:logs, results.count:3) 
02-26 05:32:23.983: D/LogScreenAdapter(22857): - publishResults() 
02-26 05:32:23.983: D/LogScreenAdapter(22857): + getView(position=0) 
02-26 05:32:24.074: D/LogScreenAdapter(22857): + getView(position=0) 
02-26 05:32:24.093: D/LogScreenAdapter(22857): + getView(position=0) 
02-26 05:32:24.113: D/LogScreenAdapter(22857): + getView(position=1) 
02-26 05:32:24.155: D/LogScreenAdapter(22857): + getView(position=2) 
02-26 05:32:24.164: D/LogScreenAdapter(22857): + getView(position=3) 
*** 02-26 05:32:24.193: W/LogScreenAdapter(22857): position out of bounds! 
02-26 05:32:24.233: D/LogScreenAdapter(22857): + getView(position=4) 
*** 02-26 05:32:24.263: W/LogScreenAdapter(22857): position out of bounds! 
02-26 05:32:24.284: D/LogScreenAdapter(22857): + getView(position=5) 
*** 02-26 05:32:24.313: W/LogScreenAdapter(22857): position out of bounds! 
02-26 05:32:24.333: D/LogScreenAdapter(22857): + getView(position=6) 
*** 02-26 05:32:24.343: W/LogScreenAdapter(22857): position out of bounds! 
02-26 05:32:24.353: D/LogScreenAdapter(22857): + getView(position=0) 
02-26 05:32:24.373: D/LogScreenAdapter(22857): + getView(position=1) 
02-26 05:32:24.383: D/LogScreenAdapter(22857): + getView(position=2) 
02-26 05:32:24.403: D/LogScreenAdapter(22857): + getView(position=3) 
*** 02-26 05:32:24.413: W/LogScreenAdapter(22857): position out of bounds! 
02-26 05:32:24.433: D/LogScreenAdapter(22857): + getView(position=4) 
*** 02-26 05:32:24.443: W/LogScreenAdapter(22857): position out of bounds! 
02-26 05:32:24.463: D/LogScreenAdapter(22857): + getView(position=5) 
*** 02-26 05:32:24.475: W/LogScreenAdapter(22857): position out of bounds! 
02-26 05:32:24.483: D/LogScreenAdapter(22857): + getView(position=6) 
*** 02-26 05:32:24.503: W/LogScreenAdapter(22857): position out of bounds! 
02-26 05:38:26.769: D/dalvikvm(22857): GC_CONCURRENT freed 316K, 5% free 13640K/14215K, paused 0ms+1ms 

你在這裏看到的是什麼,該publishResults()方法確實從7項至3項的短名單列表改變MDATA,看到上面的代碼,但Adapter狀態越來越getView()呼籲在7個項目清單,甚至它不再存在。
請注意notifyDataSetChanged()已被調用新的數據分配,所以ListView應該知道新的列表。

+1

getItemCount函數是否被調用,如果是,它返回什麼?如果你重寫它,請發佈代碼。 – 2013-02-27 05:44:18

+1

getItemCount返回什麼? – alex 2013-02-27 05:44:25

+0

我不重寫getItemCount,應該嗎? – ilomambo 2013-02-27 05:46:34

回答

20

你這是在自定義列表視圖適配器public int getCount()方法返回?

你應該返回像mData != null? mData.size() : 0

位置進行債券越來越因可能是你正在返回列表以上數據顯示在列表的大小

getCount()自定義列表適配器的方法指定大小列表視圖,因此它應該是你逝去的列表

18

好像「getCount將()」方法的重載將解決你的問題:

@Override 
public int getCount() { 
    return mData.size(); 
} 
+0

非常感謝... +1爲此 – Noman 2013-05-26 08:20:27

+0

+1,正是需要的。 – Mahm00d 2014-02-09 08:18:54

+0

這個陳述存在但是同樣的問題 – Prasad 2015-05-23 17:42:53

0

我沒有這個數據的大小,它解決了我的問題

@Override 
public int getCount() { 

    if (isFiltered == true) { 
     return myFilteredArray.size(); 
    } 

    return myUnfilteredArray.size(); 
}