gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 16:33:44 25 4
gpt4 key购买 nike

如何在点击 RecyclerView 的 Item 后访问数据。我需要的是如何从数据库中获取扩展项的逻辑。目前适配器使用 CursorRecyclerViewAdapter 从数据库中获取数据 https://gist.github.com/skyfishjy/443b7448f59be978bc59

提醒适配器.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()中设置onClickListener,你可以访问所有数据需要。

关于java - 在 recyclerview 中单击项目时获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33361860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com