我正在尝试实现一个Listview,当用户输入搜索查询或从过滤器选项中选择一个选项时,它会动态更改。我有搜索功能,但过滤器选项不是。 通过过滤器选项我的意思是,如果用户点击下面显示的弹出列表中的选项。 问题是,如果我按下过滤器选项列表确实得到过滤器并正确显示但是当我尝试滚动到列表按钮时,应用程序将崩溃。我得到一个IndexOutOfBoundsExecption但我无法弄清楚为什么。 logcat的完整错误是
01-16 19:26:11.126 9933-9933/uk.ac.uea.campus_map E/AndroidRuntime: FATAL EXCEPTION: main
Process: uk.ac.uea.campus_map, PID: 9933
java.lang.IndexOutOfBoundsException: Index: 29, Size: 29
at java.util.ArrayList.get(ArrayList.java:411)
at kyle.frameworklibrary.Rev.List.getItem(List.java:37)
at kyle.frameworklibrary.Rev.InfoItemListController.getItem(InfoItemListController.java:25)
at kyle.frameworklibrary.Rev.InfoItemListController.getView(InfoItemListController.java:47)
at android.widget.AbsListView.obtainView(AbsListView.java:2376)
at android.widget.ListView.makeAndAddView(ListView.java:1972)
at android.widget.ListView.fillDown(ListView.java:704)
at android.widget.ListView.fillGap(ListView.java:668)
at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5221)
at android.widget.ListView.trackMotionScroll(ListView.java:1889)
at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4769)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
at android.view.Choreographer.doCallbacks(Choreographer.java:683)
at android.view.Choreographer.doFrame(Choreographer.java:616)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6209)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
01-16 19:26:11.126 9933-9933/uk.ac.uea.campus_map D/AppTracker: App Event: crash
01-16 19:26:11.139 9933-9933/uk.ac.uea.campus_map I/Process: Sending signal. PID: 9933 SIG: 9
我的ListView类代码是
package uk.ac.uea.campus_map;
import kyle.frameworklibrary.Rev.InfoItem;
import kyle.frameworklibrary.Rev.InfoView;
import kyle.frameworklibrary.Rev.ListController;
import kyle.frameworklibrary.Rev.ListView;
import android.app.Fragment;
import android.app.SearchManager;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.SearchView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Danny on 14/01/2017.
*/
public class BuildingListView extends ListView implements SearchView.OnQueryTextListener{
AppContainerActivity callback;
BuildingListController listController;
SearchView searchView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
setHasOptionsMenu(true);
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
callback.getMenuInflater().inflate(R.menu.building_list_toolbar, menu);
searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.search));
SearchManager searchManager = (SearchManager) callback.getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(callback.getComponentName()));
searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(this);
super.onCreateOptionsMenu(menu, inflater);
}
public static Fragment createFragment(AppContainerActivity ACA){
BuildingListView thisListView = new BuildingListView();
thisListView.setACA(ACA);
return thisListView;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public void setACA(AppContainerActivity ACA){
callback = ACA;
}
@Override
public void onClick(int i) {
InfoView goToFrag = new InfoView();
goToFrag.setInfo((InfoItem)lc.getItem(i));
callback.gotoFragAdd(goToFrag,0,0);
}
@Override
public ListController getListController() {
listController = new BuildingListController(callback.buildingList, this);
return listController;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
lc.getFilter().filter(newText);
lv.setAdapter(listController);
lv.invalidateViews();
return false;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.filter_academic_dept:
lc.getFilter().filter("type_academic_dept");
lv.invalidateViews();
lv.refreshDrawableState();
return true;
}
return super.onOptionsItemSelected(item);
}
}
我的listadapter代码是
package uk.ac.uea.campus_map;
import android.util.Log;
import android.widget.Filter;
import kyle.frameworklibrary.Rev.InfoItem;
import kyle.frameworklibrary.Rev.InfoItemListController;
import kyle.frameworklibrary.Rev.List;
import kyle.frameworklibrary.Rev.ListView;
/**
* Created by Danny on 16/01/2017.
*/
public class BuildingListController extends InfoItemListController {
protected ItemFilter itemFilter;
public BuildingListController(List<InfoItem> l, ListView li) {
super(l, li);
}
@Override
public Filter getFilter() {
if(itemFilter == null){
itemFilter = new ItemFilter();
}
return itemFilter;
}
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString();
FilterResults fitlerResults = new FilterResults();
if(constraint!=null){
List<InfoItem> tempList = new List<InfoItem>();
if(filterString.startsWith("type")){
filterString = filterString.substring(5);
for (int i = 0; i < list.getSize(); i++) {
if (list.getItem(i).getType().toLowerCase().contains(filterString)) {
tempList.add(list.getItem(i));
}
}
} else {
for (int i = 0; i < list.getSize(); i++) {
if (list.getItem(i).getTitle().toLowerCase().contains(filterString)) {
tempList.add(list.getItem(i));
}
}
}
fitlerResults.count = tempList.getSize();
fitlerResults.values = tempList;
}
return fitlerResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredList = (List<InfoItem>) results.values;
notifyDataSetChanged();
}
}
}
非常感谢任何帮助
答案 0 :(得分:1)
您应该最有可能覆盖BuildingListView中的getCount方法并返回过滤后的项目数量