asish
asish

Reputation: 4807

Customized ArrayListAdapter.clear() is not working

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.

Upvotes: 3

Views: 1214

Answers (2)

user
user

Reputation: 87064

I think you have to override the method clear() for your custom ArrayAdapter and manually empty the list of objects on which your adapter is based(and don't forget to call notifyDataSetChanged()). This is what the default ArrayAdapter is 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 :

ArrayList<Chapter> data;

and then in your adapter's constructor initialize it by calling the method getSearchChapters() :

data = getSearchChapters();

Then you can override the method clear():

    @Override
    public void clear() {
        data.clear();
        notifyDataSetChanged();
        super.clear();
    }

Also you can't call clear from onResume() 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 the adapter.clear(). You can make a test with a button that calls clear() to see if this clears the adapter.

Upvotes: 2

JackMahoney
JackMahoney

Reputation: 3433

Clear the ArrayList that the Adapter is using then call notifyDataSetChanged() on Adapter

Upvotes: 0

Related Questions