将子活动的数据发送到主活动

时间:2015-09-12 13:29:08

标签: java android android-intent android-activity

在尝试了很多在这里找到的东西后,没有任何效果。

简化:

我有一个带按钮的主要活动。

我有"孩子" 活动 2个文字字段按钮

点击主要活动按钮后,子活动会打开(可行)。然后我将一些文本添加到两个文本字段,当我点击按钮时,我希望文本字段中的数据转移到主要活动,我无法解决这个问题。

这是我的主要活动代码

public class MainMenu extends AppCompatActivity {

Button btnAddBib;



public final static int REQUEST_CODE_B = 1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);


    btnAddBib = (Button)findViewById(R.id.btnAddBib);

    btnAddBib.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent myIntent = new Intent(v.getContext(), MainActivity.class);
            startActivityForResult(myIntent,REQUEST_CODE_B);

        }
    });


}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(requestCode) {
        case REQUEST_CODE_B:
            String Qte = data.getStringExtra("qte");
            String Time = data.getStringExtra("time");


            if (Qte != null && !Qte.isEmpty() && Time != null && !Time.isEmpty()) {
                final SQLiteDatabase dbBib;
                dbBib = openOrCreateDatabase("bibi", Context.MODE_PRIVATE, null);
                dbBib.execSQL("CREATE TABLE IF NOT EXISTS bibi(time VARCHAR, qte VARCHAR);");
                dbBib.execSQL("INSERT INTO bibi VALUES ('" + Time + "','" +
                        Qte + "')");


                Toast.makeText(getApplicationContext(), "Bibi bien enregistré",
                        Toast.LENGTH_LONG).show();
            } else {

                Toast.makeText(getApplicationContext(), "Fail get extraData",
                        Toast.LENGTH_LONG).show();

            }

           break;
    }
}

儿童活动代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ajouter_bibi);

    btnValideBib = (Button)findViewById(R.id.btnValideBib);
    btnValideBib.setBackgroundResource(R.drawable.logo_biberon);

    txtTime = (EditText)findViewById(R.id.txtTime);
    txtQte = (EditText)findViewById(R.id.txtQte);

    Calendar c = Calendar.getInstance();
    int hh = c.get(Calendar.HOUR_OF_DAY);
    int mm = c.get(Calendar.MINUTE);

    txtTime.setText(hh + ":" + mm);

    btnValideBib.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent backIntent = new Intent();
            backIntent.putExtra("qte", txtQte.getText());
            backIntent.putExtra("time", txtTime.getText());

            setResult(RESULT_OK, backIntent);
            finish();

        }
    });

问题:

在主要活动中,当我想要取回我的额外字符串时,我有空字段。

        String Qte = data.getStringExtra("qte");
        String Time = data.getStringExtra("time");

它出了什么问题?

谢谢!

1 个答案:

答案 0 :(得分:2)

更改代码:

backIntent.putExtra("qte", txtQte.getText());
backIntent.putExtra("time", txtTime.getText());

以下代码:

backIntent.putExtra("qte", txtQte.getText().toString()); //append .toString() method
backIntent.putExtra("time", txtTime.getText().toString());