为什么我的listview没有显示任何元素?

时间:2015-07-01 20:39:12

标签: android listview android-fragments android-listview

我有ViewPager我希望显示一些ListViews。我使用的是具有自定义Fragment的单个ListView

这是我的片段XML(fragment_list_view):

<FrameLayout 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.popcristian.licenta.ListViewFragment" >

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

</FrameLayout>


这是我的列表项XML(fragment_list_item):

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

    <TextView 
        android:id="@+id/productTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="16dp"
        android:singleLine="true"
        android:textStyle="bold"
        android:textSize="17sp"
        android:text="TEST TITLE"/>

    <TextView 
        android:id="@+id/productDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="16dp"
        android:layout_below="@+id/productTitle"
        android:maxLines="2"
        android:textSize="13sp"
        android:text="some text" />

    <TextView 
        android:id="@+id/productCategory"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp"
        android:layout_marginStart="10dp"
        android:layout_below="@+id/productDescription"
        android:textSize="12sp"
        android:textStyle="italic"
        android:textColor="#C0C0C0"
        android:text="Category: Men / Jeans" />

    <TextView 
        android:id="@+id/productPrice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_alignParentEnd="true"
        android:textSize="17sp"
        android:textStyle="bold"
        android:textColor="#95C957"
        android:text="500$" />

</RelativeLayout>


这是我试图显示的碎片:

public class ListViewFragment extends Fragment {

    private ListView listView;
    private ProductsListViewAdapter adapter;
    private List<String> params;
    private int index;
    private String category, subCategory;
    private List<Product> products;
    private TextView productTitle, productDescription, productCategory, productPrice;

    public ListViewFragment(int index, String category, String subCategory, List<Product> products) {
        this.index = index;
        this.category = category;
        this.subCategory = subCategory;
        this.products = products;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_list_view, null);

        Product product = products.get(0);
        String title = product.getTitle();
        Log.d("Title", title); // prints out the title
        listView = (ListView) view.findViewById(R.id.productsListView);
        adapter = new ProductsListViewAdapter(getActivity(), products);
        listView.setAdapter(adapter);

        return inflater.inflate(R.layout.fragment_list_view, container, false);
    }

}


这是我的适配器:

public class ProductsListViewAdapter extends BaseAdapter{

    private Activity activity;
    private List<Product> products;
    private LayoutInflater inflater;

    public ProductsListViewAdapter(Activity activity, List<Product> products) {
        this.activity = activity;
        this.products = products;
    }

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

    @Override
    public Object getItem(int position) {
        return products.get(position);
    }

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

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

        if(inflater == null)
            inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if(convertView == null)
            convertView = inflater.inflate(R.layout.fragment_list_item, null);

        TextView title = (TextView) convertView.findViewById(R.id.productTitle);
        TextView description = (TextView) convertView.findViewById(R.id.productDescription);
        TextView category = (TextView) convertView.findViewById(R.id.productCategory);
        TextView price = (TextView) convertView.findViewById(R.id.productPrice);

        Product product = products.get(position);

        title.setText(product.getTitle());
        description.setText(product.getDescription());
        category.setText(product.getCategory());
        price.setText(product.getPrice());

        return convertView;
    }

}


当我运行此代码时,ListView中没有显示任何内容。我做错了什么?

1 个答案:

答案 0 :(得分:1)

你的片段正在回归:

return inflater.inflate(R.layout.fragment_list_view, container, false);

但是您的适配器设置在不同的视图中:

View view = inflater.inflate(R.layout.fragment_list_view, null);
Product product = products.get(0);
String title = product.getTitle();
Log.d("Title", title); // prints out the title
listView = (ListView) view.findViewById(R.id.productsListView);
adapter = new ProductsListViewAdapter(getActivity(), products);
listView.setAdapter(adapter);

永远不会被使用,因为会返回另一个视图。

return inflater.inflate(R.layout.fragment_list_view, container, false);更改为return view;