gpt4 book ai didi

java - 在表单之间传递 bundle/ContentValues 和 bundle 之间的 ContentValues 之间的区别

转载 作者:太空宇宙 更新时间:2023-11-03 13:26:02 26 4
gpt4 key购买 nike

我试过传递 ContentValues 以插入数据库。

public long createEntry(String name, String description) {
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_DESCRIPTION, description);
return ourDatabase.insert(DATABASE_TABLE, null, cv);

}

这个方法有效。但现在我想知道如何通过 Intent 将它传递给其他形式。我只知道使用 Intent 来传输 View /表单,但不知道如何传递数据。

public void onClick(View v) {
Log.i("lol","hello");
switch (v.getId()) {
case R.id.oil:
Intent i = new Intent("com.gtxradeon.brands.FirstBrandActivity");
startActivity(i);
finish();
break;
case R.id.android:
Intent i1 = new Intent(this, FirstBrandActivity.class);
startActivity(i1);
break;

default:
break;
}

最后,Bundles 和 ContentValues 之间有什么区别。我曾尝试阅读适用于 Android 的谷歌教程,但它让我更加困惑。

最佳答案

ContentValue 用于将数据更新/插入永久存储数据结构,如 SQLite 数据库。使用 ContentValue 来防止 SQL 注入(inject)很重要。

另一方面,Bundle 用于使用 IntentActivity 之间传递数据。例如,

Bundle bundle = new Bundle();
bundle.putString("name", "John Doe");
Intent intent = new Intent();
intent.putExtras(bundle);

您可以通过以下操作在下一个 Activity 中检索 Bundle:

Bundle bundle = getIntent().getExtras();
String string = bundle.getString("name");

实现相同结果的另一种更常见的方法是:

Intent intent = new Intent();
intent.putExtra("name", "John Doe");

然后在 Activity 上,您通过以下方式获得 Intent:

Intent receivedIntent = getIntent();
String name = receivedIntent.getStringExtra("name");

关于java - 在表单之间传递 bundle/ContentValues 和 bundle 之间的 ContentValues 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19944604/

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