自定义适配器不会调用getVIew

时间:2013-09-02 07:16:51

标签: adapter

这是我的自定义适配器的代码...它不会调用我的获取视图...只返回白色空白页面。我不知道是什么问题。有人可以提供帮助。非常感谢以前!

   public class ProductShopsAdapter extends ArrayAdapter<ProductShop> {

    private Context cntx;
    private ArrayList<ProductShop> shopValues;
    ProductShopsHolder holder = null;


    public ProductShopsAdapter(Context context, int textViewResourceId,  ArrayList<ProductShop> stringValues) {
        super(context,textViewResourceId);
        shopValues = stringValues;
        cntx = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            holder = new ProductShopsHolder();  

            LayoutInflater layoutInflater = (LayoutInflater)cntx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.shops_list, null);

            //Holder Elements initialization
            holder.setShopName((TextView)convertView.findViewById(R.id.shop_name));
            holder.setPrice((TextView)convertView.findViewById(R.id.shop_part_price));

            convertView.setTag(holder);
        } else {
            holder = (ProductShopsHolder) convertView.getTag();
        }
        holder.getShopName().setText(getItem(position).getShopName());
        holder.getPrice().setText(getItem(position).getPrice());

        return convertView;
    }
}

这是我的XML

 <?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ProductShopsActivity" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/footer"
        android:padding="0dp" >
    </ListView>

    <include
        android:id="@+id/footer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@android:id/list"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        layout="@layout/footer_menu" />

</RelativeLayout>

和单一视图:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_item"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#f2f2f2"
    android:orientation="horizontal"
    android:padding="10dp" >

    <TextView
        android:id="@+id/shop_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Technomarket"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/shop_part_price"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="30 лв."
        android:textSize="18sp" />

    <TextView
        android:id="@+id/details_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:drawableRight="@drawable/rightarrow"
        android:gravity="center"
        android:text="Детайли"
        android:textColor="#289bd6"
        android:textSize="14sp" />

1 个答案:

答案 0 :(得分:4)

ArrayList - ArrayAdapter内的ArrayAdapter可以存储项目本身。这就是getView()未被调用的原因 - getCount()的默认实现返回ArrayAdapter本身的项目数。如果您将商品保留在自己的ArrayList中,请至少覆盖getCount()以返回shopValues.getCount()。否则它始终为0并且您的适配器绑定到的ListView甚至不知道有任何项目可以完全显示。

相关问题