Skip to content

Instantly share code, notes, and snippets.

@nikhilbansal97
Last active August 8, 2017 12:50
Show Gist options
  • Save nikhilbansal97/916499ad2fa28ba9427e9cce7cdab1cd to your computer and use it in GitHub Desktop.
Save nikhilbansal97/916499ad2fa28ba9427e9cce7cdab1cd to your computer and use it in GitHub Desktop.
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private static final String TAG = "DataAdapter";
private Context context;
private Cursor mCursor;
public DataAdapter(Context context,Cursor cursor) {
this.context = context;
mCursor = cursor;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.list_item,parent,false));
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Log.d(TAG, "onBindViewHolder: " + String.valueOf(position));
if(mCursor != null)
{
mCursor.moveToPosition(position);
int dataIndex = mCursor.getColumnIndex(DataContract.DataProvider.COLUMN_DATA_ITEM);
String data = mCursor.getString(dataIndex);
holder.textView.setText(data);
}
}
@Override
public int getItemCount() {
if(mCursor == null)
return 0;
return mCursor.getCount();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView textView;
public ViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.textView_listItem);
}
}
public void swapCursor(Cursor cursor){
mCursor = cursor;
mCursor.moveToFirst();
notifyDataSetChanged();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment