How can I remove a custom list item in android?

I have a list and a button in my layout file. I am adding items to listview when this button is clicked. When the operation starts, the list should be empty, but it should grow by adding 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 is adding items to the listview onclick of the button.

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

      

This works great. Can anyone help me to remove a custom list item? Thanks in advance.

+2


source to share


6 answers


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



Object item = adapter.getItem(position);

adapter.remove(item);

adapter.notifyDataSetChanged();

      

+7


source


You can write your own adapter that extends BaseAdapter and implements all the required methods.

This is an 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


source


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

+1


source


If you are using a custom adapter (for a custom layout list) you want to do this:

When your adapter looks something like this:

public class YourAdapterName extends ArrayAdapter<yourObject>

      

then the code to remove the selected ListView item would be:

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

      

It assumes that you are inside an event, which gives you access to the current position within the list. as:

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 get this position in some other way, for example, store it (onItemClick or onItemLongClick) in the textView using Visibility.GONE and get it when the button is clicked (this is silly, you can use all kinds of storage parameters like like globals, database, etc.).

+1


source


Make sure you override the uninstall method to 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 delete method will look something like this:

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

      

+1


source


call the following two lines ::

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

      

where inverterNo is your element

0


source







All Articles