CustomArrayAdapter Implementation:Unable to get the resource id

前端 未结 4 565
走了就别回头了
走了就别回头了 2021-01-19 01:12

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

public CustomUsersAdapter(Context context, Array         


        
4条回答
  •  花落未央
    2021-01-19 01:42

    Try this way,hope this will help you to solve your problem.

    class CustomUsersAdapter extends BaseAdapter {
    
        private Context context;
        pArrayList users;
    
    
        public CustomUsersAdapter(Context context,ArrayList users) {
            this.context = context;
            this.users = users;
        }
    
        public class ViewHolder {
            TextView tvName;
            TextView tvHometown;
        }
    
    
        @Override
        public int getCount() {
            return users.size();
        }
    
        @Override
        public Object getItem(int position) {
            return users.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        public View getView(final int position, View view, ViewGroup parent) {
            final ViewHolder holder;
            if (view == null) {
                holder = new ViewHolder();
                view = LayoutInflater.from(context).inflate(R.layout.item_user, null);
                holder.tvName = (TextView) view.findViewById(R.id.tvHometown);
                holder.tvHometown = (TextView) view.findViewById(R.id.tvHometown);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            holder.tvName.setText(users.get(position).name);
            holder.tvHometown.setText(users.get(position).hometown);
            return view;
        }
    }
    

提交回复
热议问题