Android Custom ListView没有项目



我尝试设置一个自定义列表视图作为表显示产品详细信息的表。如您在我的代码中所见,我使用自定义适配器和附加的XML文件来填充ListView。我的问题是listView ist空。没有显示的项目,我看不到我的错误。你能帮助我吗?

片段:

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>

在适配器中添加getCount方法

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

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

 @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;
    }

缺少BaseAdapter的类实现。请参阅文档

    @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()仅使项目可见。因为它提供了 项目计数。

1)需要更改listView以匹配父高度(其不良性能):

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

2)仅覆盖baseadapte中缺少功能:

@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;
    }

3)使用Viewholder ...它非常好的模式