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



我如何确切地从两个不同(a和b(活动中检索数据,我想在另一个活动(活动C(中显示。也许我可以像这样绘制 - a和b包含应该在C中显示的数据。

,但是问题是当我从A中收到数据时,我从B中获取数据(这次我还从B中收到数据(。来自A的数据丢失或类似于"重置"。当我从B中获取数据时,此问题也存在,并且从A。

获取数据。

我正在使用以下代码:

活动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:我只想在EditText

中显示

//这里这个问题来自

    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);

这就是我将如何从活动c

进行活动A和B
    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);
}

解决该问题或通过数据的其他技巧的任何想法。对不起,我的英语不好,我只是Android的初学者。

您可以使用共享首选项存储数据并以后使用它们,这是示例

将数据存储在共享首选项中

1(在活动中

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(在活动C 中使用它们

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

如果仅从b和b从a接收,则添加从b

中启动c接收到的捆绑包
Activity B
...
intent.putExtras(receivingBundle):
...

如果您想从A或B启动活动C, C仅从开始活动中接收数据。

我理解这是,您要将数据从A到B,然后A和B的数据传递到C。如果是这样,则必须从B到C。但是您只发送B数据,正如我所看到的。

在您的B活动中尝试一下

@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);
}

如果我理解您的问题错误,请纠正我。

相关内容

最新更新