Can anyone explain android.R.layout.simple_list_item_1 and android.R.layout.simple_list_item_2 in arrayadapter in android.
I know in android.R.layout.simple_list_ite
Like you noticed, layout_1 has one textView and it's the default one to be used. layout_2 has two text views - the other one used as a subtext.
but here's the trick - not all adapters make use of the subtext one ;)
I found it easier (not gonna say mandatory) to write a purpose-built custom adapter for anything and everything...
For instance, here's a custom adapter that will display a name and it's status using this simple_list_item_2
This WILL NOT be copy/paste code, but you'll fix it with a few tweaks...
public class BuddyArrayAdapter extends ArrayAdapter
{
private static final String tag = "BuddyArrayAdapter";
private Context context;
private TextView buddyName;
private TextView buddyStatus;
private List buddies = new ArrayList();
/**
* The default constructor which is invoked to create the buddy array
* adapter.
*
* The adapter is needed to 'translate' data into a viewable item / widget.
*
* @param context
* the application context
* @param objects
* the backing array populated by Buddy objects to be displayed.
* @see {@link ArrayAdapter}
*/
public BuddyArrayAdapter(Context context, int textViewResourceId, List objects)
{
super(context, textViewResourceId, objects);
this.context = context;
this.buddies = objects;
Collections.sort(buddies);
}
/**
* The method used for determining how many views are in this list or in
* other words, how many views are managed by this adapter.
*
* @return the number of items this adapter controls.
*/
@Override
public int getCount()
{
return this.buddies.size();
}
/**
* Get the data item associated with the specified position in the data set.
*
* @param index
* Position of the item whose data we want within the adapter's
* data set.
* @return the Buddy object data at the specified position.
*/
@Override
public Buddy getItem(int index)
{
if (index <= getCount()) //IndexOutOfBoundsException fix
return this.buddies.get(index);
return this.buddies.get(getCount() - 1);
}
/**
* Get a View that displays the data at the specified position in the data
* set. You can either create a View manually or inflate it from an XML
* layout file. When the View is inflated, the parent View (GridView,
* ListView...) will apply default layout parameters unless you use
* inflate(int, android.view.ViewGroup, boolean) to specify a root view and
* to prevent attachment to the root.
*
* This method is used to generate views to be used in the ListView. This
* the method that defines how data will look and be represented throughout
* the UI.
*
* @param position
* The position of the item that is being placed / The position
* of the item within the adapter's data set of the item whose
* view we want.
*
* @param convertView
* The old view to reuse, if possible. Note: You should check
* that this view is non-null and of an appropriate type before
* using. If it is not possible to convert this view to display
* the correct data, this method can create a new view.
* Heterogeneous lists can specify their number of view types, so
* that this View is always of the right type (see
* getViewTypeCount() and getItemViewType(int))
*
* @param parent
* The parent that this view will eventually be attached to.
* @return the view that defines how this Buddy object is represented in the
* ListView / A View corresponding to the data at the specified
* position.
*
* @see {@link BaseAdapter#getView(int, View, ViewGroup)}
*/
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if (row == null)
{
// ROW INFLATION
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.simple_list_item_2, parent, false);
}
// Get item
Buddy buddy = getItem(position);
buddy.refresh();
buddyName = (TextView) row.findViewById(R.id.buddy_name); //change this to textField1 from simple_list_item_2
buddyName.setText(buddy.toString());
buddyStatus = (TextView) row.findViewById(R.id.buddy_mood); //change this to textField2 from simple_list_item_2
buddyStatus.setText(buddy.getMood());
// Log.d(tag, buddy.getIdentity()+"'s mood is "+buddyStatus.getText());
return row;
}
So i propose you expand the constructor with an additional ArrayList which contains the subtexts and then use em instead of the buddy.getMood() call.
Finally, instantiate this adapter and set it as the listView's adapter. Voila, you've got both texts showing up ;)
For further refinement, make your own XML file containing two textViews, like this.
and instead of
row = inflater.inflate(R.layout.simple_list_item_2, parent, false);
do
row = inflater.inflate(R.layout.buddy_list_item, parent, false);
There you go, now you know how to make adapters work with custom XMLs and listViews.