我有活动A和活动B. 活动A用Button打开活动B,我传给它一个数组 在活动B中我修改了数组,当我点击一个按钮时我想要返回或再次打开活动A并将其传递给修改后的数组
有办法吗?
抱歉我的英文
在这种情况下,我有BeginService类,我将数组传递给new ConceptsActivity(bill.getService());
中的另一个活动.ConceualActivity修改数组并将其再次传递给BeginService,依此类推。我这样做是因为我需要为账单添加服务。
这是BeginService类的代码
public class BeginService extends AppCompatActivity {
private Service service[] = new Service[5];
Bill bill = new Bill();
public BeginService(){
}
public BeginService(Service service[]){
this.service = service;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
setTitle(R.string.TitleSer);
super.onCreate(savedInstanceState);
setContentView(R.layout.add_concepts);
}
public void openConcepts(View view){
new ConceptsActivity(bill.getService());
Intent intent = new Intent(this,ConceptsActivity.class);
startActivity(intent);
}
ConceptsActivity类
public class ConceptsActivity extends AppCompatActivity{
private int i=0;
private AutoCompleteTextView actv;
private TextView tvConcept;
private TextView tvConcept2;
private TextView warning;
private TextView tvPrice;
private TextView tvbs;
private EditText price;
private Button addConcept;
private String concept;
private double priceDo;
private List<AddConceptsListView> myConcept = new ArrayList<AddConceptsListView>();
private Service[] service; //= new Service[5];
public ConceptsActivity(){
}
public ConceptsActivity(Service service[]){
this.service = service;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
setTitle(R.string.TitleSer);
super.onCreate(savedInstanceState);
setContentView(R.layout.concepts_layout);
for(int j=0; j<service.length; j++){
service[j] = new Service();
if(service[j].getName() != null){
i++;
}
}
actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextViewConcept);
tvConcept = (TextView) findViewById(R.id.textViewConcept);
tvConcept2 = (TextView) findViewById(R.id.textViewConcept2);
warning = (TextView) findViewById(R.id.textViewWarning);
tvPrice = (TextView) findViewById(R.id.textViewPrice);
price = (EditText) findViewById(R.id.editTextPrice);
addConcept = (Button) findViewById(R.id.buttonAddConcept);
//Autocomplete Textview
String[] services = getResources().getStringArray(R.array.services_array);
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, services);
actv.setAdapter(adapter);
actv.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_DONE) {
//make items visible
tvConcept.setVisibility(View.VISIBLE);
tvConcept2.setVisibility(View.VISIBLE);
tvPrice.setVisibility(View.VISIBLE);
price.setVisibility(View.VISIBLE);
addConcept.setVisibility(View.VISIBLE);
concept = actv.getText()+"";
tvConcept2.setText(concept);
//To hide the keyboard
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
handled = true;
}
return handled;
}
});
}
public void onClickAddConcept(View view){
if(price.getText().toString().equals("")){
warning.setVisibility(View.VISIBLE);
}
else {
warning.setVisibility(View.INVISIBLE);
priceDo = Double.parseDouble(price.getText().toString());
Toast.makeText(getApplicationContext(),"Precio: " + priceDo+ "" + "\n"
+ "Concepto: " + concept,Toast.LENGTH_SHORT).show();
service[i].setName(concept);
service[i].setPrice(priceDo);
i++;
for(int j=0; j<service.length; j++){
if(service[j].getName() != null || service[j].getPrice() != null){
Toast.makeText(getApplicationContext(), "Array" + j + "nameConcept: " + service[j].getName() +
"\n" + "Array price: " + service[j].getPrice(), Toast.LENGTH_SHORT).show();
}
}
}
ArrayAdapter<Service> my = new ArrayAdapter<Service>(this, android.R.layout.simple_list_item_1, service);
setContentView(R.layout.add_concepts);
tvbs = (TextView) findViewById(R.id.textViewBeginService);
tvbs.setVisibility(View.INVISIBLE);
ListView mainListView = (ListView) findViewById(R.id.list2);
mainListView.setAdapter(my);
}
答案 0 :(得分:1)
在目标活动中使用setResult
:
Intent resultIntent = new Intent();
resultIntent .putExtra("arrayName", your_array_name);
setResult(Activity.RESULT_OK, resultIntent);
finish();
在您的第一个活动中,添加
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case (MY_CHILD_ACTIVITY) : {
if (resultCode == Activity.RESULT_OK) {
ArrayList<String> myList = (ArrayList<String>) getIntent().getSerializableExtra("arrayName");
// myList gives you the array list
}
break;
}
}
}