简体   繁体   English

从2个不同的活动开始相同的活动并更新数据

[英]Start same Activity from 2 different Activity and updating data

I have three activities. 我有三个活动。

In the main activity I have 3 buttons. 在主要活动中,我有3个按钮。 The first and second buttons take me to the other activities. 第一个和第二个按钮将我带到其他活动。 The third button shows me the data I choose from them using an intent. 第三个按钮显示我使用意图从它们中选择的数据。

But I can't see the data from the second and the third activities together. 但是我看不到第二个和第三个活动的数据。 I only see one of them. 我只看到其中之一。

I think each of them creating a different main activity. 我认为每个人都会创建不同的主要活动。 I want just one main activity that sums the data from both the second and the third activities. 我只需要一个主要活动来汇总第二个和第三个活动的数据。

Here is my java code for the main activity: 这是我主要活动的Java代码:

@Override protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    button = (Button) findViewById(R.id.button2);
    button1 = (Button) findViewById(R.id.button3);
    button2 = (Button) findViewById(R.id.button4);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

   textrecievebox.setText(getIntent().getExtras().getString("jockeyno"));

    // Capture button clicks
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {

            // Start NewActivity.class
            Intent myIntent = new Intent(MainActivity.this,
                    Main2Activity.class);
            startActivity(myIntent);
        }
    });
    button1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {

            // Start NewActivity.class
            Intent myIntent1 = new Intent(MainActivity.this,
                    Main3Activity.class);
            startActivity(myIntent1);
        }
    });

    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();

        }
    });
}

public  void showdata(View v)
{


    ImageView show1= (ImageView)findViewById(R.id.imageView3);
    ImageView show2= (ImageView)findViewById(R.id.imageView14);
    ImageView show3= (ImageView)findViewById(R.id.imageView15);
    ImageView show4= (ImageView)findViewById(R.id.imageView16);

    ImageView show5= (ImageView)findViewById(R.id.imageView18);
    ImageView show6= (ImageView)findViewById(R.id.imageView19);
    ImageView show7= (ImageView)findViewById(R.id.imageView20);
    ImageView show8= (ImageView)findViewById(R.id.imageView21);

    TextView textrecievebox= (TextView)findViewById(R.id.textView2);
    textrecievebox.setText(getIntent().getExtras().getString("jockeyno"));
    Bundle bundle=this.getIntent().getExtras();
    int pic=bundle.getInt("image");
   show1.setImageResource(pic);

    Bundle bundle1=this.getIntent().getExtras();
    int pic1=bundle1.getInt("image1");
    show2.setImageResource(pic1);

    Bundle bundle2=this.getIntent().getExtras();
    int pic2=bundle2.getInt("image2");
    show3.setImageResource(pic2);

    Bundle bundle3=this.getIntent().getExtras();
    int pic3=bundle3.getInt("image3");
    show4.setImageResource(pic3);

    Bundle bundle4=this.getIntent().getExtras();
    int pic4=bundle4.getInt("image4");
    show5.setImageResource(pic4);

    Bundle bundle5=this.getIntent().getExtras();
    int pic5=bundle5.getInt("image5");
    show6.setImageResource(pic5);

    Bundle bundle6=this.getIntent().getExtras();
    int pic6=bundle6.getInt("image6");
    show7.setImageResource(pic6);

    Bundle bundle7=this.getIntent().getExtras();
    int pic7=bundle7.getInt("image7");
    show8.setImageResource(pic7);
}

The second activity 第二项活动

@Override protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    jockey = (EditText)findViewById(R.id.editText2);
    Controlpanel = (EditText)findViewById(R.id.editText4);
    Detector = (EditText)findViewById(R.id.editText);
    Extinghuer = (EditText)findViewById(R.id.editText3);

    Pumpspinner=(Spinner)findViewById(R.id.Pumpspinner);
    Pumpspinner1=(Spinner)findViewById(R.id.spinner);
    Pumpspinner2=(Spinner)findViewById(R.id.spinner2);
    Pumpspinner3=(Spinner)findViewById(R.id.spinner3);

    Pump=(ImageView)findViewById(R.id.imageView);

    ArrayAdapter adapter =ArrayAdapter.createFromResource(this,R.array.Pumps,android.R.layout.simple_spinner_item);
    Pumpspinner.setAdapter(adapter);
    Pumpspinner.setOnItemSelectedListener(this);

    ArrayAdapter adapter1 =ArrayAdapter.createFromResource(this,R.array.controlpanel,android.R.layout.simple_spinner_item);
    Pumpspinner1.setAdapter(adapter1);
    Pumpspinner1.setOnItemSelectedListener(this);

    ArrayAdapter adapter2 =ArrayAdapter.createFromResource(this,R.array.firedetector,android.R.layout.simple_spinner_item);
    Pumpspinner2.setAdapter(adapter2);
    Pumpspinner2.setOnItemSelectedListener(this);

    ArrayAdapter adapter3 =ArrayAdapter.createFromResource(this,R.array.fireextighuer,android.R.layout.simple_spinner_item);
    Pumpspinner3.setAdapter(adapter3);
    Pumpspinner3.setOnItemSelectedListener(this);


    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}


}

public void onButtonClickSaveData (View v)
{
    Intent mysendIntent = new Intent(Main2Activity.this, MainActivity.class);
    mysendIntent.putExtra("jockeyno", jockey.getText().toString());

    Bundle bundle=new Bundle();
    Bundle bundle1=new Bundle();
    Bundle bundle2=new Bundle();
    Bundle bundle3=new Bundle();
    bundle.putInt("image", R.drawable.jockpypump);
    bundle1.putInt("image1", R.drawable.controlpanel1);
    bundle2.putInt("image2", R.drawable.detector);
    bundle3.putInt("image3", R.drawable.extinguisher1);

    mysendIntent.putExtras(bundle);
    mysendIntent.putExtras(bundle1);
    mysendIntent.putExtras(bundle2);
    mysendIntent.putExtras(bundle3);

    startActivityForResult(mysendIntent,1);
}

The third activity is the same as the second with different images and text. 第三项活动与第二项活动相同,只是图像和文字不同。

What you are doing is this: 您正在执行的操作是这样的:

  • From Main , start Main2 , start new instance of Main -- only have data from Main2 Main启动Main2 ,启动Main 实例-仅具有Main2数据
  • From Main , start Main3 , start new instance of main -- only have data from Main3 Main ,启动Main3 ,启动main 实例-仅具有Main3数据

What you should be doing is this 你应该做的是这个

  • From Main , start Main2 with startActivityForResult() , do stuff in Main2 , end Main2 with setResult() and finish() , store data in Main , repeat for Main3 Main开始,以startActivityForResult()开始Main2 ,在Main2 ,以setResult()finish()结束Main2 ,将数据存储在Main ,重复Main3

With setResult() you can pass the data from Main2 back to Main , same goes for Main3 . 使用setResult()您可以将数据从Main2MainMain3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM