CustomArrayAdapter执行:无法获取资源ID [英] CustomArrayAdapter Implementation:Unable to get the resource id

查看:128
本文介绍了CustomArrayAdapter执行:无法获取资源ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要实现CustomArrayAdapter和下面是构造我已经为我的自定义适配器写

 公共CustomUsersAdapter(上下文的背景下,ArrayList的<使用者>用户){
        超级(上下文,0,用户);
     }
 

在超的第二个参数调用包含一个TextView实例化视图时使用布局文件资源ID。我不知道哪些资源ID正在这里所指。任何人都可以请详细解释其资源ID正在这里所指。

我重写getView方法如下: -

  @覆盖
     公共查看getView(INT位置,查看convertView,ViewGroup中父){
        //获取数据项这个职位
        用户USER =的getItem(位置);
        //检查现有的观点正在被重复使用,否则膨胀的看法
        如果(convertView == NULL){
           convertView = LayoutInflater.from(的getContext())膨胀(R.layout.item_user,父母,假)。
        }
        数据人口//查询视图
        TextView的tvName =(TextView中)convertView.findViewById(R.id.tvName);
        TextView的tvHome =(TextView中)convertView.findViewById(R.id.tvHometown);
        //使用数据对象填充数据到模板视图
        tvName.setText(user.name);
        tvHome.setText(user.hometown);
        //返回完成视图呈现在屏幕上
        返回convertView;
    }
 

解决方案

 我不知道哪些资源ID正在这里所指。
 

既然你把 0 在构造函数中的第二个参数,它不会有任何引用任何布局

为<一个href="http://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter(android.content.Context,%20int,%20int)"相对=nofollow>文档是说:

 资源包含布局实例化视图时使用布局文件资源ID。
 

现在,如果你把一个现有的布局构造则该布局将使用实例化的视图中的 listViewItems ,但你需要重写 getView 方法,使您能够设计出文字的布局去哪里。

如果您正计划将现有的布局,然后用另一个构造函数就可以了:

 公共ArrayAdapter(上下文的背景下,INT资源,T []对象)
 

但对于设计,我不会真正增加任何资源布局构造,而是直接充气 getView 法的观点,并返回该视图。

I want to implement CustomArrayAdapter and following is the constructor I have written for my custom adapter

public CustomUsersAdapter(Context context, ArrayList<User> users) {
        super(context, 0, users);
     }

The second argument in the super call The resource ID for a layout file containing a TextView to use when instantiating views. I dont know which resource ID is being referred here. Can anyone please explain in detail which resource ID is being referred here.

My overridden getView method is as follows :-

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

解决方案

 I dont know which resource ID is being referred here.

Since you put 0 in the second parameter of the constructor it wont have any reference to any layout

as the documentation is saying:

 resource   The resource ID for a layout file containing a layout to use when instantiating views.

Now if you put an existing layout to the constructor then that layout will be use to instantiate the view of your listViewItems but you need to override the getView method to enable you to design which text goes where in your layout.

If you are planning to put an existing layout then use another constructor on it:

public ArrayAdapter (Context context, int resource, T[] objects)

But for design I wont really add any resource layout to the constructor but directly inflate the view in your getView method and return that view.

这篇关于CustomArrayAdapter执行:无法获取资源ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆