2017-02-17 113 views
-1

我試圖設置一個自定義ListView作爲表來顯示產品詳細信息。正如你在我的代碼中看到的,我使用一個自定義適配器和一個額外的xml文件來填充ListView。我的問題是ListView不是空的。沒有顯示任何項目,我沒有看到我的錯誤。你可以幫我嗎?Android自定義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> 
+0

該代碼甚至不應該編譯爲ProductDetailAdapter沒有實現BaseAdapter抽象類 – Selvin

回答

1

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 ...它非常良好格局BaseAdapter

2

添加getCount將方法在適配器

@Override 
public int getCount() { 
    return list.size(); 
} 
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

缺少類實現。 見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()不僅使項目可見。因爲它提供了 項目數量。

相關問題