pass data to three activities using intent

2019-09-16 11:38发布

问题:

I have three activities and the flow should like Information>>workForce>>workDetailsTable. I pass the data by using intent. However, when I see the data stored in SQLite, I only able to see the values saved in WorkForce table. I getting NULL value in Information table. Why would this happen? It is only able to pass the data to two activities only?

Information

 button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                Intent intent = new Intent(context, WorkForce.class);
                a = spinner.getSelectedItem().toString();
                intent.putExtra("a",a);
                startActivity(intent);
            }
        });

WorkForce

button.setOnClickListener
                (
                        new View.OnClickListener() {
                        public void onClick(View v) {
                        Intent intent = new Intent(context, WorkDetailsTable.class);
                         subCon=txt1.getText().toString();
                         startActivity(intent);


                            }
                        }
                );

WorkDetailsTable

 final String name=getIntent().getExtras().getString("a");
 final String subContractors=getIntent().getExtras().getString("subCon");
Button btn1=(Button)findViewById(R.id.button2);
        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {

                ts.insertTimeSheet(name);
                WF.insertWorkForce(subContractors);

回答1:

you are not passing subCon string through intent to workdetails table. pass them like you did in information activity. also you need to get value of a from intent in workforce and pass the same value in intent to workdetails.

so your code for WorkForce would be like below.

button.setOnClickListener ( new View.OnClickListener() {
      public void onClick(View v) {
          Intent intent = new Intent(context,WorkDetailsTable.class);
          subCon=txt1.getText().toString();
          intent.putExtra("a",getIntent().getExtras().getString("a"));
          intent.putExtra("subCon",subCon);
          startActivity(intent);
      }
    });


回答2:

It doesn't seem like you are passing the data using Intent while launching WorkDetailsTable. You probably want it to look like this:

String name=getIntent().getExtras().getString("a");

button.setOnClickListener
            (
                    new View.OnClickListener() {
                    public void onClick(View v) {
                    Intent intent = new Intent(context, WorkDetailsTable.class);
                     subCon=txt1.getText().toString();
                     intent.putExtra("a",name);
                     intent.putExtra("subCon",subCon);
                     startActivity(intent);


                        }
                    }
            );

But this is probably not the best way you want to do this. Have you considered using SharedPreferences instead? This would allow you to store information in one activity and retrieve in any other activity.

There's a good example here.