我正在尝试制作列表视图,每行包含一个imageview和两个文本视图。显示图像和第二个textview,但第一个textview是空白的...无法弄清楚出了什么问题...我无法绕过自定义列表适配器...我已阅读http://commonsware.com/Android/excerpt.pdf到
我的数据正确加载到arraylist。 我的代码:
private class SongAdapter extends ArrayAdapter<Szam> {
private ArrayList<Szam> items;
public SongAdapter(Context context, int textViewResourceId, ArrayList<Szam> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
Szam o = items.get(position);
if (o != null) {
TextView tt = (TextView) v.findViewById(R.id.firstLine);
TextView bt = (TextView) v.findViewById(R.id.secondLine);
if (tt != null) {
tt.setText(o.getArtist()); }
if(bt != null){
bt.setText(o.getTitle());
}
}
return v;
}
我的xml布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:minHeight="?android:attr/listPreferredItemHeight"
android:layout_height="wrap_content" android:padding="6dip">
<TextView android:id="@+id/secondLine" android:layout_width="fill_parent"
android:layout_height="26dip" android:layout_toRightOf="@+id/icon"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true" android:singleLine="true"
android:ellipsize="marquee"
android:text="Simple application that shows how to use RelativeLayout" />
<TextView android:id="@+id/firstLine" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_toRightOf="@+id/icon"
android:layout_alignParentRight="true" android:layout_alignParentTop="true"
android:layout_above="@id/secondLine"
android:layout_alignWithParentIfMissing="true" android:gravity="center_vertical"
android:text="My Application" />
<ImageView android:layout_marginRight="6dip" android:id="@+id/icon"
android:src="@drawable/play" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_alignParentLeft="true"></ImageView>
</RelativeLayout>
更正的布局是:
<TextView android:id="@+id/secondLine1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_toRightOf="@+id/icon"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true" android:singleLine="true"
android:ellipsize="marquee"
android:textColor="#ffffff"
android:textSize="12sp"
android:text="Simple application that shows how to use RelativeLayout" />
<TextView android:id="@+id/firstLine1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_toRightOf="@+id/icon"
android:layout_alignParentRight="true" android:layout_alignParentTop="true"
android:ellipsize="marquee"
android:textColor="#ffffff"
android:textSize="16sp"
android:layout_alignWithParentIfMissing="true"
android:text="My Application" />
<ImageView android:layout_marginRight="6dip" android:id="@+id/icon"
android:src="@drawable/play" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_alignParentLeft="true"></ImageView>
答案 0 :(得分:1)
你不能使用LinearLayouts吗?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"
android:minHeight="?android:attr/listPreferredItemHeight" android:layout_height="wrap_content" android:padding="6dip">
<ImageView android:layout_marginRight="6dip" android:id="@+id/icon" android:src="@drawable/play" android:layout_width="wrap_content"
android:layout_height="fill_parent"></ImageView>
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:orientation="vertical"
android:layout_height="wrap_content">
<TextView android:text="My Application" android:layout_height="wrap_content" android:gravity="center_vertical" android:id="@+id/firstLine"
android:layout_width="fill_parent"></TextView>
<TextView android:singleLine="true" android:text="Simple application that shows how to use RelativeLayout" android:layout_height="wrap_content"
android:ellipsize="marquee" android:id="@+id/secondLine" android:layout_width="fill_parent"></TextView>
</LinearLayout>
</LinearLayout>