【问题标题】:passing bundle/ContentValues between forms and the difference between ContentValues between bundle在表单之间传递 bundle/ContentValues 以及在 bundle 之间传递 ContentValues 的区别
【发布时间】:2013-11-25 12:18:08
【问题描述】:

我已尝试将 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;
        }

最后,Bundles 和 ContentValues 之间有什么区别。我尝试阅读 google android 教程,但它让我更加困惑。

【问题讨论】:

    标签: java android xml sqlite


    【解决方案1】:

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

    另一方面,Bundles 用于使用Intents 在Activities 之间传递数据。例如,

    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");
    

    【讨论】:

      【解决方案2】:

      通常内容值用于 Sql 等数据库中。 我建议以某种方式将值从一项活动传递到另一项活动。

      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:-

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-18
        • 1970-01-01
        • 1970-01-01
        • 2011-03-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多