问题
I am trying to perform a search on LazyAdapter class by creating a custom Filter. But when I am trying to perform the search by using the TextWatcher, the application is forcing close. The code for the LazyAdapter class is as follows: package com.demo.directory; 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); // title
TextView address = (TextView) vi.findViewById(R.id.address_org); // artist
// name
ImageView thumb_image = (ImageView) vi.findViewById(R.id.list_image); // thumb
// 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); // title
TextView address = (TextView) vi.findViewById(R.id.address_org); // artist
// name
ImageView thumb_image = (ImageView) vi.findViewById(R.id.list_image); // thumb
// image
HashMap<String, String> org = new HashMap<String, String>();
org = data.get(position);
// Setting all values in listview
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 is now working, but it doesn't show anything if I delete the search data, i.e., the original data of the ListView is not returned if the EditText data is cleared back after searching..
回答1:
To retain data items after you clear search edittext, below code is useful. Hope this helps you.
public Filter getFilter()
{
Filter filter = new Filter()
{
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results)
{
System.out.println(" --- constraint in publishResults --- "+constraint+" - results - "+results);
itemDetailList = (List<Dish>)results.values;
notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint)
{
FilterResults filterResults = new FilterResults();
List<Dish> filtered_list = new ArrayList<Dish>();
if(originalItemsDetails == null)
{
System.out.println(" --- originalItemsDetails IS NULL --- ");
originalItemsDetails = new ArrayList<Dish>(itemDetailList);
}
else
{
System.out.println(" --- originalItemsDetails NOT NULL --- ");
}
if(constraint == null || constraint.length() == 0)
{
filterResults.count = originalItemsDetails.size();
filterResults.values = originalItemsDetails;
}
else
{
constraint = constraint.toString().toLowerCase();
for(int i=0; i<originalItemsDetails.size(); i++)
{
String dataNames = originalItemsDetails.get(i).getDishName().toLowerCase();
if(dataNames.startsWith(constraint.toString()))
{
filtered_list.add(originalItemsDetails.get(i));
}
}
filterResults.count = filtered_list.size();
System.out.println(" --- filterResults.count --- "+filterResults.count);
filterResults.values = filtered_list;
System.out.println(" --- filterResults.values --- "+filterResults.values);
}
System.out.println(" --- constraint in performFiltering --- "+constraint);
return filterResults;
}
};
return filter;
}
回答2:
Are you sure your name_org
and s
variables are not null???
You have to first check it, like,
if(name_org != null && s != null)
{
if(name_org.contains(s))
{
// Your other stuff goes here...
.
.
}
}
回答3:
You can try and create a custom Filter that can help in showing the data back again once the data in the edit text becomes empty. Or you can create two adapters for the listview, first one containing the original data of the listview and the second one which displays the search results for the edit text search. Once the data in edit text becomes empty, set the List Adapter to the initial one.
回答4:
I think s is null, have you checked it. Because contains method throws NPE even if charsequence that you pass is null
来源:https://stackoverflow.com/questions/11823720/nullpointerexception-at-lazyadapter