如何从一个活动的数据传递到另一个活动,并保存到不同的表? [英] How to pass data from one activity to another activity and save into different table?

查看:154
本文介绍了如何从一个活动的数据传递到另一个活动,并保存到不同的表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能挽救不同类别的数据,然后将它们保存到不同的表通过点击?

Information.java

 保护无效的onCreate(捆绑savedInstanceState){
        Button按钮=(按钮)findViewById(R.id.button6);
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.information);
        txtDate =(EditText上)findViewById(R.id.editText5);
        。字符串日期= txtDate.getText()的toString();
        button.setOnClickListener(新View.OnClickListener(){
            公共无效的onClick(查看为arg0){
                意向意图=新意图(背景下,WorkForce.class);
                startActivity(意向);
            }
        });
}

WorkForce.java

  txtDate1 =(EditText上)findViewById(R.id.editText6);
 按钮btnSaved =(按钮)findViewById(R.id.button5);
        btnSaved.setOnClickListener(新View.OnClickListener(){
            公共无效的onClick(查看为arg0){
                。字符串日期= txtDate1.getText()的toString();
             ts.insertTimeSheet(日期); //从类信息数据
              s1.insertData(DATE1);            }
        });


解决方案

Information.java 数据传递到 Information.java ..如下:
你可以看到<一个href=\"http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android\">here很多关于活动之间传递数据的答案。
在发送活动...

 意向意图=新意图(相关信息。,WorkForce.class);
intent.putExtra(日,日期);
intent.putExtra(时间,时间);
startActivity(意向);

目前劳动力.. ...活动在的OnCreate 方法。

 字符串日期= getIntent()getExtras()的getString(DATE)。;
字符串时间= getIntent()getExtras()的getString(时代)。

因此​​,您可以在发送接收的活动有活动数据...

,现在你可以保存数据。

和不同的数据库 ..这只是一个助手类的设计问题。您只需通过数据库的名称助手类的构造函数(连同所需的上下文实例),而不是硬编码:

 公共类DBOpenHelper扩展SQLiteOpenHelper {    公共DBOpenHelper(上下文的背景下,字符串DBNAME,诠释dbVersion){
        超(背景下,数据库名,空,dbVersion);
    }
...
}

为您与您所需的数据库名称两种操作创建对象。
并作出不同的对象为操作。

Is it possible to save the data from different classes and then save them into different table by one click?

Information.java

 protected void onCreate(Bundle savedInstanceState) {
        Button button=(Button)findViewById(R.id.button6);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.information);
        txtDate = (EditText) findViewById(R.id.editText5);
        String date=txtDate.getText().toString();
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                Intent intent = new Intent(context, WorkForce.class);
                startActivity(intent);
            }
        });
}

WorkForce.java

txtDate1 = (EditText) findViewById(R.id.editText6);
 Button btnSaved=(Button)findViewById(R.id.button5);
        btnSaved.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                String date=txtDate1.getText().toString();
             ts.insertTimeSheet(date);  //data from class Information
              s1.insertData(date1);

            }
        });

解决方案

Pass the data from Information.java to Information.java.. as below: you can see here a lot of answers for data passing between activities. At sending activity...

Intent intent = new Intent(Information.this, WorkForce.class);
intent.putExtra("date",date);
intent.putExtra("time",time);
startActivity(intent);

At WorkForce.. activity...in OnCreate method.

String date = getIntent().getExtras().getString("date");
String time = getIntent().getExtras().getString("time");

Thus you can have data at receiving activity from sending activity...

and now you can save the data.

and for different databases .. It is just a matter of your Helper class design. You can just pass the name of DB to your Helper class constructor (along with required Context instance) instead of hardcoding:

public class DBOpenHelper extends SQLiteOpenHelper {

    public DBOpenHelper(Context context, String dbName, int dbVersion) {
        super(context, dbName, null, dbVersion);
    }
...
}

Create object for your both operations with your desired database name. and make different object for both operations.

这篇关于如何从一个活动的数据传递到另一个活动,并保存到不同的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆