2013-02-28 78 views
0

在我的片段中,我有一个列表。我想在我的列表中显示数据。 这是适配器类:Android,无法显示列表项目

@Override 
    public int getCount() { 
     10; 
    } 

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

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

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     Log.e(TAG, "---"); 
     if (convertView == null) { 
      convertView = myInflater.inflate(R.layout.list_topicslist, null); 
      holder  = new ViewHolder(); 

      holder.tvTitle   = (TextView)  convertView.findViewById(R.id.tvTitle); 
      holder.tvExcerpt  = (TextView)  convertView.findViewById(R.id.tvExcerpt); 
      holder.tvDate   = (TextView)  convertView.findViewById(R.id.tvDate); 
      holder.tvAuthor   = (TextView)  convertView.findViewById(R.id.tvAuthor); 
      holder.tvComment_Count = (TextView)  convertView.findViewById(R.id.tvComment_Count); 

      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     holder.tvTitle.setText("Title"); 
     holder.tvExcerpt.setText("Content"); 
     holder.tvAuthor.setText("Hesam"); 
     holder.tvComment_Count.setText("Votes:"); 
     holder.tvDate.setText("1/1/2013"); 

     return convertView; 
    } 

这是list_topicslist.xml:

<?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" 
     android:id="@+id/rlContainer" 
     android:background="@color/White" > 


    <RelativeLayout 
      android:id="@+id/rlHeader" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="25dp" android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" android:background="@color/bg_list_title"> 
     <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/title_author" 
       android:id="@+id/tvAuthor" android:layout_alignParentRight="true" android:layout_centerVertical="true" 
       android:layout_margin="@dimen/sideMargin" android:lines="1" android:maxLines="1"/> 
     <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/title_date" 
       android:id="@+id/tvDate" android:layout_alignParentLeft="true" 
       android:layout_margin="@dimen/sideMargin" android:maxLines="1" 
       android:lines="1" android:layout_centerVertical="true"/> 
    </RelativeLayout> 

    <RelativeLayout 
      android:id="@+id/rlBody" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="100dp" 
      android:layout_below="@id/rlHeader" android:clickable="false"> 

     <ImageView 
       android:layout_width="100dp" 
       android:layout_height="100dp" 
       android:id="@+id/ivPostImage" android:layout_alignParentRight="true" 
       android:layout_alignParentTop="true" android:layout_margin="@dimen/sideMargin" 
       android:src="@drawable/ic_loading_green"/> 
     <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/title_title" 
       android:id="@+id/tvTitle" android:layout_toLeftOf="@+id/ivPostImage" 
       android:layout_alignTop="@+id/ivPostImage" android:layout_margin="@dimen/sideMargin" android:lines="1" 
       android:maxLines="3"/> 
     <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/title_excerpt" 
       android:id="@+id/tvExcerpt" android:layout_toLeftOf="@+id/ivPostImage" 
       android:layout_below="@+id/tvTitle" android:layout_margin="@dimen/sideMargin" android:lines="1" 
       android:maxLines="6"/> 
    </RelativeLayout> 

    <RelativeLayout 
      android:id="@+id/rlFooter" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="25dp" 
      android:layout_below="@id/rlBody" 
      android:background="@color/bg_list_footer"> 
     <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/title_comment_count" 
       android:id="@+id/tvComment_Count" android:layout_alignParentRight="true" 
       android:layout_alignParentTop="true" android:layout_margin="@dimen/sideMargin" 
       android:layout_centerVertical="true" android:lines="1" android:maxLines="1"/> 
    </RelativeLayout> 

</RelativeLayout> 

这是片段的XML:

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" > 

    <ListView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/lvTopicsList" 
      android:scrollbars="none" 
      android:scrollingCache="false" 
      tools:listitem="@layout/list_topicslist" /> 
</RelativeLayout> 

当我运行应用程序,我看不到我的清单,但日志显示getView()已被调用。

02-28 21:09:10.195: E/*** TopicAdapter ***(16184): --- 
02-28 21:09:10.220: D/dalvikvm(16184): GC_CONCURRENT freed 2720K, 33% free 11321K/16775K, paused 2ms+1ms 
02-28 21:09:10.235: E/*** TopicAdapter ***(16184): --- 
02-28 21:09:10.235: E/*** TopicAdapter ***(16184): --- 
02-28 21:09:10.235: E/*** TopicAdapter ***(16184): --- 
02-28 21:09:10.240: E/*** TopicAdapter ***(16184): --- 
02-28 21:09:10.240: E/*** TopicAdapter ***(16184): --- 
02-28 21:09:10.250: E/*** TopicAdapter ***(16184): --- 
02-28 21:09:10.275: E/*** TopicAdapter ***(16184): --- 
02-28 21:09:10.295: D/dalvikvm(16184): GC_CONCURRENT freed 262K, 31% free 11693K/16775K, paused 2ms+2ms 

什么和我的错误在哪里? 任何建议,将不胜感激。谢谢

+0

你能还显示片段的代码? – FWeigl 2013-02-28 13:02:40

+0

谢谢Ascorbin,它有很多代码行,浪费你的时间。基于我的调试一切都很好,我也可以看到Log.e(标签,“---”);在日志猫,所以它显示getView()正在工作。我不知道为什么它不显示数据。数据是硬编码的,所以应该显示! :( – Hesam 2013-02-28 13:15:37

回答

0
@Override 
public Object getItem(int position) { 
return position;// change null to position 
} 

看看这里的视频。 http://www.youtube.com/watch?v=wDBM6wVEO70。 要想清楚内存管理的概念,请看这个链接http://www.youtube.com/watch?v=_CruQY55HOk

看看类似的问题How to solve GC_concurrent freed?。看起来你的堆满了。

上述链接应该可以帮助您找到摆脱困境的方法。使用MAT分析器来检查是否有内存泄漏。

+0

这不是这种情况,因为如果我改变它为任何整数没有列表项。我对垃圾收集器很可疑,因为我有太多的代码行,垃圾收集器正在清理内存。 – Hesam 2013-02-28 13:11:20

+0

如果没有内存你的应用程序需要运行,你应该得到一个outofmemory错误,Android会标记和扫描,如果garbabge集合发生,你会在Logcat中看到一些消息,例如暂停GC的时间。 – Raghunandan 2013-02-28 13:15:04

0

问题是因为lint的愚蠢建议,要求我将layout_width设置为0dp而不是match-parent。我改变活动的我的XML代码(不是上面提到的),以这样的事情在activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

    <fragment 
      android:name="com.kamalan.fragments.Fragment_TopicsList" 
      android:id="@+id/fragment" 
      android:layout_width="match_parent" <================== 
      android:layout_height="match_parent" 
      android:layout_gravity="left|center_vertical" /> 
</LinearLayout> 

现在,应用程序工作正常。


更新

我发现这个职位有用:Why is 0dp considered a performance enhancement?