Android Custom ListView没有商品

时间:2017-02-17 09:10:51

标签: android listview

我尝试将自定义ListView设置为表格以显示“产品详细信息”。正如您在我的代码中看到的,我使用自定义适配器和附加的xml文件来填充ListView。我的问题是ListView是空的。没有显示任何项目,我也没有看到我的错误。你能帮助我吗?

片段:

public class ProductDetail1Fragment extends Fragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_product_detail1,container,false);

    HashMap<String,String> mapProduct = new HashMap<String,String>();

    for(int i=0;i<10;i++) {
        mapProduct.put("Key" + i, "Value" + i);
    }

    ListView listView=(ListView) view.findViewById(R.id.productListview);
    ProductDetailAdapter adapter = new ProductDetailAdapter(getActivity(),mapProduct);
    listView.setAdapter(adapter);

    return view;


}

适配器:

public class ProductDetailAdapter extends BaseAdapter {
    private HashMap<String,String> list;
    private Context context;

public ProductDetailAdapter(Context c, HashMap<String,String> list){
    super();
    this.context = c;
    this.list=list;

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

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if(convertView == null){
        convertView=inflater.inflate(R.layout.product_detail_data_row,null);
    }
    TextView textViewKey = (TextView)convertView.findViewById(R.id.productDataKey);
    TextView textViewValue = (TextView)convertView.findViewById(R.id.productDataValue);


    textViewKey.setText("tst");
    textViewValue.setText("ddfadfs");

    return convertView;
}
}

fragment.xml之

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="com.parker.tfdeapp.ProductDetail2Fragment">

<ListView
android:id="@+id/productListview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>

</LinearLayout>

Listview_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/productDataKey"
    android:layout_height="wrap_content"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:textStyle="bold" />

<TextView
    android:id="@+id/productDataValue"
    android:layout_height="wrap_content"
    android:layout_width="0dp"
    android:layout_weight="1" />
</LinearLayout>

4 个答案:

答案 0 :(得分:2)

在适配器中添加getCount方法

@Override
public int getCount() {
    return list.size();
}

答案 1 :(得分:2)

在适配器类中添加此覆盖方法

 @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public ListData getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

答案 2 :(得分:2)

缺少BaseAdapter的类实现。 请参阅documentation

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public ListData getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
  

实施getCount()只会使项目可见。因为它提供   项目计数。

答案 3 :(得分:1)

1)需要更改ListView以匹配父高(其性能不佳):

@Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public ListData getItem(int index) {
        return mList.get(index);
    }

    @Override
    public long getItemId(int index) {
        return index;
    }

2)只是覆盖BaseAdapte中缺少的函数:

for(var i=1; i<=@totalCountItems; i++){

                            sb.Append("<div class=\"ibox-content\">"+
                                        "<div class=\"table-responsive\">"+
                                            "<table class=\"table shoping-cart-table\">"+
                                                "<tbody>"+
                                                    "<tr>"+
                                                        "<td width=\"90\">"+
                                                            "<div class=\"cart-product-imitation\">"+
                                                            "</div>"+
                                                        "</td>"+
                                                        "<td class=\"desc\">"+
                                                            "<h3>"+
                                                                "<a href=\"#\" class=\"text-navy\">"+
                                                                    Model[i].Name+
                                                                "</a>"+
                                                            "</h3>"+
                                                            "<p class=\"small\">"+
                                                                Model[i].DescriptionLong+
                                                            "</p>"+
                                                            "<dl class=\"small m-b-none\">"+
                                                                "<dt>Description lists</dt>"+
                                                                "<dd>"+Model[i].DescriptionSort+"</dd>"+
                                                            "</dl>"+
                                                            "<div class=\"m-t-sm\">"+
                                                                "<a href=\"#\" class=\"text-muted\"><i class=\"fa fa-gift\"></i> Add gift package</a>"+
                                                                "|"+
                                                                "<a href=\"#\" class=\"text-muted\"><i class=\"fa fa-trash\"></i> Remove item</a>"+
                                                            "</div>"+
                                                        "</td>"+
                                                        "<td>"+
                                                            "$"+Model[i].Price+
                                                            "<s class=\"small text-muted\">$"+Model[i].DPrice+"</s>"+
                                                        "</td>"+
                                                        "<td width=\"65\">"+
                                                            "<input type=\"text\" class=\"form-control\" placeholder=\"1\">"+
                                                        "</td>"+
                                                        "<td>"+
                                                            "<h4>"+
                                                                "$"+(Model[i].Price)*(@Model[i].Count)+
                                                            "</h4>"+
                                                        "</td>"+
                                                    "</tr>"+
                                            "</tbody>"+
                                        "</table>"+
                                    "</div>"+
                            "</div>");
                        }}
                    $("iboxTest").html(@sb);
                }

3)使用JSON-LD: Using data:post.body in Blogger template ......非常好的模式