I am trying to search the LazyAdapter class by creating my own filter. But when I try to search using TextWatcher, the application is forced to close. The code for the LazyAdapter class is as follows: com.demo.directory package; Public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity.getApplicationContext());
}
public View getFilter(CharSequence seq, View convertView) {
String name_org;
View vi = convertView;
convertView = null;
if (convertView == null)
vi = inflater.inflate(R.layout.list_row, null);
TextView name = (TextView) vi.findViewById(R.id.name_org);
TextView address = (TextView) vi.findViewById(R.id.address_org);
ImageView thumb_image = (ImageView) vi.findViewById(R.id.list_image);
for (int i = 0; i < data.size(); i++) {
HashMap<String, String> org = new HashMap<String, String>();
org = data.get(i);
name_org = org.get(OrganizationActivity.KEY_NAME);
if (name_org != null && seq != null) {
if (name_org.contains(seq)) {
name.setText(org.get(OrganizationActivity.KEY_NAME));
address.setText(org.get(OrganizationActivity.KEY_CITY)
+ ", " + org.get(OrganizationActivity.KEY_STATE));
imageLoader.DisplayImage(
org.get(OrganizationActivity.KEY_IMAGE_URL),
thumb_image);
notifyDataSetChanged();
} else {
org.remove(OrganizationActivity.KEY_NAME);
}
}
}
return vi;
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.list_row, null);
TextView name = (TextView) vi.findViewById(R.id.name_org);
TextView address = (TextView) vi.findViewById(R.id.address_org);
ImageView thumb_image = (ImageView) vi.findViewById(R.id.list_image);
HashMap<String, String> org = new HashMap<String, String>();
org = data.get(position);
name.setText(org.get(OrganizationActivity.KEY_NAME));
address.setText(org.get(OrganizationActivity.KEY_CITY) + ", "
+ org.get(OrganizationActivity.KEY_STATE));
imageLoader.DisplayImage(org.get(OrganizationActivity.KEY_IMAGE_URL),
thumb_image);
return vi;
}
}
The above code now works, but it shows nothing if I delete the search data, i.e. ListView source data will not be returned if the EditText data is deleted after the search.
source
share