我已经实现了界面和其他必需的东西,但在onClick方法中我卡住了。
// this is inside ViewHolderClass that extends RecyclerView.ViewHolder
implements View.OnClickListener //
@Override
public void onClick(View v) {
if (mOnClickListener != null) {
mOnClickListener.onListItemClick(v.getTag().toString(), getAdapterPosition());
}
在标记值的位置,我想获取用户点击的listItem值。
更详细的代码:
public class ScannerListAdapter extends
RecyclerView.Adapter<ScannerListAdapter.ScannerViewHolder> {
private Context mContext;
private Cursor mCursor;
// to store reference member variable is created
private final ListItemClickListener mOnClickListener;
// to initialize listener
public ScannerListAdapter(Context context, Cursor cursor,
ListItemClickListener listener ) {
mContext = context;
mCursor = cursor;
mOnClickListener = listener;
}
@NonNull
@Override
public ScannerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.scanner_list_item, parent, false);
return new ScannerViewHolder(view);
}
代码继续......
@Override
public void onBindViewHolder(@NonNull ScannerViewHolder holder, int position)
{
if (!mCursor.moveToPosition(position))
return;
ArrayList<Integer> values = new ArrayList<>();
values.add(1);
values.add(2);
String name = mCursor.getString(mCursor.getColumnIndex(ScannerEntry.COLUMN_SCANNED_RESULT));
int id = mCursor.getInt(mCursor.getColumnIndex(ScannerEntry._ID));
String date = mCursor.getString(mCursor.getColumnIndex(ScannerEntry.COLUMN_TIMESTAMP));
holder.resultTextView.setText(name);
// holder.itemView.setTag(key1,id);
holder.dateTextView.setText(date);
// holder.itemView.setTag(key2,name);
holder.itemView.setTag(values);
}
@Override
public int getItemCount() {
return mCursor.getCount();
}
public void swapCursor(Cursor newCursor) {
// Always close the previous mCursor first
if (mCursor != null) mCursor.close();
mCursor = newCursor;
if (newCursor != null) {
// Force the RecyclerView to refresh
this.notifyDataSetChanged();
}
}
代码继续......
// inner class is created to hold the data of different views
class ScannerViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener {
TextView dateTextView;
TextView resultTextView;
//method required to cast in itemView
public ScannerViewHolder(View itemView) {
super(itemView);
dateTextView = (TextView) itemView.findViewById(R.id.date_textView);
resultTextView = (TextView)
itemView.findViewById(R.id.result_textView);
// to let view to invoke it
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
ArrayList<Integer> values = (ArrayList<Integer>) v.getTag();
// String value=v.getTag(2).toString();
if (mOnClickListener != null) {
mOnClickListener.onListItemClick(values.toString(),
getAdapterPosition());
}
/*int adapterPosition = getAdapterPosition();
String weatherForDay = mWeatherData[adapterPosition];
mOnClickListener.onListItemClick(adapterPosition,
weatherForDay);*/
}
}
//interface is created to define a listener
public interface ListItemClickListener {
// we just need index of the click item so int variable is created
void onListItemClick(String v, int position);
}
} 在这里你可以检查和评论。 和onListItemClick(String v,int position)正在发送给MainActivity。
答案 0 :(得分:1)
您可以在onBindViewHolder()中使用setTag(value)
方法,然后可以使用getTag()
检索该值。
//set value
view.setTag(value);
//get value
String value = view.getTag().toString();
有多个值:
//set values
ArrayList<String> values = new ArrayList<>();
values.add("one");
values.add("two");
values.add("three");
view.setTag(values)
//get values
ArrayList<String> values = (ArrayList<String>) view.getTag();
答案 1 :(得分:0)
您已经调用了RecyclerView.ViewHolder的getAdapterPosition()
。这将返回数据集合中单击项的位置。所以你可以用它来获取数据。