Question

I have a ListView, where each list item row has a custom layout that has a person's name and a delete button.

I do not know how I should code the event handler in the delete button in such a way that it will delete the row. I know that I need to delete the item in the ArrayList and then call adapter.notifyDataSetChanged(), but I do not have access to either the ArrayList or the adapter from inside my custom row layout class.

I have seen some similar questions asked, but I do not see any the involve a delete button that is inside a custom list item layout.

The only possible solution I can think of is to pass in a reference to the adapter object and also a reference to the ArrayList into the PersonLayout (doing this inside the adapter's getView() method), but there has to be a better solution.

Here is the code:

/**
 * PersonLayout is the layout for a single list item (row) in the listview.
 * It displays the name for a single person.
 * 
 * Each PersonLayout row also contains a delete button that is used to delete that row.
 * 
 * I do not know what I should do in onClick() for the delete button
 * in order to delete this row.
 */
public class PersonLayout extends RelativeLayout implements OnClickListener 
{
    private TextView nameTextView;
    private Button deleteButton;

    private Person person;

    private Context context;

    public PersonLayout(Context context)
    {
        super(context);
    }

    public PersonLayout(Context context, Person p)
    {
        super(context);

        this.context = context;

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.listview_person, this, true);

        nameTextView = (TextView) findViewById(R.id.nameTextView);
        deleteButton = (Button) findViewById(R.id.deleteButton);

        this.setOnClickListener(this);

        setPerson(p);
    }

    public void setPerson(Person p)
    {
        person = p;

        nameTextView.setText(p.getName());
    }

    @Override
    public void onClick(View v)
    {
        // handle delete button click

        // How do I delete the current list item (row) ?
    }

}  // end class PersonLayout


/**
 * The custom adapter for the ListView.
 */
public class PeopleListAdapter extends BaseAdapter
{
    private Context context;
    private ArrayList<Person> people;

    public PeopleListAdapter(Context context, ArrayList<Person> people)
    {
        this.context = context;
        this.people = people
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        PersonLayout personLayout = null;
        Person person = people.get(position);

        if (convertView == null)
        {
            personLayout = new PersonLayout(context, person);
        }
        else
        {
            personLayout = (PersonLayout) convertView;
            personLayout.setPerson(person);
        }

        return personLayout;
    }

}  // end class PeopleListAdapter
Was it helpful?

Solution

Why do you create a new class for your layout? You can just create a layout xml and inflate it inside your adapter's getView method.

If you do it like that, you can set the click listener inside the getview method of the adapter, and from there you have access to both the arraylist and the adapter.

OTHER TIPS

I had same problem. After so many days search, settled with Delegates in JAVA concept. Here is a tutorial Click here. Then add click listener on your delete button. From that event send delete action to necessary object. You can try answer from Mark Buikema also.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top