Java 在表单之间传递bundle/ContentValues以及bundle之间ContentValues之间的差异
我已尝试传递Java 在表单之间传递bundle/ContentValues以及bundle之间ContentValues之间的差异,java,android,xml,sqlite,Java,Android,Xml,Sqlite,我已尝试传递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(DATAB
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);
}
这种方法有效。但现在我想知道如何通过意图把它传递给其他形式。我只知道使用意图传输视图/表单,但不知道如何传递数据
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;
}
最后,bundle和ContentValues之间的区别是什么。。我曾尝试阅读谷歌android教程,但这让我更加困惑
ContentValue
s用于将数据更新/插入到永久存储数据结构中,如SQLite数据库。使用ContentValue
s来防止SQL注入非常重要
另一方面,bundle用于使用Intent
s在活动之间传递数据。比如说,
Bundle bundle = new Bundle();
bundle.putString("name", "John Doe");
Intent intent = new Intent();
intent.putExtras(bundle);
您可以通过执行以下操作在下一个活动中检索捆绑包
:
Bundle bundle = getIntent().getExtras();
String string = bundle.getString("name");
实现相同结果的另一种更常见的方法是:
Intent intent = new Intent();
intent.putExtra("name", "John Doe");
然后在活动中
通过以下方式获得意图
:
Intent receivedIntent = getIntent();
String name = receivedIntent.getStringExtra("name");
通常,像Sql这样的数据库中使用内容值。
我的建议是以s的方式将价值观从一项活动传递到另一项活动
1.捆绑。
2.共同偏好。
3.静态变量
捆绑:-
Bundle b=new Bundle();
b.putString("key","value you need to retrieve in another activity");
b.putString("name","nikhil");
Intent i=new Intent(ctx,Youractivityname.class);
i.putExtras(b);
StartActiviyt(i);
在下一个活动页面中
Intent get=getIntent();
Bundle b=get.getExtras();
if(b!=null){
String name=b.getString("name");
}
共享引用:-
SharedPreferences sp;
SharedPreferences.Editor edit;
sp = getSharedPreferences("enter", MODE_PRIVATE);
edit = sp.edit();
edit.putString("username", nikhil);
edit.commit();
在下一个活动中
SharedPreferences sp = getSharedPreferences("enter", MODE_PRIVATE);
user.setText(sp.getString("username", "default value"));
静态变量:-
在第一项活动中:-
static String s="nikhil";
String n=firstactivity.s
在下一个活动中:-
static String s="nikhil";
String n=firstactivity.s