Skip to content

Instantly share code, notes, and snippets.

@RowlandOti
Created August 2, 2016 17:32
Show Gist options
  • Save RowlandOti/4e7c3ec303432dc7cc493e1294a9445d to your computer and use it in GitHub Desktop.
Save RowlandOti/4e7c3ec303432dc7cc493e1294a9445d to your computer and use it in GitHub Desktop.
package com.example.android.booklisting;
import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.List;
/**
* Created by Rushi Patel on 7/29/2016.
*/
public class ResultsAdapter extends ArrayAdapter<Result> {
public static final String LOG_TAG = ResultsAdapter.class.getSimpleName();
private static class ViewHolder {
TextView resultTitle;
TextView resultAuthor;
}
public ResultsAdapter(Activity context) {
super(context, 0);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Result currentResult = getItem(position);
//ViewHolder is more efficient because it spends less time finding views
ViewHolder holder;
if (convertView == null) {
// If there's no view to re-use, inflate a brand new view for row
holder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.list_item, parent, false);
holder.resultTitle = (TextView) convertView.findViewById(R.id.book_title);
holder.resultAuthor = (TextView) convertView.findViewById(R.id.author);
// Cache the viewHolder object inside the fresh view
convertView.setTag(holder);
} else {
// View is being recycled, retrieve the viewHolder object from tag
holder = (ViewHolder) convertView.getTag();
}
holder.resultTitle.setText(currentResult.getTitle());
holder.resultAuthor.setText(currentResult.getBookAuthor());
return convertView;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment