2015-05-17 62 views
1

我有我的列表視圖不會顯示在我的片段中的這個問題。我檢查了StackOverflow上的其他相關問題,但我沒有得到答案。除了這個旨在顯示列表視圖的片段外,其他片段運行良好。以下是代碼。片段xml只包含一個列表視圖。未顯示片段中的ListView

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; 
    } 
} 
+0

也許嘗試投入的ViewGroup佈局比如一個framelayout或者relativelayout來包圍ListView(你說fragment xml only conta在列表視圖中)。 –

+0

此外,發佈LogCat,如果發生錯誤 –

+0

我懷疑問題是與您的適配器。 – tachyonflux

回答

0

FeaturedAdapterListNav CALSS constuctor

寫本(需要告知您的數據集大小適配器)

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

,而不是

super(context, R.layout.featured_each_row); 
+0

謝謝,但做了之後,它顯示了這一點 - java.lang.RuntimeException:內容具有ID屬性'android.R.id.list',這不是一個ListView類 –

+0

如果您正在擴展'ListActivity' | 'ListFragment'你必須設置你的'Listview'視圖id爲'androi.R.id.list' – Bharatesh

+0

好吧。讓我檢查一下,謝謝 –