gpt4 book ai didi

java - 了解 Arrayadapter 示例

转载 作者:行者123 更新时间:2023-12-01 05:55:03 24 4
gpt4 key购买 nike

我需要帮助解释下面的示例 (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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com