好吧,我有ListViewAdapter.java
,看起来像是:
ListViewAdapter.java
public class ListViewItem {
public final Drawable icon; // the drawable for the ListView item ImageView
public final String title; // the text for the ListView item title
public final String precio; // the price for the ListView item
public final String descuento; // the price for the discount for the ListView item
// the text for the ListView item description
public ListViewItem(Drawable icon, String title, String precio, String descuento) {
this.icon = icon;
this.title = title;
this.precio = precio;
this.descuento = descuento;
}
}
当我在Fragment上创建ListView时没有说错了......代码是:
// initialize the items list
mItems = new ArrayList<ListViewItem>();
Resources resources = getResources();
mItems.add(new ListViewItem(resources.getDrawable(R.drawable.tomate_oferta), getString(R.string.aim), getString(R.string.aim_precio), getString(R.string.aim_descuento)));
mItems.add(new ListViewItem(resources.getDrawable(R.drawable.levadura_oferta), getString(R.string.youtube), getString(R.string.youtube_precio), getString(R.string.youtube_descuento)));
mItems.add(new ListViewItem(resources.getDrawable(R.drawable.sopa_oferta), getString(R.string.bebo), getString(R.string.bebo_precio), getString(R.string.bebo_descuento)));
mItems.add(new ListViewItem(resources.getDrawable(R.drawable.zumo_oferta), getString(R.string.pew), getString(R.string.pew_precio), getString(R.string.pew_descuento)));
// initialize and set the list adapter
setListAdapter(new ListViewDemoAdapter(getActivity(), mItems));
}
最后我的xml看起来像:
ListViewItem.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- the parent view - provides the gray background -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center_vertical"
android:background="@color/frame_background"
android:padding="5dp" >
<!-- the innner view - provides the white rectangle -->
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/frame" >
<!-- the icon view -->
<ImageView android:id="@+id/ivIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:contentDescription="@string/icon_content_description"
android:scaleType="fitXY"
android:layout_alignParentLeft="true" />
<!-- the container view for the title and description -->
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/ivIcon"
android:layout_centerVertical="true" >
<!-- the title view -->
<TextView android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#0000FF"
android:textAppearance="@android:style/TextAppearance.Medium" />
<!-- the description view -->
<TextView android:id="@+id/tvDiscount"
android:layout_below="@id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF0000"
android:textSize="12dp"
android:textAppearance="@android:style/TextAppearance.Small" />
<TextView android:id="@+id/tvPrice"
android:layout_below="@id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF0000"
android:textAppearance="@android:style/TextAppearance.Small" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
ListViewDemoAdapter.java
public class ListViewDemoAdapter extends ArrayAdapter<ListViewItem> {
public ListViewDemoAdapter(Context context, List<ListViewItem> items) {
super(context, R.layout.listview_item, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null) {
// inflate the GridView item layout
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.listview_item, parent, false);
// initialize the view holder
viewHolder = new ViewHolder();
viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
viewHolder.tvPrice = (TextView) convertView.findViewById(R.id.tvPrice);
viewHolder.tvDiscount = (TextView) convertView.findViewById(R.id.tvPrice);
convertView.setTag(viewHolder);
} else {
// recycle the already inflated view
viewHolder = (ViewHolder) convertView.getTag();
}
// update the item view
ListViewItem item = getItem(position);
viewHolder.ivIcon.setImageDrawable(item.icon);
viewHolder.tvTitle.setText(item.title);
viewHolder.tvDiscount.setText(item.descuento);
viewHolder.tvPrice.setText(item.precio);
return convertView;
}
private static class ViewHolder {
ImageView ivIcon;
TextView tvTitle;
TextView tvDiscount;
TextView tvPrice;
}
}
结果:
我不知道我做错了什么......我的主要问题是XML
,但我看不到。
希望你们能帮助我。感谢。
答案 0 :(得分:3)
你的问题似乎出现在你的layout_below属性中:
每个textView都设置为layout_below =&#34; @ id / tvTitle&#34;,但是每个其他textView都被tvPrice隐藏。你必须这样改变:
<!-- the title view -->
<TextView android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#0000FF"
android:textAppearance="@android:style/TextAppearance.Medium" />
<!-- the description view -->
<TextView android:id="@+id/tvDiscount"
android:layout_below="@id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF0000"
android:textSize="12dp"
android:textAppearance="@android:style/TextAppearance.Small" />
<TextView android:id="@+id/tvPrice"
android:layout_below="@id/tvDiscount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF0000"
android:textAppearance="@android:style/TextAppearance.Small" />
在适配器中,您引用了具有相同ID _
的两个视图 viewHolder.tvPrice = (TextView) convertView.findViewById(R.id.tvPrice);
viewHolder.tvDiscount = (TextView) convertView.findViewById(R.id.tvPrice);
您必须将其更改为:
viewHolder.tvDiscount = (TextView) convertView.findViewById(R.id.tvDiscount);
答案 1 :(得分:2)
我看到了2个错误:
首先是布局,tvprice和tvdiscount重叠,因为它们显示在另一个上面。
<!-- the description view -->
<TextView android:id="@+id/tvDiscount"
android:layout_below="@id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF0000"
android:textSize="12dp"
android:textAppearance="@android:style/TextAppearance.Small" />
<TextView android:id="@+id/tvPrice"
android:layout_below="@id/tvDiscount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF0000"
android:textAppearance="@android:style/TextAppearance.Small" />
其次,您没有设置电视折扣价值:
// update the item view
ListViewItem item = getItem(position);
viewHolder.ivIcon.setImageDrawable(item.icon);
viewHolder.tvTitle.setText(item.title);
viewHolder.tvPrice.setText(item.precio);
viewHolder.tvDiscount.setText(item.descuento);
另一个问题是viewHolder.tvDiscount指向tvPrice,更正如下:
viewHolder.tvDiscount = (TextView) convertView.findViewById(R.id.tvDiscount);
答案 2 :(得分:0)
将您的xml更改为此。使用@ + id / tvTitle代替@ id / tvTitle
<!-- the innner view - provides the white rectangle -->
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/frame" >
<!-- the icon view -->
<ImageView android:id="@+id/ivIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:contentDescription="@string/icon_content_description"
android:scaleType="fitXY"
android:layout_alignParentLeft="true" />
<!-- the container view for the title and description -->
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/ivIcon"
android:layout_centerVertical="true" >
<!-- the title view -->
<TextView android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#0000FF"
android:textAppearance="@android:style/TextAppearance.Medium" />
<!-- the description view -->
<TextView android:id="@+id/tvDiscount"
android:layout_below="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF0000"
android:textSize="12dp"
android:textAppearance="@android:style/TextAppearance.Small" />
<TextView android:id="@+id/tvPrice"
android:layout_below="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF0000"
android:textAppearance="@android:style/TextAppearance.Small" />
</RelativeLayout>
</RelativeLayout>