1. Check out our companion app, Forums for Android! Download from Google Play

setText on a list view not persistent when using simple adapter

Discussion in 'Application Development' started by kaancho, Aug 15, 2010.

  1. kaancho

    kaancho Member
    Thread Starter

    Jul 15, 2010
    I am using list view based on a simple adapter. each list item is composed of 3 text views. when I click an item in the list i use setText to set the "text" in one of the text views. When I scroll the list however the textview I set using setText seems to have disappeared.

    How do i make it persistent?


  2. johnnybeem

    johnnybeem New Member

    Aug 9, 2010
    Try updating the adapter data if there's a function to do that. Basically (as I understand it) when you call setText on the View, you are setting the text on the View *currently displaying on the screen*. But when you scroll, to save memory resources, Android might scrap the reference to that "old" View and then recreate a new one later by calling getView on the adapter. Therefore, getView is returning a new view based on the data in the adapter, not what you used for setText.

    If you can't find a function to update the data in the adapter, you might want to implement your own adapter (extend from BaseAdapter). It's pretty easy to do and there's an example here:

    Grid View | Android Developers

    Let me know if that works,
  3. andywhoa

    andywhoa Well-Known Member

    May 15, 2010
    Software Engineer
    West Des Moines, IA
    You need to manage the data stored in each row in the Adapter. When a list item is not visible, it is destroyed. The getView function gets called for every list item once it appears on screen. When you scroll and it goes off screen, it is destroyed and the process starts again. If you want the data to persist, you need to manage it behind the scenes.

    Here's a good tutorial where the user has defined an object called Order. Each row in the list represents an Order. The tutorial writer uses an ArrayList in an extended Adapter to hold the Orders for all rows. The getView grabs the order corresponding to that position and draws the information. In your case, upon clicking on the row, you would update Order item for that list position in the Adapter's items array to persist the data.

    Android Series: Custom ListView items and adapters | Software Passion

    By the way, the words "position" and "index" are not interchangeable in Adapters. "Position" is the location of the item is in the entire list. "Index" is the location of the item on the visible screen. This is why the first parameter of the getView method is called "position."

Share This Page