Fragment中的ListView未显示

时间:2015-05-17 23:16:37

标签: android listview android-fragments android-listview

我有关于我的listview没有显示在我的片段中的问题。我已经检查了StackOverflow上的其他相关问题,但我还没有得到答案。除了这个用于显示列表视图的片段之外,其他片段的效果很好。下面是代码。片段xml仅包含列表视图。

    public class FeaturedIsh extends Fragment {
        ListView listview;

        String title[] ={"Quote of the week","Devotional","News","Events"};
        Integer titleImage[] = {R.drawable.user,R.drawable.user,R.drawable.user,R.drawable.user};
        String content[] = {"The more you do, the more you are able to do, the less you do, the less you are able to do","Today's topic : Careful for nothing", "Bukola Babies won again","Two weddings this Saturday"};

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            String title[] = {"Quote of the week","Devotional","News","Events"};
            Integer titleImage[] = {R.drawable.user,R.drawable.user,R.drawable.user,R.drawable.user};
            String content[] = {"The more you do, the more you are able to do, the less you do, the less you are able to do","Today's topic : Careful for nothing", "Bukola Babies won again","Two weddings this Saturday"};

            Log.d("yo", "onCreate worked");

        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.featuredish, container, false);
            listview = (ListView)v.findViewById(R.id.feat_list_view);
            Log.d("yo","inflater true");
            FeaturedAdapterListNav Falnav = new FeaturedAdapterListNav(getActivity(), title, titleImage, content);
            Log.d("yo","Adapter worked");
            listview.setAdapter(Falnav);
            Log.d("yo", "Adapter set");
            return v;
        }
    }

Here is my adapter code

package com.example.cozasocial;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.zip.Inflater;

/**
 * Created by SamuelAgbede on 5/12/2015.
 */
public class FeaturedAdapterListNav extends ArrayAdapter<String> {
    Activity context;
    String[] title;
    Integer[] titleImage;
    String[] content;

    public FeaturedAdapterListNav(Activity context, String[] title, Integer[] titleImage, String[] content) {
        super(context, R.layout.featured_each_row);

        this.context = context;
        this.title = title;
        this.titleImage = titleImage;
        this.content = content;


    }


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


        LayoutInflater inflater = context.getLayoutInflater();
        View theView = inflater.inflate(R.layout.featured_each_row, null, true);

        ImageView image = (ImageView) theView.findViewById(R.id.image_featured);
        TextView contents = (TextView) theView.findViewById(R.id.content);
        TextView headings = (TextView) theView.findViewById(R.id.heading);

        image.setImageResource(titleImage[position]);
        contents.setText(content[position]);
        headings.setText(title[position]);
            return theView;
    }
}

1 个答案:

答案 0 :(得分:0)

FeaturedAdapterListNav calss constuctor

写这个(需要通知适配器你的数据集大小)

super(context, R.layout.featured_each_row, title);

而不是

 super(context, R.layout.featured_each_row);