Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

wasiq aftab
wasiq aftab
1,994 Points

delete a row from list view on button click

i am creating an application in which data is got from a barcode scanner which identifies from an id, and show detail from the database through a web service. i am currently using custom list which has a text view and a button, the data which i want to embed on the textview is displaying correctly...

my question is how do i delete a particular row from the list on the button click .

here is the custom adaptor code.

public class MyCustomAdapter extends ArrayAdapter<String> 
{
public MyCustomAdapter(Context context, int textViewResourceId, ArrayList<String> items)
{
super(context, textViewResourceId, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = FusionMain.this.getLayoutInflater();
View row = inflater.inflate(R.layout.list_row, parent, false);
TextView item = (TextView)row.findViewById(R.id.tvItems);
item.setText(items.get(position));
return row;
}
} // end MyCustomAdapter

1 Answer

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

The basic idea is that you want to remove the item from your source array, i.e. whatever String array you are adapting with this custom adapter. Then you can call a method on your custom adapter called notifyDataSetChanged() and it will repopulate your list. Here's a StackOverflow answer that contains some example code that might help:

http://stackoverflow.com/a/5344958/475217