我試圖設置一個自定義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>
該代碼甚至不應該編譯爲ProductDetailAdapter沒有實現BaseAdapter抽象類 – Selvin