ArrayAdapter and ListView help!


Last Updated:

  1. LeonR

    LeonR Member This Topic's Starter

    Joined:
    Feb 22, 2010
    Messages:
    18
    Likes Received:
    0
    I am at a dead end, I just can't quite seem to figure out exactly what I need to do.
    What I want is simply to put a control (progres bar, button etc) into a listview. Thats it!

    I have tried all the 'simple' examples for adding a string array, but that obviously doesn't help me add controls.


    Ideally, what I would like to do is create a table layout dynamically (which I can do), and then add that to each element of the ListView..

    Any ideas would be really appreciated, even if its just a simple 'this is how you add a button to a listview' example.

    Thanks!
     

    Advertisement
  2. alostpacket

    alostpacket Over Macho Grande? VIP Member

    Joined:
    Nov 29, 2009
    Messages:
    7,978
    Likes Received:
    3,603
    Can you post some code of where your are stuck? Have you been able to create a view for each list item?


    Basically your adapter class should have a getView() method that returns a view for a list item. In that "list item" view you should be able to add your progress bar pretty easily.



    Adapter | Android Developers

    The ListView will call getView() on your adapter for each item that is displayed on the page. This is important because you will want to cache your views and if you have a view that just went off the page and out of the user's sight, you'll want to reuse it by resetting it's variables and properties but not re-instantiating it. You can do this easily with getView because when the main ListView calls getView() on your adapter it will pass in the old view that just went off the page if there is one available. That is what the "convertView" parameter is.


    Code (Text):
    1.  
    2.      /**
    3.      * @param convertView
    4.      *            The old view to overwrite, if one is passed by the main ListView (which is done automatically by the Android OS)
    5.      * @param mItems
    6.      *            the ArrayList of data in our adapter, the list item needs the
    7.      *            context and data passed to it when it is constructed so it knows what to display
    8.      * @returns a ListItemView that holds wraps around a list item
    9.      */
    10.     public View getView ( int position, View convertView, ViewGroup parent )
    11.     {
    12.  
    13.         ListItemView liv;
    14.  
    15.         if ( convertView == null ) // no old view to reuse, we must create a new one!
    16.         {
    17.             liv = new ListItemView ( mContext, mItems.get ( position ) );
    18.         }
    19.         else
    20.         {
    21.             // Reuse/Overwrite the View passed
    22.             // We are assuming(!) that it is castable!
    23.  
    24.             ListableVO src = mItems.get(position);
    25.  
    26.             liv = (ListItemView) convertView;
    27.  
    28.             liv.resetData ( src );  // call a custom method resetData() to be sure our new data displays OK in the list item
    29.      
    30.         }
    31.         return liv;
    32.     }
    33.  
     
  3. LeonR

    LeonR Member This Topic's Starter

    Joined:
    Feb 22, 2010
    Messages:
    18
    Likes Received:
    0
    Hi, and thanks for taking the time to reply :cool:

    My current code is a mess as i've been trying so many different approaches... but it currently looks like this..


    main.xml

    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.     android:orientation="vertical"
    5.     android:layout_width="fill_parent"
    6.     android:layout_height="fill_parent"
    7.     >
    8.  
    9.  
    10. <ListView  android:id="@+id/ListView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></ListView>
    11.  
    12.  
    13.  
    14. </LinearLayout>
    15.  
    16.  
    17.  

    The main java code (UITestAct)


    Code (Text):
    1.  
    2.  
    3. public class UItestAct extends Activity {
    4.        /** Called when the activity is first created. */
    5.  
    6.     static final String[] COUNTRIES = new String[] {
    7.              "Afghanistan", "Albania", "Algeria", "American Samoa","Andorra"};
    8.  
    9.  
    10.  
    11.  @Override
    12.  public void onCreate(Bundle savedInstanceState) {
    13.      super.onCreate(savedInstanceState);
    14.  
    15.    setContentView(R.layout.main);
    16.    
    17.    
    18.    ProgressBar myBar=new ProgressBar(this);
    19.    myBar.setProgress(50);  //just give it a quick test value
    20.    
    21.    TableLayout myTableLayout = new TableLayout(this); //(TableLayout)findViewById(R.id.TableLayout01);
    22.      
    23.    
    24.    //define a coouple of controls
    25.    TextView myText1 = new TextView(this);  
    26.    TableRow myRow = new TableRow(this);
    27.      
    28.    myText1.setText("test text");
    29.    
    30.   // add the controls to the row view
    31.    myRow.addView(myText1);
    32.    myRow.addView(myBar);
    33.    
    34.    // add the row view to the table layout
    35.    myTableLayout.addView(myRow);
    36.    
    37.    
    38.        //get a reference to my list view (on main.xml)
    39.      ListView myListView = (ListView)findViewById(R.id.ListView01);
    40.      
    41.  
    42.    
    43.     ArrayAdapter<Object> myAdapter = new ArrayAdapter<Object>(this,??????,???????);
    44.    
    45.  
    46.    
    47.     myListView.setAdapter(myAdapter);
    48.  
    49.    
    50.    
    51.    }
    52.  
    53. }
    54.  
    55.    
    56.    
    57.  
    58.  




    I think what im trying to do is probably quite primitive, its just im taking the wrong approach....

    Thanks again for your help!
     
  4. Zero

    Zero Member

    Joined:
    Oct 23, 2009
    Messages:
    11
    Likes Received:
    2
    You need to extend ArrayAdapter, use an instance of that class as your adapter and then return a custom view in getView. The basic way to do it is to inflate a custom listview row layout and modify it before returning it. But, that is quite inefficient and there are different ways to make the method faster. AndroidGuys have a nice set of posts on it.
     
  5. LeonR

    LeonR Member This Topic's Starter

    Joined:
    Feb 22, 2010
    Messages:
    18
    Likes Received:
    0
    Cool, thanks for the help dude! :)
     

Share This Page

Loading...