2

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.

7 Answers 7

7

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

Object item = adapter.getItem(position);

adapter.remove(item);

adapter.notifyDataSetChanged();
Sign up to request clarification or add additional context in comments.

1 Comment

I added some code for a custom adapter bellow... just for people who need something like that. ListViews are more or less customized by developers in order to make them more beautiful. When that happens, things change... Check out the Answer bellow.
3

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

}

Comments

1

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

Comments

1

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

1 Comment

This works all right for ArrayAdapter but you cannot use Generics with BaseAdapter.
1

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

Comments

0

call the below two lines::

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

where inverterNo is your item

Comments

0

It easy; you only to need is: add a method public in your personalize adapter some this:

public void remove(int position) {
    itemsMovieModelFiltered.remove(position);
    notifyDataSetChanged();
}

Remenber, this method you must add in your personalize adapter. Then, call this method from other

 adapte=new PersonalizeListAdapter(getActivity().getApplicationContext(),
            movieModelList);
adapte.remove(position);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.