How to delete a custom listview item in android?

筅森魡賤 提交于 2019-12-06 13:44:31

If you know the position of the item you can do this:

Object item = adapter.getItem(position);

adapter.remove(item);

adapter.notifyDataSetChanged();

You may write your own adapter extends BaseAdapter and implement all you need methods.

It is example of my adapter:

public class PeopleUserAdapter extends BaseAdapter 
{
private List<User> users;
private int viewResourceId;
private Context context;

public PeopleUserAdapter(Context context, int viewResourceId) 
{
    this.context = context;
    this.viewResourceId = viewResourceId;
    this.users = new ArrayList<User>();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    UserItemHolder holder;
    if (convertView == null)
    {
        convertView = LayoutInflater.from(context).inflate(viewResourceId, parent, false);
        holder = new UserItemHolder(convertView);
    }
    else holder = (UserItemHolder) convertView.getTag();

    User user = getItem(position);
    holder.name.setText("@" + user.getLogin());
    return convertView;
}

@Override
public int getCount() 
{
    return users.size();
}

@Override
public User getItem(int position) 
{
    return users.get(position);
}

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

public void clear()
{
    users.clear();
}

public void addAll(Collection<User> users)
{
    this.users.addAll(users);
    notifyDataSetChanged();
}

public void replace(Collection<User> users)
{
    clear();
    addAll(users);
}

public static PeopleUserAdapter init(Context context)
{
    return new PeopleUserAdapter(context, R.layout.item_user);
}

}

adapter.remove(item) .. and then call adapter.notifyDataSetChanged();

In case you are using a custom adapter (for a custom layout listview), you will want to do this:

When your Adapter is something like:

public class YourAdapterName extends ArrayAdapter<yourObject>

then the code for deleting the selected ListView Item will be:

ListView yourListView = (ListView) findViewById(R.id.listviewid); 
YourAdapterName adapter;
adapter = (YourAdapterName) yourListView.getAdapter();
yourObject theitem = adapter.getItem(position);
adapter.remove(theitem);
adapte.notifyDataSetChanged();

This is assuming you are inside an event that gives you access to the current position inside the listview. like:

public boolean onItemLongClick(AdapterView<?> parent, View strings,int position, long id)

or

public void onItemClick(AdapterView<?> arg0, View v, int position, long id)

Otherwise you will need to obtain that position some other way, like storing it (onItemClick or onItemLongClick) in a textView with Visibility.GONE, and retrieve it when clicking the button (this is silly, you can use all kinds of storage options, like global variables, database and such).

Make sure you have overridden the remove method on your custom adapter

For example if this is your add method:

@Override
public void add(String[] object) {
    scoreList.add(object);
    super.add(object);
}

then your remove method would look something like this:

@Override
public void remove(String[] object) {
    scoreList.remove(object);
    super.remove(object);
}

call the below two lines::

adapter.remove(inverterNo);
adapter.notifyDataSetChanged();

where inverterNo is your item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!