我已经创建了一个自定义的ListView,可以生成正确的数据,但是当我尝试通过名称搜索特定项时,它返回正确的项,但1秒后由于IndexOutOfBoundException而崩溃,我不太确定为什么,因为我已经将所有数组初始化为正确的大小。
BossAdapter.java Boss.java
ListView listView;
int[] boss_icon = {R.drawable.boss_1, R.drawable.boss_2, R.drawable.boss_3,
R.drawable.boss_4, R.drawable.boss_5, R.drawable.boss_6, R.drawable.boss_7,
R.drawable.boss_8, R.drawable.boss_9, R.drawable.boss_10, R.drawable.boss_11,
R.drawable.boss_12, R.drawable.boss_13, R.drawable.boss_14, R.drawable.boss_15,
R.drawable.boss_16, R.drawable.boss_17, R.drawable.boss_18};
String[] boss_title= new String[18];
BossAdapter adapter;
String[] bossTime= new String[18];
Handler handler;
String[] bossAppearance= new String[18];
EditText search_view;
ArrayList<Boss> bossList;
int bossID;
private int getID(String name){
int id = 0;
switch(name){
case "다크 지란트":
id = 0;
return id;
case "머쉬맘":
id = 1;
return id;
case "MARK52 ALPHA":
id = 2;
return id;
case "깡패 바라하":
id = 3;
return id;
case "데블린 워리어":
id = 4;
return id;
case "닉시":
id = 5;
return id;
case "에피":
id = 6;
return id;
case "자이언트 라바아이":
id = 7;
return id;
case "둔둔":
id = 8;
return id;
case "레버넌트 좀비":
id = 9;
return id;
case "우르자":
id = 10;
return id;
case "부기콜리":
id = 11;
return id;
case "그리폰":
id = 12;
return id;
case "프랑케네뜨":
id = 13;
return id;
case "경비대장 차우":
id = 14;
return id;
case "그리피나":
id = 15;
return id;
case "매드오네뜨":
id = 16;
return id;
case "자이언트 터틀":
id = 17;
return id;
}
return id;
}
private void createListview(){
listView = (ListView) findViewById(R.id.list_view);
search_view = (EditText)findViewById(R.id.search_text);
boss_title = getResources().getStringArray(R.array.boss_array);
bossList = new ArrayList<Boss>();
adapter = new BossAdapter(getApplicationContext(), bossList);
listView.setAdapter(adapter);
listView.setTextFilterEnabled(true);
bossTime = getKoreanTime();
// Moved our object creation here, so that it should only be done once.
for (String boss : boss_title) {
bossID = getID(boss);
bossAppearance = bossAppearance(bossID);
Boss bossObject = new Boss(boss_icon[bossID], boss, bossTime[bossID], bossAppearance[bossID]);
bossList.add(bossObject);
}
handler = new Handler();
Runnable update = new Runnable() {
@Override
public void run() {
bossTime = getKoreanTime();
int count = adapter.getCount();
for(String boss: boss_title){
bossID = getID(boss);
bossAppearance = bossAppearance(bossID);
((Boss) adapter.getItem(bossID)).setBoss_time(bossTime[bossID]); // Re-set time
((Boss) adapter.getItem(bossID)).setBoss_appearance(bossAppearance[bossID]);
}
adapter.notifyDataSetChanged(); // Notify our update
handler.postDelayed(this, 1000);
}
};
handler.postDelayed(update, 10);
EditText myFilter = (EditText) findViewById(R.id.search_text);
myFilter.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s.toString());
}
});
}
Strings.xml
Boss计时器
<string-array name="boss_array">
<item>다크 지란트</item>
<item>머쉬맘</item>
<item>MARK52 ALPHA</item>
<item>깡패 바라하</item>
<item>데블린 워리어</item>
<item>닉시</item>
<item>에피</item>
<item>자이언트 라바아이</item>
<item>둔둔</item>
<item>레버넌트 좀비</item>
<item>우르자</item>
<item>부기콜리</item>
<item>그리폰</item>
<item>프랑케네뜨</item>
<item>경비대장 차우</item>
<item>그리피나</item>
<item>매드오네뜨</item>
<item>자이언트 터틀</item>
</string-array>
日志记录器(LOGCAT)
07-24 17:11:18.628 11159-11159/baegmon.com.bosstimer E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: baegmon.com.bosstimer, PID: 11159
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at baegmon.com.bosstimer.BossAdapter.getItem(BossAdapter.java:38)
at baegmon.com.bosstimer.MainActivity$1.run(MainActivity.java:158)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)