0
嗨,我正在開發有ListView的應用程序。我已經使用自定義適配器和添加過濾option.It工作正常,當我把文本在ssearchBox但問題是,當我開始刪除文本從searchBox它不會帶回項目..它停留在那裏。自定義ListView篩選和回收列表中的舊項目
這是我的代碼...請幫忙。
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();
}
}
}
}
對不起,我是新來android.Can你告訴我,在我的片段添加代碼? –