开发者

Understanding Arraydapter example

开发者 https://www.devze.com 2023-01-08 14:31 出处:网络
I need help explaing the following example below (c) CommonsWare. I know it makes a subclass of Arraydapter to produce custom listviews.

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%", 开发者_Go百科"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);
        }
    }
}


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消