【发布时间】:2014-08-19 04:49:21
【问题描述】:
我想实现 CustomArrayAdapter,下面是我为自定义适配器编写的构造函数
public CustomUsersAdapter(Context context, ArrayList<User> users) {
super(context, 0, users);
}
super 调用中的第二个参数 包含 TextView 的布局文件的资源 ID,以在实例化视图时使用。我不知道这里引用的是哪个资源 ID。谁能详细解释一下这里引用的是哪个资源ID。
我重写的 getView 方法如下:-
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
User user = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_user, parent, false);
}
// Lookup view for data population
TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
TextView tvHome = (TextView) convertView.findViewById(R.id.tvHometown);
// Populate the data into the template view using the data object
tvName.setText(user.name);
tvHome.setText(user.hometown);
// Return the completed view to render on screen
return convertView;
}
【问题讨论】:
-
那是你的layout id,你可以通过构造函数传递或者直接插入那里。
-
第二个参数是您的 ListView 行布局 ID,就像您在 getView() 中使用的一样。
-
我已经编辑了我的问题以包含 getView 方法。 .你能告诉我这是正确的方法吗?