ListView, ArrayAdapter, getView()


Last Updated:

  1. tunneling

    tunneling Member This Topic's Starter

    Joined:
    Mar 23, 2010
    Messages:
    6
    Likes Received:
    1
    I guess it's not too long after one get's started with android that you realize your going to have to override the getView() method of an ArrayAdapter.

    I've been struggling with this for hours, and so before I throw my phone against the wall.. can someone enlighten me?

    I'm basically using this tutorial

    Android Series: Custom ListView items and adapters | Software Passion

    I've added some Log.i trying to determine what's going on. All I can figure out is that the getView() method is never getting called.

    SoftwarePassionView
    Order
    row.xml
    main.xml
    Manifest
     

    Advertisement
  2. jon21569

    jon21569 Member

    Joined:
    Aug 10, 2008
    Messages:
    23
    Likes Received:
    7
    Take a look at this sample code.
     

    Attached Files:

  3. tunneling

    tunneling Member This Topic's Starter

    Joined:
    Mar 23, 2010
    Messages:
    6
    Likes Received:
    1
    i guess that i should have mentioned that i have had success using "android.R.layout.simple_list_item_1". although i haven't tried to @Override the getView() to customize the display.

    I still don't understand what's going wrong with the code i posted. I've zipped it up from the workspace if that makes it easier for someone to try.
     

    Attached Files:

  4. jon21569

    jon21569 Member

    Joined:
    Aug 10, 2008
    Messages:
    23
    Likes Received:
    7
    Check the main.xml file in the SoftwarePassionView project. If you make the following items wrap_content in the xml file you will see the list.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:eek:rientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView
    android:id="@+id/header_message"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/main_no_items"/>
    <ListView
    android:id="@+id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
    <TextView
    android:id="@+id/android:empty"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/main_no_items"/>
    </LinearLayout>
     
  5. tunneling

    tunneling Member This Topic's Starter

    Joined:
    Mar 23, 2010
    Messages:
    6
    Likes Received:
    1
    ahh.. my phone thanks you. so was the fill_parent smashing the view?
     
  6. tunneling

    tunneling Member This Topic's Starter

    Joined:
    Mar 23, 2010
    Messages:
    6
    Likes Received:
    1
    sorry for so many posts, but i just realized that before getView() wasn't being called.. how is it that changing the resource causes that method to be called?
     
  7. jon21569

    jon21569 Member

    Joined:
    Aug 10, 2008
    Messages:
    23
    Likes Received:
    7
    When the height was set android:layout_height="fill_parent"

    in

    <TextView
    android:id="@+id/header_message"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="@string/main_no_items"/>

    it pushed the layout

    <ListView
    android:id="@+id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />

    off the screen. Since the ListView was not visible android knew it would not be able to draw it on the screen so getView() method was never called to render the objects.
     
    tunneling likes this.
  8. iAmSun

    iAmSun New Member

    Joined:
    Mar 30, 2010
    Messages:
    1
    Likes Received:
    0
    As far as I know the getView() method is called when the Views are being drawn to the context, maybe after notifyDataSetChanged() has been called to refresh the attached View.

    By override that method you can modify the display of the list item ,e.g. if you want to change the image resource of an ImageView in your list item (if any) if a certain condition met.

    We don't need to call the method explicitly though. Hope this will help you :D
     
  9. beto

    beto New Member

    Joined:
    Jun 9, 2010
    Messages:
    1
    Likes Received:
    0
    Hi Guyz, I Want to add a simple_list_item_multiple_choice to your example, but some how i just can't make it work. can anyone plz help me on that ?
     
  10. skyxiao6688

    skyxiao6688 New Member

    Joined:
    Oct 31, 2010
    Messages:
    1
    Likes Received:
    0
    thanks!Take a look!
     
  11. Kavalec

    Kavalec New Member

    Joined:
    Apr 19, 2012
    Messages:
    1
    Likes Received:
    0
    Thank You! That took me exactly where I needed to go.
     
  12. mithrandiir42

    mithrandiir42 New Member

    Joined:
    Sep 23, 2011
    Messages:
    3
    Likes Received:
    0
    Wow, I struggled with this for a few days. that did the trick! Thank you!
     

Share This Page

Loading...