java - 了解 Arrayadapter 示例

标签 java android

我需要帮助解释下面的示例 (c) CommonsWare。 我知道它创建了 Arraydapter 的子类来生成自定义 ListView 。

但是我不明白这些行:

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

super() 的作用是什么?这些争论有什么好处呢?为什么我需要传递“items”作为参数,而不是另一个名为“rating”的数组?

完整代码:

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);
        }
    }
}

最佳答案

What does super() do?

它会导致执行父类(super class)的构造函数。这是 Java 中的要求。

And what will the arguments be good for?

父类(super class)的构造函数需要参数。

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

我的示例没有评级。我没有编写您上面粘贴的代码。

关于java - 了解 Arrayadapter 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3289998/

相关文章:

java - 获取 TIBCO EMS 队列

java - 安卓工作室 : what compiler settings when including Google Maps

java - 如何使用谷歌应用程序引擎绘制图像?

android - 如何制作像 Telegram 一样的循环进度条?

java - 生成错误 : Cannot find the class file for org. openqa.selenium.internal.Locatable

Java-替换括号中的字符串

java - 如何在不使用 Android Studio 的情况下制作 Android 应用程序?

android - 来自数据类 .copy() 的新对象

android - jfeinstein的SlidingMenu库如何自定义setSecondaryMenu的宽度

java.lang.NullPointerException long baba1