Create a gist now

Instantly share code, notes, and snippets.

How to work with lists adapter, ListView and RecyclerView
//This are ment to be use with the RecyclerView
//Linear
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
//Grid
GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 2);
//Here Im passing a custom model, but you can use a String
public class CustomAdapter extends ArrayAdapter<YourObject> {
//Most of the time this is your constructor, otherwise maybe you need the YourObject[]
public AssignationAdapter(Context context, int resource, List<c> objects) {
super(context, resource, objects);
//If you create a global variable int resource, and here this.resource = resource, you can change the resource programatically by the instatiation
}
//You are going to use this subclass as a way to access your views as variables
private class ViewChildHolder {
//Make sure you have 1 variable for each view you want to manipulate, this view could be a LinearLayout with a TV and a IV
TextView objectName;
ImageView objectPhoto;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
ViewChildHolder holder;
//This will inflate your view using the subclass above
if (convertView == null) {
//You are passing your resource again here, cause there is no resource global variable
convertView = inflater.inflate(R.layout.list_item_custom, parent, false);
holder = new ViewChildHolder();
//Here you can find the views you want to manipulate
holder.objectName = (TextView) convertView.findViewById(R.id.objectNameId);
//TODO find the IV holder holder.objectPhoto = ...
convertView.setTag(holder);
} else {
holder = (ViewChildHolder) convertView.getTag();
}
//get the item by posisiont and set the name to the TV
holder.objectName.setText(getItem(position).getName());
return convertView;
}
/*If you are working with a spinner then the this method will change the dropdown elements appearance. The previous
getView method will be the view in the spinner. You can do here the same that is done in getView*/
/*@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return convertView;
}*/
@Override
public long getItemId(int position) {
//Does your object has an Id of some sort, then override this and you will get the Id in the itemClickListener by example
return getItem(position).getid();
}
}
//Dont forget to set the adapter later to your view
//Create the subclass before extending, yes obvious, but sime times can be forgotten
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
//RecylerView is supposed to encapsulate every part of the logic, so there is no click item listener
//That is why I get context, cause off course, if the item is clicked you can do an Intent (the most common case)
//from here, but is slow, so I rather send a broadcast and let the Activity to take care of the intent.
//The rest of the actions could be here, changing color to the text in the view, setting something to the objetc, etc
private Context context;
//Again, this could be simple a String
private List<YourObject> mObject;
//There is no constructor matching the super here, so create your own with what ever you need
public CustomAdapter(Context context, List<YourObject> mObject) {
this.context = context;
this.mObject = mObject;
}
//This will inflate your view as in the previous, but the method for accesing each individual view is separated, below
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_custom, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
//You could access a String here
final YourObject object = mObject.get(position);
//This is the view you set in the subclass
holder.name.setText(object.getName());
//Lets change the text color on click
holder.name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setTextColor(ContextCompat(context, getColor(R.color.colorAccent))
}
});
//This is an example of a onClick action, sendind a broadcast to the Activity, then in the activity received the broadcast and do something like an Intent passing the url as a param
holder.photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent click = new Intent();
click.setAction("clickePhoto");
click.putExtra("photoUrl", object.getUrl);
context.sendBroadcast(click);
}
});
//TODO set the url to the holder.photo
}
@Override
public int getItemCount() {
return mObject.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView name;
ImageView photo;
//Same that above, create a subclass which you will use to access your views
//But this time, the inflating is above and the finding is here
ViewHolder(View view) {
super(view);
name = (TextView) view.findViewById(R.id.itemNameId);
photo = (ImageView) view.findViewById(R.id.itemPhotoId);
}
}
}
//Dont forget to set the adapter to the RecyclerView and mainly, RecyclerViews need a LayoutManager so you need to do that also
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment