简体   繁体   中英

get the data when an Item is clicked in recyclerview

How to access data after clicking an Item of RecyclerView . What I need is the logic behind on how to get the expanded Items from the database. Currently for adapter using CursorRecyclerViewAdapter to get data from database https://gist.github.com/skyfishjy/443b7448f59be978bc59

RemindersAdapter.java

 public class RemindersAdapter extends     CursorRecyclerViewAdapter<RemindersAdapter.ItemViewHolder> {
   private final LayoutInflater inflater;
   List<ListInfo> data = Collections.emptyList();
   private Context context;
   ListInfo temporaryBucket;
public RemindersAdapter(Context context, Cursor cursor){
    super(context, cursor);
    inflater = LayoutInflater.from(context);
    this.context = context;
}

@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.reminder_item, parent, false);

    ItemViewHolder holder = new ItemViewHolder(view);
    temporaryBucket = new ListInfo();

    return holder;
}

@Override
public void onBindViewHolder(ItemViewHolder viewHolder, Cursor cursor) {
        int     id      = cursor.getInt(cursor.getColumnIndex(MyDBHandler.COLUMN_ID));
        String  title   = cursor.getString(cursor.getColumnIndex(MyDBHandler.COLUMN_TITLE_REMINDER));
        String  desc    = cursor.getString(cursor.getColumnIndex(MyDBHandler.COLUMN_DESC_REMINDER));
        String  date    = cursor.getString(cursor.getColumnIndex(MyDBHandler.COLUMN_DATE_REMINDER));

        viewHolder.title.setText(title);

}

class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView title;

    public ItemViewHolder(View itemView) {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.reminderTitle);

        itemView.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        int position = getLayoutPosition();

        Toast.makeText(context, "Clicked", Toast.LENGTH_SHORT).show();
    }
  }
}

MyDBHandler.java

 public class MyDBHandler extends SQLiteOpenHelper{
  private static final int DATABASE_VERSION = 7;
  private static final String DATABASE_NAME = "paroah.db";
  public static final String TABLE_REMINDER = "reminders";
  public static final  String COLUMN_ID = "_id";
  public static final  String COLUMN_TITLE_REMINDER = "title";
  public static final  String COLUMN_DESC_REMINDER = "desc";
  public static final  String COLUMN_DATE_REMINDER = "date_created";
  private Cursor allReminders;
public MyDBHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}


@Override
public void onCreate(SQLiteDatabase db) {
    String query = " CREATE TABLE "
            +TABLE_REMINDER+ "(" +
            COLUMN_ID +" INTEGER PRIMARY KEY AUTOINCREMENT,"+
            COLUMN_TITLE_REMINDER + " TEXT ,"+
            COLUMN_DESC_REMINDER + " TEXT ,"+
            COLUMN_DATE_REMINDER + " TEXT "+
            ");";

    db.execSQL(query);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.d("aoi", "Upgrading database from version " + oldVersion + " to "
            + newVersion + ", which will destroy all old data");
    try {
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_REMINDER);
        onCreate(db);

    } catch (SQLException e) {
        Log.d("aoi",  "getting exception "
                + e.getLocalizedMessage().toString());
    }
}

public void addReminder(ListInfo reminder ){
    ContentValues values = new ContentValues();
    values.put(COLUMN_TITLE_REMINDER, reminder.getTitle());
    values.put(COLUMN_DESC_REMINDER, reminder.getDesc());
    values.put(COLUMN_DATE_REMINDER, reminder.getDate());

    SQLiteDatabase db = getWritableDatabase();

    db.insert(TABLE_REMINDER, null, values);
    db.close();
}

public Cursor getAllReminders() {

    SQLiteDatabase db = getWritableDatabase();

    String query = "SELECT * FROM "+TABLE_REMINDER;

    allReminders = db.rawQuery(query, null);

    return allReminders;
}

}

In my onBindViewHolder I'm getting "id, title, desc and date" but only showing the title which when clicked will show the desc and date. For testing just showing a Toast for now on click of item.

您可以使用holder.itemView.setOnClickListener(new OnClickListener({...})onBindViewHolder()设置holder.itemView.setOnClickListener(new OnClickListener({...}) ,然后可以访问所需的所有数据。

You can bind view holder with all the data even if you just show the title

    @Override
    public void onBindViewHolder(ItemViewHolder viewHolder, Cursor cursor) {
            int     id      = cursor.getInt(cursor.getColumnIndex(MyDBHandler.COLUMN_ID));
            String  title   = cursor.getString(cursor.getColumnIndex(MyDBHandler.COLUMN_TITLE_REMINDER));
            String  desc    = cursor.getString(cursor.getColumnIndex(MyDBHandler.COLUMN_DESC_REMINDER));
            String  date    = cursor.getString(cursor.getColumnIndex(MyDBHandler.COLUMN_DATE_REMINDER));

            viewHolder.bind(id, title, desc, date);

    }

    class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        int idData;
        String titleData;
        String descData;
        String dateData;

        TextView title;

        public ItemViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.reminderTitle);

            itemView.setOnClickListener(this);

        }

        public void bind(int id, String title, String desc, String date){
            this.idData = id;
            this.titleData = title;
            this.descData = desc;
            this.dateData = date;
            this.title.setText(title);
        }

        @Override
        public void onClick(View v) {
            // You can access all the data here
            Toast.makeText(context, "Clicked", Toast.LENGTH_SHORT).show();
        }
      }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM