You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中FragmentBell的post_id传Activity及SQLite数据加载问题

Android开发问题解决方案

问题1:传递post_id到ActivityNotificationDetail

核心是在触发跳转的逻辑中(通常是Adapter的Item点击或Fragment内的点击事件),通过Intent携带post_id,再在目标Activity中读取。

方案示例

1. 在AdapterBell的Item点击中传递数据

// AdapterBell的ViewHolder点击事件处理
itemView.setOnClickListener(v -> {
    // 获取当前Item对应的Bell对象
    Bell currentBell = bellList.get(getAdapterPosition());
    // 创建跳转Intent
    Intent intent = new Intent(v.getContext(), ActivityNotificationDetail.class);
    // 传递post_id,根据实际字段类型调整(int/String等)
    intent.putExtra("KEY_POST_ID", currentBell.getPost_id());
    // 启动Activity
    v.getContext().startActivity(intent);
});

2. 在ActivityNotificationDetail中接收数据

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notification_detail);
    
    // 读取传递的post_id,第二个参数是默认值(未获取到时返回)
    int postId = getIntent().getIntExtra("KEY_POST_ID", -1);
    // 验证数据有效性后处理业务逻辑
    if (postId != -1) {
        // 这里写使用post_id的代码
    }
}

如果是在FragmentBell内部触发跳转,只需将v.getContext()替换为requireContext()或getActivity()即可。

问题2:Fragment中从SQLite获取ArrayList数据

需要先实现数据库操作类,再在Fragment中调用查询方法获取数据。

方案示例

1. 实现SQLite数据库操作类

public class BellDbHelper extends SQLiteOpenHelper {
    private static final String DB_NAME = "bell_db";
    private static final int DB_VERSION = 1;
    private static final String TABLE_BELL = "bell";
    // 对应Bell实体类的字段,根据你的实际定义调整
    private static final String COLUMN_POST_ID = "post_id";
    // 其他字段...

    public BellDbHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // 创建表的SQL语句,匹配Bell实体类字段
        String createTableSql = "CREATE TABLE " + TABLE_BELL + " (" +
                COLUMN_POST_ID + " INTEGER PRIMARY KEY," +
                // 其他字段定义...
                ")";
        db.execSQL(createTableSql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // 版本升级时的表处理逻辑
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_BELL);
        onCreate(db);
    }

    // 查询所有Bell数据,返回ArrayList<Bell>
    public ArrayList<Bell> queryAllBells() {
        ArrayList<Bell> bellList = new ArrayList<>();
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_BELL, null);

        if (cursor.moveToFirst()) {
            do {
                Bell bell = new Bell();
                // 从Cursor中读取数据并设置到Bell对象
                bell.setPost_id(cursor.getInt(cursor.getColumnIndex(COLUMN_POST_ID)));
                // 设置其他字段...
                bellList.add(bell);
            } while (cursor.moveToNext());
        }
        // 关闭Cursor和数据库连接
        cursor.close();
        db.close();
        return bellList;
    }
}

2. 在FragmentBell中调用查询方法

public class FragmentBell extends Fragment {
    private AdapterBell adapter;
    private ArrayList<Bell> bellList;
    private BellDbHelper dbHelper;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_bell, container, false);
        
        // 初始化数据库帮助类
        dbHelper = new BellDbHelper(requireContext());
        // 查询获取数据
        bellList = dbHelper.queryAllBells();
        
        // 绑定数据到RecyclerView(假设用RecyclerView展示)
        RecyclerView recyclerView = view.findViewById(R.id.rv_bell);
        recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
        adapter = new AdapterBell(bellList);
        recyclerView.setAdapter(adapter);
        
        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // 关闭数据库连接,避免内存泄漏
        dbHelper.close();
    }
}

内容的提问来源于stack exchange,提问作者jay raman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 13:42:25