I’m using ListView to show search results. I have a Coustomized arrayListAdapter which returns a list of Objects. This is working fine. My listView is showing just a TextView nothing else.
But I want to clear the List when I again go to the search page. I have tried
@Override
protected void onResume()
{
mListAdapter.clear();
mListAdapter.notifyDataSetChanged();
super.onResume();
}
And the SearchListAdapter
public class SearchListAdapter extends ArrayAdapter<Chapter>
{
public Context context;
private static String searchText = null;
public SearchListAdapter(Context context)
{
super(context, R.layout.favorite_tab_list_view_row);
this.context = context;
}
@Override
public void clear()
{
for (int i = 0; i < getCount(); i++)
getSearchChapters().remove(i);
notifyDataSetChanged();
super.clear();
}
private ArrayList<Chapter> getSearchChapters()
{
ArrayList<Chapter> chapter = new ArrayList<Chapter>();
if (searchText != null)
for (int i = 0; i < DataStore.getHierarchicalChapters()
.getSubChapters().size(); i++)
{
String str = DataStore.getHierarchicalChapters()
.getSubChapters().get(i).getChapterContent()
.toLowerCase();
Pattern pat = Pattern.compile(searchText.toLowerCase());
Matcher mat = pat.matcher(str);
while (mat.find())
{mat.start()); //
chapter.add(DataStore.getHierarchicalChapters()
.getSubChapters().get(i)); // the
break;
// occurrance
}
if (DataStore.getHierarchicalChapters().getSubChapters().get(i)
.hasSubchapters())
{
for (int j = 0; j < DataStore.getHierarchicalChapters()
.getSubChapters().get(i).getSubChapters().size(); j++)
{
str = DataStore.getHierarchicalChapters()
.getSubChapters().get(i).getSubChapters()
.get(j).getChapterContent();
mat = pat.matcher(str);
while (mat.find())
{
chapter.add(DataStore.getHierarchicalChapters()
.getSubChapters().get(i).getSubChapters()
.get(j));
break;
}
}
}
}
return chapter;
}
@Override
public int getCount()
{
return getSearchChapters().size();
}
@Override
public Chapter getItem(int position)
{
return getSearchChapters().get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if (row == null)
{
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.favorite_tab_list_view_row, parent,
false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.chapterName = (TextView) row
.findViewById(R.id.favoriteChapterTextViewId);
row.setTag(viewHolder);
}
ViewHolder viewHolder = (ViewHolder) row.getTag();
viewHolder.chapterName.setText(getSearchChapters().get(position)
.getTitle());
return row;
}
static class ViewHolder
{
public TextView chapterName;
}
public static void setSearchText(String searchText)
{
SearchListAdapter.searchText = searchText;
}
}
My question is, How can I clear the list ?
Thank You.
I think you have to
overridethe methodclear()for your customArrayAdapterand manually empty the list of objects on which your adapter is based(and don’t forget to callnotifyDataSetChanged()). This is what the defaultArrayAdapteris doing.EDIT :
Your code will never work now because your custom adapter gets its data from calling the method
private ArrayList<Chapter> getSearchChapters()which rebuilds the adapter’s data every time the method is called(for example every time the adapter calls getView() it will rebuild the data). My advice is to make a private field in your adapter :and then in your adapter’s constructor initialize it by calling the method
getSearchChapters():Then you can override the method
clear():Also you can’t call
clearfromonResume()because your list will never get populated with data(the adapter will be empty). I didn’t understand when you want to clear the list so i can’t tell you when to call theadapter.clear(). You can make a test with a button that callsclear()to see if this clears the adapter.