0

I'm facing a problem that my getView() starts recycling even when the adapter is set in listview for the first time, I have 5 items visible for the first time and due to the requirement i have to set the listview height with a constant value(even though I tried changing to fill_parent but no effect) i have searched and found this same as mine problem link! i have read it thouroughly and came to know that this kind of problem is because the listview height should not be set to wrap_content i have even try that but still no effect my log cat says

I/System.out(17411): getview 0 null
I/System.out(17411): getview 1 android.widget.RelativeLayout@40574ce0
I/System.out(17411): getview 2 android.widget.RelativeLayout@40574ce0
I/System.out(17411): getview 3 android.widget.RelativeLayout@40574ce0
I/System.out(17411): getview 4 android.widget.RelativeLayout@40574ce0
I/System.out(17411): getview 5 android.widget.RelativeLayout@40574ce0
I/System.out(17411): getview 0 android.widget.RelativeLayout@40574ce0
I/System.out(17411): getview 1 null
I/System.out(17411): getview 2 null
I/System.out(17411): getview 3 null
I/System.out(17411): getview 4 null

when adapter is set for the first time, (I didn't even scroll it but still it recycling don't know why) here is my layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/newDramaLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/background" >

 <RelativeLayout
    android:id="@+id/header" 
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:visibility="gone"
    android:background="@drawable/header">

    <Button 
        android:id="@+id/refreshBt"
        android:layout_width="55dp"
        android:layout_height="50dp"
        android:onClick="refresh"
        android:layout_alignParentRight="true"
        android:background="@drawable/refresh"/>
    </RelativeLayout>

   <LinearLayout
       android:id="@+id/info"
       android:layout_width="match_parent"
       android:layout_height="30dp"
       android:visibility="gone"
       android:gravity="center_horizontal"
       android:layout_below="@+id/header"
       android:background="@drawable/information"
       android:paddingLeft="10dp" >

       <TextView
           android:id="@+id/label"
           android:layout_width="wrap_content"
           android:layout_marginBottom="5dp"
           android:layout_height="wrap_content"
           android:text="@string/label"
           android:textColor="@android:color/secondary_text_dark" />
   </LinearLayout>

<ListView 
    android:id="@+id/newDramasList"
    android:layout_below="@+id/info"
    android:cacheColorHint="#00000000"
    android:layout_width="match_parent"
    android:layout_centerInParent="true"
    android:dividerHeight="5dp"
    android:divider="@android:color/transparent"
    android:layout_height="320dp"
    android:visibility="gone">

</ListView>

<ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone"
    android:layout_centerInParent="true" />

    <LinearLayout
    android:id="@+id/footer" 
    android:layout_below="@+id/newDramasList"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:visibility="gone"
    android:background="@drawable/footer"/>

   </RelativeLayout>

the listview I have try making its height fill_parent but problem still not fixed. here is my code

    $@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
    System.out.println("getview "+position+" "+convertView);
    View row=convertView;

    if(row==null)
    {
        LayoutInflater inflater=((Activity)context).getLayoutInflater();
        row=inflater.inflate(layoutResourceId, parent,false);

        holder=new NewDramas();
        holder.ivIcon=(ImageView)row.findViewById(R.id.dramaIcon);
        holder.tvDramaName=(TextView)row.findViewById(R.id.dramaTitle);
        holder.tvDate=(TextView)row.findViewById(R.id.date);

        row.setTag(holder);
    }
    else
    {
        holder=(NewDramas)row.getTag();
    }

    holder.ivIcon.setImageResource(R.drawable.new_drama_icon);
    holder.tvDramaName.setText(dramaList.get(position).getDramaName());
    holder.tvDate.setText(dramaList.get(position).getDate());

    return row;
}

here is the inflated layout

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="280dp"
    android:layout_height="70dp"
    android:orientation="vertical"
    android:background="@drawable/new_dramas_list_back" >

    <ImageView 
        android:id="@+id/dramaIcon"
        android:layout_width="50dp"
        android:layout_height="45dp"
        android:src="@drawable/favorite_icon"/>
      <Button 
        android:id="@+id/fav"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/dramaIcon"
        android:visibility="gone"
        android:focusable="false"
        android:layout_alignRight="@+id/dramaIcon"
        android:background="@drawable/heart"
        /> 

    <TextView
        android:id="@+id/dramaTitle"
        android:layout_toRightOf="@+id/dramaIcon"
        android:layout_alignTop="@+id/dramaIcon"
        android:layout_width="wrap_content"
        android:width="150dp"
        android:paddingTop="5dp"
        android:textColor="@android:color/white"
        android:ellipsize="end"
        android:layout_marginLeft="32dp"
        android:layout_height="wrap_content"
        android:text="title"
        android:textStyle="bold" />
  <LinearLayout
        android:id="@+id/updateDate" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@+id/dramaIcon"
        android:layout_alignParentRight="true"
        android:layout_marginTop="2dp" >  
    <TextView 
        android:id="@+id/updated"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="updated:"
        android:textColor="#ffff00"
        android:layout_alignBottom="@+id/dramaIcon"/>

    <TextView
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ffff00"
        android:layout_marginLeft="1dp"
        android:text="12/08/2102" />
</LinearLayout>    

</RelativeLayout>

I also added a button as a footerView in my listview? I hope this ain't the reason of this strange behaviour and some thing strange I have noticed that in logcat the relative id for the getview 1 is same repeating for the 5 times, that means there is problem with position 1 in my list view

can any one please help me understand this weird behaviour, thanks in advance.

Community
  • 1
  • 1
Muhammad Babar
  • 8,084
  • 5
  • 39
  • 56
  • I'm not expert in Android UI, but what I see from your logcat List is reusing same View for 5 first row. So List thinks that every row is not visible – Eugen Martynov Dec 31 '12 at 12:22
  • you should share inflated layout with us to let us say something. – Talha Dec 31 '12 at 12:28
  • @talhakosen i have added the inflated layout you can check it now – Muhammad Babar Dec 31 '12 at 12:35
  • @EugenMartynov yes thats the wierd behaviour of getView() im not understanding – Muhammad Babar Dec 31 '12 at 12:39
  • @Muhammad NewDramas() what are there in this class ? – Talha Dec 31 '12 at 12:47
  • @talhakosen it basically the holder class of the Views in the adapter – Muhammad Babar Dec 31 '12 at 12:50
  • 1
    @MuhammadBabar: [RomainGuy](http://stackoverflow.com/a/2639159/593709) is android engineer at google. He've said that ListView should be of `fill_parent` or specific height (like you said) but shouldn't be `wrap_content`. So you don't need to worry about how many times and in which order it will call getView() or `getView()` is returning `null` view sometimes. your code and implementation is correct. so don't worry and go ahead. – Adil Soomro Dec 31 '12 at 13:58
  • @AdilSoomro i know that i have some images in my list row,and on click i need to change images,and the getView() problem is going to hurt me then,and can i set `layout_height` and `layout_width` with some constant values like 280dp is it ok,as Romain Guy said they shouldn't be `wrap_content` – Muhammad Babar Dec 31 '12 at 17:37
  • Mr.Babar can you please answer my question on http://stackoverflow.com/questions/7156752/multiple-screen-resolution/21096591#21096591 – androiddev. Jan 13 '14 at 16:46
  • @androiddev. there isn't any question by you! – Muhammad Babar Jan 15 '14 at 05:34

1 Answers1

0

android:layout_weight="1" Try to set it to the ListView and what you want it to be displayed yet. (Sorry for english :P)

Maury
  • 604
  • 2
  • 8
  • 25