我需要帮助解释下面的例子(c)CommonsWare。 我知道它使Arraydapter的子类生成自定义列表视图。
但是我不理解这些行:
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);
}
}
}
答案 0 :(得分:0)
super()做什么?
它导致超类'构造函数被执行。这是Java中的一项要求。
这些论点会有什么好处 为?
超类的构造函数需要参数。
为什么我需要传递“项目”作为 参数但不是其他数组 被称为“评级”?
我的示例没有rating
。我没有写上面你粘贴的代码。
答案 1 :(得分:0)
super是对基类构造函数的调用。因此,在您的情况下,您正在调用ArrayAdapter的构造函数。要查看哪些参数对它“好”,您需要查看基类构造函数的声明
有关详细信息,请参阅Java教程 - http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/java/IandI/super.html