我实现了一个过滤器来搜索我的CustomAdapter.java
我添加一个视频链接,以便更好地解释我的问题。
https://www.youtube.com/watch?v=jVzVeC7dZ-8
我有2个问题:
过滤器后页面打开不正确
过滤器
HedlinesFragment.java
public class HeadlinesFragment extends ListFragment implements
SearchView.OnQueryTextListener {
String[] Headlines = {
"Article One",
"Article Two",
"Article 3",
"Article 4",
"Article 5",
"Article 6",
};
SearchView search_view;
OnHeadlineSelectedListener mCallback;
String[] menutitles;
TypedArray menuIcons;
CustomAdapter adapter;
private List<RowItem> rowItems;
// The container Activity must implement this interface so the frag can deliver messages
public interface OnHeadlineSelectedListener {
/** Called by HeadlinesFragment when a list item is selected */
public void onArticleSelected(int position);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// We need to use a different list item layout for devices older than Honeycomb
int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
android.R.layout.simple_list_item_activated_1 : android.R.layout.simple_list_item_1;
return inflater.inflate(R.layout.list_fragment, null, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
menutitles = getResources().getStringArray(R.array.titles);
menuIcons = getResources().obtainTypedArray(R.array.icons);
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < menutitles.length; i++) {
RowItem items = new RowItem(menutitles[i], menuIcons.getResourceId(
i, -1));
rowItems.add(items);
}
adapter = new CustomAdapter(getActivity(), (ArrayList<RowItem>) rowItems);
setListAdapter(adapter);
search_view = (SearchView) getActivity().findViewById(R.id.editText);
search_view.setOnQueryTextListener(this);
}
@Override
public void onStart() {
super.onStart();
// When in two-pane layout, set the listview to highlight the selected list item
// (We do this during onStart because at the point the listview is available.)
if (getFragmentManager().findFragmentById(R.id.article_fragment) != null) {
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception.
try {
mCallback = (OnHeadlineSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Notify the parent activity of selected item
mCallback.onArticleSelected(position);
// Set the item as checked to be highlighted when in two-pane layout
getListView().setItemChecked(position, true);
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
CustomAdapter.java
public class CustomAdapter extends BaseAdapter implements Filterable {
Context context;
ArrayList<RowItem> rowItem;
ArrayList<RowItem> mStringFilterList;
ValueFilter valueFilter;
CustomAdapter(Context context, ArrayList<RowItem> rowItem) {
this.context = context;
this.rowItem = rowItem;
mStringFilterList = rowItem;
}
@Override
public int getCount() {
return rowItem.size();
}
@Override
public Object getItem(int position) {
return rowItem.get(position);
}
@Override
public long getItemId(int position) {
return rowItem.indexOf(getItem(position));
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.mylist, null);
}
ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);
TextView txtTitle = (TextView) convertView.findViewById(R.id.item);
RowItem row_pos = rowItem.get(position);
// setting the image resource and title
imgIcon.setImageResource(row_pos.getIcon());
txtTitle.setText(row_pos.getTitle());
Picasso
.with(context)
.load(Ipsum.url[position])
.fit() // will explain later
.centerCrop()
.into(imgIcon);
return convertView;
}
@Override
public Filter getFilter() {
if (valueFilter == null) {
valueFilter = new ValueFilter();
}
return valueFilter;
}
private class ValueFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
ArrayList<RowItem> filterList = new ArrayList<RowItem>();
for (int i = 0; i < mStringFilterList.size(); i++) {
if ( (mStringFilterList.get(i).getTitle().toUpperCase() )
.contains(constraint.toString().toUpperCase())) {
RowItem rowItem = new RowItem(mStringFilterList.get(i)
.getTitle() , mStringFilterList.get(i)
.getIcon()
);
filterList.add(rowItem);
}
}
results.count = filterList.size();
results.values = filterList;
} else {
results.count = mStringFilterList.size();
results.values = mStringFilterList;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
rowItem = (ArrayList<RowItem>) results.values;
notifyDataSetChanged();
}
}
RowItem.java
public class RowItem {
private String title;
private int icon;
public RowItem(String title, int icon) {
this.title = title;
this.icon = icon;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}