如何从两个不同的活动检索数据并显示在另一个不同的活动

问题描述:

如何从两个不同的活动(可以说是:A和B)中检索数据,并且我想在另一个活动(活动C)中显示。也许我可以这样画--A和B包含应该在C中显示的数据。就像那样简单。如何从两个不同的活动检索数据并显示在另一个不同的活动

但问题是,当我从B接收数据(此时我也接收来自B的数据)之后,我从A接收数据。来自A的数据缺失或与“重置”一样。

活性A:当我接收来自B数据和予

我使用以下代码取从A数据这个问题也存在这种使用意图

public static final String EXTRA_COUNTRY_SITE = "EXTRA_COUNTRY_SITE"; 
private static final int REQUEST_RESPONSE_SITE = 1; 

class ItemLists implements AdapterView.OnItemClickListener{ 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     TextView tvs = (TextView)view.findViewById(R.id.textViewSiteName); 
    // Lets say textview just like String "Data A" 
     Intent intent = new Intent(view.getContext(), LoginActivityView.class); 
     intent.putExtra(EXTRA_COUNTRY_SITE, tvs.getText().toString()); 
    // tvs.getText().toString() here you can change it to String 
     startActivityForResult(intent, REQUEST_RESPONSE_SITE); 
    } 
} 

活动如何发送的数据B:这是用数据意向

public static final String EXTRA_COUNTRY_EMPLOYEE = "EXTRA_COUNTRY_EMPLOYEE"; 
private static final int REQUEST_RESPONSE_EMPLOYEE = 1; 

class ItemListe implements AdapterView.OnItemClickListener{ 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     TextView tve = (TextView)view.findViewById(R.id.textViewEmpName); 
    // Lets say textview just like String "Data B" 
     Intent intent = new Intent(view.getContext(), LoginActivityView.class); 
     intent.putExtra(EXTRA_COUNTRY_EMPLOYEE, tve.getText().toString()); 
    // tvs.getText().toString() here you can change it to String 
     startActivityForResult(intent, REQUEST_RESPONSE_EMPLOYEE); 
    } 
} 

活动C I发送方式:我只是想在的EditText

展示10

//下面这个问题从

String country_emp = getIntent().getStringExtra(ViewEmployeeActivity.EXTRA_COUNTRY_EMPLOYEE); 
    EditText editTextEmployee= (EditText) findViewById(R.id.editTextEmployee); 
    editTextEmployee.setText(country_emp); 

    String country_site = getIntent().getStringExtra(ViewSiteActivity.EXTRA_COUNTRY_SITE); 
    EditText editTextSite= (EditText) findViewById(R.id.editTextSite); 
    editTextSite.setText(country_site); 

来了,这是怎么了,我从活动Ç

public void click(View v) { 
    Intent intent = null; 
    switch(v.getId()) { 
     case R.id.editTextSite: 
      intent = new Intent(this, ViewSiteActivity.class); 
      break; 
     case R.id.editTextEmployee: 
      intent = new Intent(this, ViewEmployeeActivity.class); 
    } 
    startActivity(intent); 
} 

任何想法去活动A和B来解决这个问题或其他技巧来传递数据。对不起,我的英语不好,我只是Android的初学者。

+0

使用意图来传递数据是跨活动共享数据的行的方式。你的确切工作流程是什么?如果你从'A - > B - > C',那么你需要按照这个顺序通过intent传递数据。 –

+0

我的工作流程是A&B - > C就是这个 –

+0

没有A&B ......你在Android中从一个活动跳到另一个活动。只需通过意图传递跨活动所需的数据即可。 –

,可以使用共享首选项来存储数据,后来使用它们, 这里的例子

要在共享偏好将数据存储

1)在活动A

SharedPreferences sharedpreferences; 
sharedpreferences = getSharedPreferences("myPref", Context.MODE_PRIVATE); 
SharedPreferences.Editor editor = sharedpreferences.edit(); 
      editor.putString(EXTRA_COUNTRY_SITE,tvs.getText().toString()); 
      editor.commit(); 

2)在活动B

SharedPreferences sharedpreferences; 
sharedpreferences = getSharedPreferences("myPref", Context.MODE_PRIVATE); 
SharedPreferences.Editor editor = sharedpreferences.edit(); 
      editor.putString(EXTRA_COUNTRY_EMPLOYEE,tvs.getText().toString()); 
      editor.commit(); 

3)在活动Ç使用它们

SharedPreferences prefs = this.getSharedPreferences("myPref", Context.MODE_PRIVATE); 
String countrySite = prefs.getString(EXTRA_COUNTRY_SITE, null); 
String countryEmp = prefs.getString(EXTRA_COUNTRY_EMPLOYEE, null); 
+0

感谢兄弟多数民众赞成真棒,解决我的问题。我希望我可以upvote你的答案。 :D XD –

+0

谢谢:)希望有人会 –

若C只从B和B只有从A然后添加束从A接收到的意图在乙

Activity B 
... 
intent.putExtras(receivingBundle): 
... 

如果想从A或B, c。启动活动Ç起始物C接收只接收来自开始活动的数据。

+0

我首先从C开始活动,然后开始活动A或B.在这里,如果我的工作流程仍然令人困惑,或者我会混淆解释它。来自A&B的数据 - > C。 –

我的理解是,你想将数据从A传递给B,然后把A和B的数据传递给C.如果是这样的话,你必须将A和B数据都从B发送到C.但是你是只发送B数据,因为我可以看到代码。

在B活性click事件

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    TextView tve = (TextView)view.findViewById(R.id.textViewEmpName); 
// Lets say textview just like String "Data B" 
    Intent intent = new Intent(view.getContext(), LoginActivityView.class); 
    intent.putExtra(EXTRA_COUNTRY_EMPLOYEE, tve.getText().toString()); 
    intent.putExtra(ViewEmployeeActivity.EXTRA_COUNTRY_EMPLOYEE, getIntent().getStringExtra(ViewEmployeeActivity.EXTRA_COUNTRY_EMPLOYEE)); 
// tvs.getText().toString() here you can change it to String 
    startActivityForResult(intent, REQUEST_RESPONSE_EMPLOYEE); 
} 

纠正我,如果我理解你的问题错试试这个。

+0

我的意思是我想从a和b接收数据,而不是a或b。然后传递给活动C ... –

+0

然后为A和B活动维护两个公共静态字符串,并在各个活动中的onclick事件之后将数据设置为字符串。然后,您可以从C活动访问A和B字符串。 – Chowdary102

+0

只需在B活动的onclick事件中添加此行,这将解决您的需求,我猜 intent.putExtra(ViewEmployeeActivity.EXTRA_COUNTRY_EMPLOYEE,getIntent()。getStringExtra(ViewEmployeeActivity.EXTRA_COUNTRY_EMPLOYEE)); – Chowdary102