簡體   English   中英

在recyclerview中單擊項目時獲取數據

[英]get the data when an Item is clicked in recyclerview

單擊RecyclerView的項目后如何訪問數據。 我需要的是如何從數據庫中獲取展開的項目背后的邏輯。 當前用於使用CursorRecyclerViewAdapter從數據庫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;
}

}

在我的onBindViewHolder我得到“ id,title,desc和date”,但僅顯示標題,單擊該標題將顯示desc和日期。 對於測試,只需單擊項目即可立即顯示Toast。

您可以使用holder.itemView.setOnClickListener(new OnClickListener({...})onBindViewHolder()設置holder.itemView.setOnClickListener(new OnClickListener({...}) ,然后可以訪問所需的所有數據。

您可以將視圖持有者與所有數據綁定,即使您僅顯示標題

    @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();
        }
      }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM