tags:

views:

41

answers:

2

I need help explaing the following example below (c) CommonsWare. I know it makes a subclass of Arraydapter to produce custom listviews.

However I don't understand these rows:

    IconicAdapter() {
        super(DynamicDemo.this, R.layout.row, items);
    }

What does super() do? And what will the arguments be good for? Why do I need pass "items" as an argument but not the other array that is called "rating" ?

Full code:

public class DynamicDemo extends ListActivity {
    private String[] items = { "lorem", "ipsum", "dolor", "sit", "amet",
            "consectetuer", "adipiscing", "elit", "morbi", "vel", "ligula",
            "vitae", "arcu", "aliquet", "mollis", "etiam", "vel", "erat",
            "placerat", "ante", "porttitor", "sodales", "pellentesque",
            "augue", "purus" };

    private String[] rating = { "25%", "65%", "95%", "55%", "15%", "25%r",
            "25%", "25%", "25%", "25%", "25%", "25%", "25%", "25%", "25%",
            "25%", "25%", "25%", "25%", "25%", "25%", "25%", "25%", "25%",
            "25%" };

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        setListAdapter(new IconicAdapter());
    }

    public void onListItemClick(ListView parent, View v, int position, long id) {
        selection.setText(items[position]);
    }

    class IconicAdapter extends ArrayAdapter<String> {
        IconicAdapter() {
            super(DynamicDemo.this, R.layout.row, items);
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = getLayoutInflater();
            View row = inflater.inflate(R.layout.row, parent, false);
            TextView label = (TextView) row.findViewById(R.id.label);
            TextView label2 = (TextView) row.findViewById(R.id.label2);

            label.setText(items[position]);
            label2.setText(rating[position]);

            return (row);
        }
    }
}
A: 

What does super() do?

It causes the superclass' constructor to be executed. This is a requirement in Java.

And what will the arguments be good for?

The arguments are required by the superclass' constructor.

Why do I need pass "items" as an argument but not the other array that is called "rating" ?

My example does not have rating. I did not write the code you have pasted above.

CommonsWare
Thank you for your reply. I have modified the example it while trying to learn. So the superclass constructor is:ArrayAdapter(Context context, int textViewResourceId, List<T> objects)?What I want to do is to pass multiple arrays to arrayadapter. What is the "right" way to do this?
droidgren
@droidgren: you cannot pass multiple arrays to the `ArrayAdapter`. Instead, you should make a single array of more complex objects.
CommonsWare
Is that the only way to do it? To combine my arrays to one single complex array?
droidgren
@droidgren: it is the best way of using `ArrayAdapter`
CommonsWare
Ok, so I should use an alternative Adapter if I want to use more arrays then one? If so, which Adapter is most appropriate?
droidgren
@droidgren: you would need to create your own, perhaps using `BaseAdapter` as a starting point.
CommonsWare
Ok, Thank you. I really appreciate it. I also found another tutorial which is using a custom Adapter, like you told.http://tech-droid.blogspot.com/2009/07/custom-listview-for-android.htmlBut.. It what is weird is that my "rating" array still got printed even though the variable was not passed though the super constructor.. How do you explain that?
droidgren
@droidgren: Because you have two arrays of the same length, the position values are the same for both.
CommonsWare
Yes, you are right.. Making the array smaller makes that app FC. Thanks for clearing it up.
droidgren
A: 

super is the call to the base class constructor. So in your case you are calling the constructor of ArrayAdapter. To see what arguments are "good" for it you need to see the declaration of base class constructor

Please refer to the Java Tutorial for more info - http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/java/IandI/super.html

ZloiAdun