2016-01-13 81 views
0

我有活動A和活動B 活動A用Button打開活動B並將其傳遞給數組 在活動BI中修改數組,並在何時我點擊一個按鈕我想要返回或再次打開活動A並將其傳遞給修改過的陣列Android:返回上一個活動並將其傳遞給數組

有沒有辦法做到這一點?

對不起,我的英語

在這種情況下,我有BeginService類和我的數組傳遞到另一個活動new ConceptsActivity(bill.getService());的ConceptsActivity修改數組,並把它傳遞給再等等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); 

} 
+1

看看startActivityForResult/onActicityResult和捆綁 – zgc7009

+0

你有沒有考慮過使用Bundle的 –

+0

什麼是捆綁包? – Alxlly

回答

1

使用setResult在目標活動:

Intent resultIntent = new Intent(); 
resultIntent .putExtra("arrayName", your_array_name); 
setResult(Activity.RESULT_OK, resultIntent); 
finish(); 

在你的第一個活動,增加

​​
+0

問題是我不能使用ArrayList,我需要一個對象類型的數組 – Alxlly

相關問題