views:

179

answers:

3

I have a little problem with listview. How do i clear a listview content, knowing that it has a custom adapter?

edit : the custom adapter class extends BaseAdapter, it looks like this :

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {

    private Activity activity;
    private String[] data;
    private static LayoutInflater inflater=null;

    public MyAdapter(Activity _a, String[] _str)
    {
        activity = _a;
        data = _str;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public static class ViewHolder{
        public TextView text;
    }

    @Override
    public int getCount() {
        return data.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        View v = view;
        ViewHolder holder;
        if(v == null)
        {
            v = inflater.inflate(R.layout.rowa, null);
            holder=new ViewHolder();
            holder.text=(TextView)v.findViewById(R.id.dexter);
            v.setTag(holder);
        }else{
            holder=(ViewHolder)v.getTag();
        }

        holder.text.setText(data[position]);


        return v;
    }

}
A: 

Remove your items from your custom adapter and call notifyDataSetChanged().

mreichelt
using notifyDataSetChanged() has no effect on listview contend.
Slash
A: 

Call clear() method from your custom adapter

carlovv
clean() is not available with BaseAdapter.
Slash
you can also set the listview adapter to null.
carlovv
it doesn't work.
Slash
listView.setAdapter(null)?
carlovv
+1  A: 

I guess you passed a List or an Array to the Adapter. If you keep the instance of this added collection, you can do a

collection.clear();
listview.getAdapter().notifyDataSetChanged();

this'll work only if you instantiated the adapter with collection and it's the same instance.

Also, depending on the Adapter you extended, you may not be able to do this. SimpleAdapter is used for static data, thus it can't be updated after creation.

PS. not all Adapters have a clear() method. ArrayAdapter does, but ListAdapter or SimpleAdapter don't

Maragues
i'm extending BaseAdapter and i think it hasn't clear() method.
Slash
Do you need it to be a BaseAdapter? If you could switch to extending an ArrayAdapter, you would get the clear() method. Looks like in BaseAdapter you need to use this registerDataSetObserver(DataSetObserver observer), I guess it'll update automatically if the DataSet changed. But, ArrayAdapter will probable make things easier. Good luck!
Maragues
thanks for help but can you give me a sample code on how to extend ArrayAdapter. thanks.
Slash
check http://stackoverflow.com/questions/2265661/how-to-use-arrayadaptermyclass It's really simillar to what you already have.
Maragues
thanks for sharing but i seems that my problem isn't related to the adapter, because even when i use the ArrayAdapter's clear() and i reload the activity, the previous content remains there in addition to the new content. i'm in real trouble. (Sorry for bad english, i hope you get the point).
Slash
Do you override the adapter data set or the adapter after creating it? That is, do you re assign the instances you used to add the adapter to the list? If these instances change, the connection between the adapter, the listview and the screen may fail.
Maragues
Sorry but i don't get it, can you please explain more!
Slash
it wasn't a n adapter problem, it's XML parsing problem. thaks a lot guys for help.
Slash