自定义 ListView 适配器不显示任何结果

Custom ListView Adapter displays no results

我正在使用 ListFragment 并通过从 BaseAdapter 扩展准备了自定义 ListView 适配器,我调用了在 Fragment 中设置适配器的方法,但 Listview 没有显示任何内容。 下面是我的自定义适配器和 ListFragment 类:

适配器:

package adapters;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sarham.kabs.fruity.R;

/**
 * Created by Kabunyi Wanja on 22/03/2015.
 */
public class CategoriesListAdapter extends BaseAdapter{
    private Context context;

    public CategoriesListAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return 0;
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CategoryViewHolder categoryViewHolder;
        if(convertView==null){
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.categories_list_custom_layout, parent, false);

            categoryViewHolder = new CategoryViewHolder();
            categoryViewHolder.category = (TextView)convertView.findViewById(R.id.tv_categories);
            convertView.setTag(categoryViewHolder);
        }
        else{
            categoryViewHolder = (CategoryViewHolder) convertView.getTag();
        }
        categoryViewHolder.category.setText("Category");
        //on testing, the below line never executes
        Log.d("ADAPTER", "Textview updated");

        return convertView;
    }
    //ViewHolder class
    public static class CategoryViewHolder{
        TextView category;
    }
}

列表片段:

package com.sarham.kabs.fruity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.support.v7.app.ActionBarDrawerToggle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import adapters.CategoriesListAdapter;

/**
 * Created by Kabunyi Wanja on 22/03/2015.
 */
public class CategoriesFragment extends ListFragment {
    private ListView categoriesListView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.categories_fragment, container, false);
        categoriesListView = (ListView)v.findViewById(android.R.id.list);
        setCategoriesListAdapter();
        Log.d("ADAPTER: ", "Adapter has been set for listview");

        return v;
    }
    //set adapter to categoriesListView
    public void setCategoriesListAdapter(){
        categoriesListView.setAdapter(new CategoriesListAdapter(getActivity()));
    }
}

来自您的适配器:

public int getCount() {
        return 0;
    }

getCount()方法returns零。这将导致 ListView 表现得好像没有数据一样。