2016-01-11 64 views
0

我有一個Service數組類型,但它必須是動態的,因爲長度可能會在運行時有所不同如何初始化數組?Android填充一個數組並將其傳遞給另一個類

如何從另一個類中填充此數組? 服務類我有getter和setter方法

這是我的服務類:

public class Service { 
int id; 
String name; 
Double price; 

public Service(int id, String name, Double price) { 
    super(); 
    this.id = id; 
    this.name = name; 
    this.price = price; 
} 

public Service(String name, Double price) { 
    this.name = name; 
    this.price = price; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public Double getPrice(){ 
    return price; 
} 

public void setPrice(Double price){ 
    this.price = price; 
} 

public int getId() { 
    return id; 
} 

public void setId(int id) { 
    this.id = id; 
} 

}

這是我的條例草案類

public class Bill { 
Service service[]; 

public Bill(){ 

} 

public Bill(Service service[]) { 
    this.service = service; 
} 

public Service[] getService() { 
    return service; 
} 

public void setService(Service service[]) { 
    this.service = service; 
} 

在我ConceptsActivity I類有這個數組,我想填充時調用onClickAddConcept方法它與我的概念和priceDo變量

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 EditText price; 
private Button addConcept; 
private String concept; 
private double priceDo; 
private List<AddConceptsListView> myConcept = new ArrayList<AddConceptsListView>(); 
private Service service[]; 

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

    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); 
     if(price.getText().toString().contains(".")){ 
      priceDo = Double.parseDouble(price.getText().toString()); 
      Toast.makeText(getApplicationContext(),"Precio: " + priceDo+ "" + "\n" 
        + "Concepto: " + concept,Toast.LENGTH_SHORT).show(); 
     } 
     else{ 
      priceDo = Double.parseDouble(price.getText().toString()); 
      Toast.makeText(getApplicationContext(),"Precio: " + priceDo+ "" + "\n" 
        + "Concepto: " + concept,Toast.LENGTH_SHORT).show(); 
     } 
    } 
} 

}

回答

0

你的問題是不太清楚。從我可以告訴的是,它看起來並不像你曾經實例化「服務」。另外,根據定義,數組不能是動態的。您應該使用ArrayList或其他動態大小的數據結構。

相關問題