Last active
August 8, 2017 12:50
-
-
Save nikhilbansal97/916499ad2fa28ba9427e9cce7cdab1cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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