您好我正在开发具有listview的应用程序。我使用了自定义适配器并添加了过滤选项。当我在ssearchBox中放置文本时工作正常但问题是当我开始从searchBox中删除文本时它没有带回项目..它停留在那里。
这是我的代码......请帮助。
public class SimpleAdapter extends BaseAdapter implements SectionIndexer{
private String mSections = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private ArrayList<String> mCurrentList;
private Context mContext;
HolderFilter holderFilter;
public SimpleAdapter(ArrayList<String> mCurrentList, Context mContext) {
super();
this.mCurrentList = mCurrentList;
this.mContext = mContext;
}
@Override
public int getCount() {
if (mCurrentList != null)
return mCurrentList.size();
else
return 0;
}
@Override
public String getItem(int position) {
if (mCurrentList != null)
return mCurrentList.get(position);
else
return null;
}
@Override
public long getItemId(int position) {
return position;
}
public void onItemSelected(int position) {
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final int p = position;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.img_row_layout, null);
try {
TextView textRow = (TextView) convertView.findViewById(R.id.name);
textRow.setText(getItem(position));
} catch (Exception e) {
}
return convertView;
}
@Override
public int getPositionForSection(int section) {
for (int j = 0; j < getCount(); j++) {
if (section == 0) {
// For numeric section
for (int k = 0; k <= 9; k++) {
String text = null;
try {
text = mCurrentList.get(j);
} catch (Exception e) {
}
if (text == null)
return 0;
else if (String.valueOf(text.charAt(0)).toLowerCase().equals(String.valueOf(String.valueOf(k)).toString().toLowerCase()))
return j;
}
} else {
String artist = null;
try {
artist = mCurrentList.get(j);
} catch (Exception e) {
}
if (artist == null)
return 0;
else if (String.valueOf(artist.charAt(0)).toLowerCase().equals(String.valueOf(mSections.charAt(section)).toString().toLowerCase())) {
return j;
}
}
}
return 0;
}
@Override
public int getSectionForPosition(int position) {
return 0;
}
@Override
public Object[] getSections() {
String[] sections = new String[mSections.length()];
for (int i = 0; i < mSections.length(); i++)
sections[i] = String.valueOf(mSections.charAt(i));
return sections;
}
public Filter getFilter() {
if (holderFilter == null){
holderFilter = new HolderFilter();
}
return holderFilter;
}
@SuppressLint("DefaultLocale")
private class HolderFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint == null || constraint.length() == 0) {
results.values = mCurrentList;
results.count = mCurrentList.size();
}else {
List<String> nHolderList = new ArrayList<String>();
for (String h : mCurrentList) {
if (h.toString().toUpperCase().startsWith(constraint.toString().toUpperCase()))
nHolderList.add(h);
}
results.values = nHolderList;
results.count = nHolderList.size();
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,FilterResults results) {
if (results.count == 0)
notifyDataSetInvalidated();
else {
mCurrentList = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
}
}
}
答案 0 :(得分:1)
当您过滤项目时,您将重置原始包含所有项目的数组。你需要保留它,有两个数组:
见下面的例子:
/*
* Author: Madhur Ahuja
* Copyright (c) 2014.
*/
package in.co.madhur.vocabbuilder.ui;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.RatingBar;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import in.co.madhur.vocabbuilder.R;
import in.co.madhur.vocabbuilder.model.Word;
/**
* Created by madhur on 22-Jun-14.
*/
public class WordTokenAdapter extends BaseAdapter implements Filterable
{
private List<Word> words, originalWords;
private Context context;
public WordTokenAdapter(List<Word> words, Context context)
{
this.setWords(words);
this.context = context;
originalWords = new ArrayList<Word>();
for (Word word : words)
{
originalWords.add(word);
}
}
@Override
public int getCount()
{
return getWords().size();
}
@Override
public Object getItem(int position)
{
return getWords().get(position);
}
@Override
public long getItemId(int position)
{
return getWords().get(position).getId();
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
Word word= (Word) getItem(position);
View view;
ViewHolder holder;
if (convertView == null)
{
holder = new ViewHolder();
view = LayoutInflater.from(context).inflate(R.layout.word_token_item, parent, false);
holder.word = (TextView) view.findViewById(R.id.word);
holder.ratingBar=(RatingBar)view.findViewById(R.id.ratingBar);
view.setTag(holder);
}
else
{
view = convertView;
holder = (ViewHolder) view.getTag();
}
holder.word.setText(word.getName());
holder.ratingBar.setRating((float)word.getRating()/2);
return view;
}
public List<Word> getWords()
{
return words;
}
public void setWords(List<Word> words)
{
this.words = words;
}
@Override
public Filter getFilter()
{
return new Filter()
{
@Override
protected FilterResults performFiltering(CharSequence constraint)
{
FilterResults results = new FilterResults();
List<Word> filteredWords;
// perform your search here using the searchConstraint String.
if (constraint != null && constraint.length() > 0)
{
filteredWords = new ArrayList<Word>();
for (Word word : originalWords)
{
if (word.getName().startsWith((String) constraint))
{
filteredWords.add(word);
}
}
}
else
{
filteredWords = originalWords;
}
synchronized (this)
{
results.count = filteredWords.size();
results.values = filteredWords;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results)
{
List<Word> filteredWords = (List<Word>) results.values;
words = filteredWords;
notifyDataSetChanged();
}
};
}
private static class ViewHolder
{
TextView word;
RatingBar ratingBar;
}
}