How to delete a custom listview item in android?

天大地大妈咪最大 提交于 2019-12-22 17:39:19

问题


I have a listview and a button in my layout file. I'am adding items to listview on click of that button. The listview should be empty when the activity is started but it should grow by adding the items to it. This is my code inside onCreate() :

list  = (ListView)findViewById(R.id.inverterListView);
adapter = new ArrayAdapter<String>(InverterList.this, R.layout.inverters_list_row, R.id.inverterNumberTextViewInPanelListRow);
 list.setAdapter(adapter); 

And here iam adding the items to listview onclick of a button.

adapter.add(inverterNo);
adapter.notifyDataSetChanged();

This works fine. Can anyone guide me to delete custom listview item ? Thanks in advance.


回答1:


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

Object item = adapter.getItem(position);

adapter.remove(item);

adapter.notifyDataSetChanged();



回答2:


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);
}

}




回答3:


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




回答4:


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).




回答5:


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);
}



回答6:


call the below two lines::

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

where inverterNo is your item



来源:https://stackoverflow.com/questions/10311431/how-to-delete-a-custom-listview-item-in-android

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