Android Tutorial 5 - Custom ArrayAdapter Class

MimirSoft · 17K views · 2011 · 3:05

Open on YouTube

Software (other)tutorialdated

Why Stack Overflow recommends it

Stack Overflow users link it when they tell askers to build their own ArrayAdapter to customise list items, handle row listeners, or use their own model class in a list.

Recommended by 2 people across 2 different months, 2011 to 2012.

What it is

A roughly three-minute Android tutorial from MimirSoft that follows on from the previous episode in the series. It shows how to write and use a custom ArrayAdapter class instead of the built-in adapter, so an array of strings can be mapped to a ListView.

What people said

Recommends Knossos · 2011-12 · Stack Overflow · on “Mapping value selected in Spinner to other values”

Personally, I would create a custom ArrayAdapter. That would let you use your own class (class Food, for example) as the item in the list. That way you can do all of your calculations abstracted. This video [this video] may get you started on the right foot.

Recommends vinothp · 2012-07 · Stack Overflow · on “android: how to retain clicked list item's pressed/selected state until pressing other items on list”

Try this Step 1: Try to reset background color(i.e your original background color) to all list items using for loop in onItemClickListener() . Step 2 :Set Different background color for selected one. If you using Customized list View then it will be really easy. EDIT Customized List View Tutorial Custom ListView [LINK] and this [LINK] Video - [this video] Hope it helps EDIT Here's what I did: I iterated through the visible list to set the each item's background to transparent the setting the selected item's background a different color. This way, the color sticks until I press another item. protected void onListItemClick(ListView l, View v, int position, long id) { int childCount = l.getChildCoun

Cites Roy Hinkley · 2012-10 · Stack Overflow · on “Listview populated by sql db with onListItemClick to fill textviews in other activity”

If you want to set up listeners on list items, you will need to extend array adapter and put the listener code in the adapter: public class SettingsListAdapter extends ArrayAdapter<String> { @Override public View getView(int position, View convertView, ViewGroup parent){ // put your listener in here mybutton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // do work here } } } In other words, the simplelistadapter is not sufficient to do want you want, you will need to create your own. edit Here's a good example of creating your own custom ArrayAdapter: Custom Array Adapter Tutorial [this video]