I wrote my code to use a Cursor directly due to pskink's comment below the Answer at Populate recyclerview with a SQLite database?. After doing some research (based on the OnClickListener for RecyclerView question) and experimentation I found that moving the setOnClickListener() to the ViewHolder and adding onClick() as its method, I can access the TextView but not get the String value.
mCursor is initialized:
public SelectPetNameAdapter(Context context, Cursor cursor) {
this.mContext = context;
mCursor = cursor;
//TEST: the following is here to verify that the Cursor is passed to this class. It is.
int cnt = getItemCount();
Log.d("Cursor count is", " " + cnt);
}
Code where onClick lives:
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView tvName;
public View mView;
public ViewHolder(View itemView) {
super(itemView);
Log.d("Instantiating", "ViewHolder");
itemView.setOnClickListener(this);
tvName = (TextView) itemView.findViewById(R.id.petname);
mView = itemView;
}
@Override
public void onClick(View v){
Log.d("Entered", "onClick in ViewHolder");
int mPosition = getLayoutPosition();
String name = mCursor.getString(0);
int rowId = mCursor.getPosition();
Intent intent = new Intent(mContext, PetInformationActivity.class);
long holderId = itemView.getId();
String holderName = itemView.toString();
int viewId = v.getId();
mCursor.moveToPosition(mPosition);
Log.d("Entered", "setOnClickListener in SelectPetNameAdapter");
Log.d("holderId is", " " + holderId);
Log.d("holderName is", " " + holderName);
Log.d("viewId is", " " + viewId);
Log.d("position is", " " + mPosition);
Log.d("Pet ID is", " " + rowId);
Log.d("Pet name is", " " + name);
Bundle extras = new Bundle();
extras.putString(NAME_KEY, name);
intent.putExtras(extras);
mContext.startActivity(intent);
}
}
While this does provide access to the TextView's String the displayed name is out of sync with the TextView clicked. As shown below in LOGCAT the first Position is: 2 is the position of the clicked TextView but the Pet ID: 0 and Pet name: qwerty are the Cursor values of a previous click. After another click the Position is: 8 is the position of the clicked TextView and the Pet ID: 2 and the Pet name: fert are the value of the Cursor from the previous click immediately above!
LOGCAT
Entered: onClick in ViewHolder
Entered: setOnClickListener in SelectPetNameAdapter
holderId is: 2131558581
holderName is: android.support.v7.widget.AppCompatTextView{b3adb53 V.ED..C.. ...P.... 0,98-1056,147 #7f0d00b5 app:id/petname}
viewId is: 2131558581
Position is: 2
Pet ID is: 0
Pet name is: qwerty
Entered: onCreate in PetInformatioActivity
petName (from Bundle) is: qwerty
Id (from Bundle) is: null
Entered: onClick in ViewHolder
Entered: setOnClickListener in SelectPetNameAdapter
holderId is: 2131558581
holderName is: android.support.v7.widget.AppCompatTextView{45fe03c V.ED..C.. ...P.... 0,392-1056,441 #7f0d00b5 app:id/petname}
viewId is: 2131558581
Position is: 8
Pet ID is: 2
Pet name is: fert
Entered: onCreate in PetInformatioActivity
petName (from Bundle) is: fert
Id (from Bundle) is: null
However, while the position is correct for the current click, how do I access the String in the clicked TextView, not the previous String in the Cursor?