0

In my fragment I have a list. I want to display data on my list. This is adapter class:

@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;
    }

This is 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>

This is xml of fragment:

<?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>

When I run the application I cannot see my list but Log shows that getView() has been called.

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

What and where is my mistake(s)? Any suggestions would be appreciated. Thanks

Hesam
  • 52,260
  • 74
  • 224
  • 365
  • Can you also show the fragment's code? – fweigl Feb 28 '13 at 13:02
  • Thank you Ascorbin, it has so many lines of code and is waste of your time. based on my debug everything is fine and I also can see Log.e(TAG, "---"); in log cat so it shows getView() is working. I have no idea why it doesn't present data. Data is hard coded so it should be displayed! :( – Hesam Feb 28 '13 at 13:15

2 Answers2

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

Have a look at the video here. http://www.youtube.com/watch?v=wDBM6wVEO70. To get a clear idea of memory management have a look at this link http://www.youtube.com/watch?v=_CruQY55HOk.

Have a look at a similar question How to solve GC_concurrent freed?. Looks like your heap is full.

The above links should help you find your way out of trouble. Use a MAT Analyzer to check for any memory leaks.

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • It's not the case because if I change it to whatever integers there is no list item. I'm suspicious to garbage collector because I have so many lines of code that garbage collector is cleaning memory. – Hesam Feb 28 '13 at 13:11
  • If there is no memory for your app to run, you should get a outofmemory error. Android does mark and sweep. If garbabge collection occurs you see some messages in Logcat like pausing time for GC. – Raghunandan Feb 28 '13 at 13:15
0

Problem was because of stupid suggestion by lint that asked me to set layout_width to 0dp instead of match-parent. I change my xml code of activity (is not mentioned above) to something like this in 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>

Now, application is working fine.


Update

I found this post useful: Why is 0dp considered a performance enhancement?

Community
  • 1
  • 1
Hesam
  • 52,260
  • 74
  • 224
  • 365