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_item_1 and android.R.layout.simple_list_item_2 are layout which is define in android itself.
in android.R.layout.simple_list_item_1 only contain only one textview but android.R.layout.simple_list_item_2 contain two text view.
i want to example for android.R.layout.simple_list_item_2 ...how to show two text view in listview with adapter.
my code is
package com.app.listview;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ExampleListViewActivity extends Activity {
private String[] nameArr = new String[]{"Arun","Anil","Ankit","Manoj"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView)findViewById(R.id.lv);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
android.R.id.text1,
nameArr);
listView.setAdapter(adapter);
}
}
As mentioned by Klap "android.R.layout.simple_list_item_1 is a reference to an built-in XML layout document that is part of the Android OS" All the layouts are located in: sdk\platforms\android-xx\data\res\layout. To view the XML of layout : Eclipse: Simply type android. R.
In Android R is an Java-class that is auto-generated from your resources by the build process. The R. layout member is a auto-generated class that contains all IDs for layouts.
simple_spinner_item is the layout of each drop-down item on the spinner list. And inorder to house these x number of drop-down items, the layout required is simple_spinner_dropdown_item.
The difference is the following. simple_list_item_1
contains only a TextView
, whereas simple_list_item_2
has two inside a subclass of RelativeLayout
. These are both taken from Jelly Bean.
simple_list_item_1
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
android:paddingRight="?android:attr/listPreferredItemPaddingRight"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
/>
simple_list_item_2
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:mode="twoLine"
>
<TextView android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="?android:attr/listPreferredItemPaddingLeft"
android:layout_marginTop="8dip"
android:textAppearance="?android:attr/textAppearanceListItem"
/>
<TextView android:id="@android:id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@android:id/text1"
android:layout_alignLeft="@android:id/text1"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
</TwoLineListItem>
According to the docs for ArrayAdapter:
By default this class expects that the provided resource id references a single TextView.
So by default, an ArrayAdapter
doesn't automatically fill in multiple TextView
instances. You can, however, override the getView()
method and fill in the two TextView
s that appear in R.layout.simple_list_item_2
I found this to be the simplest answer to your question:
ArrayAdapter adapter = new ArrayAdapter(context, android.R.layout.simple_list_item_2, android.R.id.text1, list) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text1 = (TextView) view.findViewById(android.R.id.text1);
TextView text2 = (TextView) view.findViewById(android.R.id.text2);
text1.setText(person[position].getName());
text2.setText(person[position].getAge());
return view;
}
};
If you didn't notice: the trick is to supply android.R.id.text1
as (principally unneccessary) parameter to ArrayAdapter, otherwise the call to super
will cause an exception.
Also, this solution does not need an Inflater
or make use of TwoLineListItem
, which was deprecated in API 17.
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<Buddy>
{
private static final String tag = "BuddyArrayAdapter";
private Context context;
private TextView buddyName;
private TextView buddyStatus;
private List<Buddy> buddies = new ArrayList<Buddy>();
/**
* The default constructor which is invoked to create the buddy array
* adapter.
* <p>
* 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}<T>
*/
public BuddyArrayAdapter(Context context, int textViewResourceId, List<Buddy> 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.
* <p>
* 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.
* <p>
* @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))
* <p>
* @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.
<?xml version="1.0" encoding="utf-8"?>
<com.skype.widget.CheckableLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckedTextView
android:id="@+id/buddy_name"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:checkMark="?android:attr/textCheckMark"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:text="@string/buddy_name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/buddy_mood"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/empty_string"
android:layout_marginLeft="-350dp"
android:layout_marginTop="16dp"
android:gravity="center_vertical|bottom"
android:textAppearance="?android:attr/textAppearanceSmall" />
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.
messages - is a List<Map<String, String>>
,
title and data - is keys of map.
SimpleAdapter adapter = new SimpleAdapter(this, messages,
android.R.layout.simple_list_item_2,
new String[] {"title", "data"},
new int[] {android.R.id.text1,
android.R.id.text2,
});
list.setAdapter(adapter);
Thats all you need.
An ArrayAdapter only knows how to deal with one TextView per row. If you want it to deal with more you need to deal with that yourself by subclassing ArrayAdapter and overriding the getView()
method.
Depending on how you are creating your array, there may be another answer as well.
If the array is being created from a DB (you show a hard-coded string array, but that might just be for your example as far as I know) and you are not constrained to the array by some other factor, you might consider using a CursorAdapter as they are already set up to deal with multiple TextViews without having to subclass the adapter, and it would save you the processing power of turning DB data into an array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With