ListView, ArrayAdapter, getView()

Discussion in 'Application Development' started by tunneling, Mar 23, 2010.

  1. tunneling

    tunneling Member
    6

    Mar 23, 2010
    6
    1
    6
    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
    16

    Aug 10, 2008
    23
    7
    16
    Take a look at this sample code.
     

    Attached Files:

  3. tunneling

    tunneling Member
    6

    Mar 23, 2010
    6
    1
    6
    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
    16

    Aug 10, 2008
    23
    7
    16
    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
    6

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

    tunneling Member
    6

    Mar 23, 2010
    6
    1
    6
    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
    16

    Aug 10, 2008
    23
    7
    16
    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
    5

    Mar 30, 2010
    1
    0
    5
    Still a student
    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
    5

    Jun 9, 2010
    1
    0
    5
    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
    5

    Oct 31, 2010
    1
    0
    5
    thanks!Take a look!
     
  11. Kavalec

    Kavalec New Member
    5

    Apr 19, 2012
    1
    0
    5
    Thank You! That took me exactly where I needed to go.
     
  12. mithrandiir42

    mithrandiir42 New Member
    5

    Sep 23, 2011
    3
    0
    5
    Wow, I struggled with this for a few days. that did the trick! Thank you!
     

Share This Page

Loading...