Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android

Felipe Schauren
Felipe Schauren
6,367 Points

Removing a ListView with a CheckBox and an EditText should remove the content inside.

Hi everyone, I'm working in an app that creates a shopping list; It consists of a ListView with a CheckBox, an EditText and a Button inside. When the Button is pressed it should delete that row and its contents, but right now it seems to only be deleting the row.

It also has a button outside of the ListView that adds new rows.

Here's my getView:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    View view = convertView;

    if (view == null) {
        holder = new ViewHolder();
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.single_list_item, null);

        view.setTag(holder);
    } else {
        holder = (ViewHolder)convertView.getTag();
    }

    holder.listItemEdit = (EditText) view.findViewById(R.id.itemEditText);
    holder.listItemText = (TextView) view.findViewById(R.id.itemTextView);
    holder.listItemDel = (Button) view.findViewById(R.id.itemDelButton);

    holder.listItemDel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mList.remove(position);

            notifyDataSetChanged();
        }
    });

    return view;
}

public static class ViewHolder {
    public TextView listItemText;
    public EditText listItemEdit;
    public Button listItemDel;
}

Here's the relevant code for the ListView in my MainActivity:

    mItemListView = (ListView) findViewById(R.id.itemListView);
    mTitleTextView = (TextView) findViewById(R.id.titleTextView);
    mAddButton = (Button) findViewById(R.id.addButton);

    final ArrayList<String> list = new ArrayList<String>();
    list.add(null);
    final MyCustomAdapter adapter = new MyCustomAdapter(list, this);

    mItemListView.setAdapter(adapter);

    //create a new item for the list
    mAddButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            list.add(null);
            adapter.notifyDataSetChanged();
        }
    });

An example of what happens when I try to use my app:

     Line | Checkbox | EditText
     1    | x         | Sample text 1
     2    | o         | Sample text 2
     3    | x         | Sample text 3
     4    | o         | Sample text 4

if I delete line 3, it will look like this:

     1    | x         | Sample text 1
     2    | o         | Sample text 2
     4    | x         | Sample text 3

The correct line is removed but its contents move to the line that takes its place.

If I were to add a new line after deleting that line, the new line would have the contents of the last line that was deleted (in this example, row 5 would have the contents that were in row 4)

Thank you.