简体   繁体   English

在recyclerview中单击项目时获取数据

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

How to access data after clicking an Item of RecyclerView . 单击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 当前用于使用CursorRecyclerViewAdapter从数据库https://gist.github.com/skyfishjy/443b7448f59be978bc59获取数据的适配器

RemindersAdapter.java 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 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. 在我的onBindViewHolder我得到“ id,title,desc和date”,但仅显示标题,单击该标题将显示desc和日期。 For testing just showing a Toast for now on click of item. 对于测试,只需单击项目即可立即显示Toast。

您可以使用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();
        }
      }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击recyclerView Item时如何将数据获取到webView活动中 - how to get data into webView activity when recyclerView Item is clicked 单击recyclerview上的项目时的多个数据 - Multiple data when clicked item on recyclerview 单击RecyclerView项时发生NullPointerException - NullPointerException when RecyclerView item is clicked Android - 获取 recyclerview 点击项目的 SQLite object 数据 - Android - Get recyclerview clicked item's SQLite object data 单击过滤的 RecyclerView 项时显示错误数据 - Showing wrong data when filtered RecyclerView item is clicked 单击过滤的RecyclerView项目时显示错误数据 - Show wrong data when filtered RecyclerView item is clicked 单击项目时将数据从 RecyclerView 传递到 Fragment - Passing Data from RecyclerView to Fragment when Item is Clicked 单击第一个 RecyclerView 中的项目时,如何将数据从一个 RecyclerView 流向另一个 RecyclerView? - How to flow Data from One RecyclerView to Another RecyclerView when an item in first RecyclerView is clicked? 在recyclerView中获取选中的item或者被点击的item - Get the selected item or the item that is being clicked in the recyclerView 如何在Android RecyclerView中获取单击项的文本 - How to get the text of clicked item in android recyclerview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM