正如您所注意到的,layout_1 有一个 textView,它是默认使用的。layout_2 有两个文本视图 - 另一个用作子文本。
但这是诀窍 - 并非所有适配器都使用潜台词;)
我发现为任何事物编写一个专门构建的自定义适配器更容易(不是强制性的)......
例如,这是一个自定义适配器,它将使用这个 simple_list_item_2 显示名称和状态
这不会是复制/粘贴代码,但你会通过一些调整来修复它......
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;
}
因此,我建议您使用包含子文本的附加 ArrayList 扩展构造函数,然后使用 em 而不是 buddy.getMood() 调用。
最后,实例化这个适配器并将其设置为listView的适配器。瞧,您已经显示了两个文本;)
为了进一步细化,制作您自己的包含两个 textView 的 XML 文件,如下所示。
<?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" />
而不是
row = inflater.inflate(R.layout.simple_list_item_2, parent, false);
做
row = inflater.inflate(R.layout.buddy_list_item, parent, false);
好了,现在您知道了如何使适配器与自定义 XML 和 listView 一起工作。