Android : Custom ListView Filtering and Bring back old item in the list

on Monday, July 7, 2014


Hi I am developing app that has listview. I have used custom Adapter and added filtering option.It's working fine when I put text in the ssearchBox But the problem is when I start to erase the text from searchBox it does not bring back the item..It stays there.


This is my code...Please help.



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 comments:

Post a Comment