我使用ListView來顯示搜索結果。我有一個Coustomized arrayListAdapter,它返回一個對象列表。這工作正常。我的listView只顯示一個TextView沒有別的。自定義ArrayListAdapter.clear()不起作用
但是我想再次進入搜索頁面時清除列表。我曾嘗試
@Override
protected void onResume()
{
mListAdapter.clear();
mListAdapter.notifyDataSetChanged();
super.onResume();
}
而且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;
}
}
我的問題是,怎樣才能清除列表?
謝謝。
後一些更多的代碼.. – 2012-02-18 12:25:43