• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps ArrayAdapter and ListView help!

LeonR

Newbie
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!
 
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

public abstract View getView (int position, View convertView, ViewGroup parent)

Since: API Level 1
Get a View that displays the data at the specified position in the data set. You can either create a View manually or inflate it from an XML layout file. When the View is inflated, the parent View (GridView, ListView...) will apply default layout parameters unless you use inflate(int, android.view.ViewGroup, boolean) to specify a root view and to prevent attachment to the root.

Parameters

position The position of the item within the adapter's data set of the item whose view we want. convertView The old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view. parent The parent that this view will eventually be attached to
Returns


  • A View corresponding to the data at the specified position.
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:
     /**
     * @param convertView
     *            The old view to overwrite, if one is passed by the main ListView (which is done automatically by the Android OS)
     * @param mItems
     *            the ArrayList of data in our adapter, the list item needs the 
     *            context and data passed to it when it is constructed so it knows what to display
     * @returns a ListItemView that holds wraps around a list item
     */
    public View getView ( int position, View convertView, ViewGroup parent )
    {

        ListItemView liv;

        if ( convertView == null ) // no old view to reuse, we must create a new one!
        {
            liv = new ListItemView ( mContext, mItems.get ( position ) );
        }
        else
        { 
            // Reuse/Overwrite the View passed
            // We are assuming(!) that it is castable!

            ListableVO src = mItems.get(position); 

            liv = (ListItemView) convertView;

            liv.resetData ( src );  // call a custom method resetData() to be sure our new data displays OK in the list item
     
        }
        return liv;
    }
 
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:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >


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



</LinearLayout>


The main java code (UITestAct)


Code:
public class UItestAct extends Activity {
       /** Called when the activity is first created. */

    static final String[] COUNTRIES = new String[] {
             "Afghanistan", "Albania", "Algeria", "American Samoa","Andorra"};



 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

   setContentView(R.layout.main);
   
   
   ProgressBar myBar=new ProgressBar(this);
   myBar.setProgress(50);  //just give it a quick test value
    
   TableLayout myTableLayout = new TableLayout(this); //(TableLayout)findViewById(R.id.TableLayout01);
     
   
   //define a coouple of controls
   TextView myText1 = new TextView(this);  
   TableRow myRow = new TableRow(this);
     
   myText1.setText("test text");
    
  // add the controls to the row view
   myRow.addView(myText1);
   myRow.addView(myBar);
   
   // add the row view to the table layout
   myTableLayout.addView(myRow);
    
   
       //get a reference to my list view (on main.xml)
     ListView myListView = (ListView)findViewById(R.id.ListView01);
     

    
    ArrayAdapter<Object> myAdapter = new ArrayAdapter<Object>(this,??????,???????);
    

    
    myListView.setAdapter(myAdapter);

    
    
   }

}





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

Thanks again for your help!
 
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.
 
Back
Top Bottom