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.
Try this way,hope this will help you to solve your problem.
class CustomUsersAdapter extends BaseAdapter {
private Context context;
pArrayList<User> users;
public CustomUsersAdapter(Context context,ArrayList<User> 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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With