Android填充数组并将其传递给另一个类

时间:2016-01-11 20:38:36

标签: java android arrays

我有一个Service数组类型,但它必须是动态的,因为长度可能因运行时而异,我如何初始化数组?

如何从另一个类中填充此数组? 在Service类中,我有setter和getter

这是我的服务类:

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类中,我有这个数组,我想在使用我的概念和priceDo变量调用onClickAddConcept方法时填充

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

}

1 个答案:

答案 0 :(得分:0)

你的问题很不清楚。据我所知,它看起来并不像是在实例化“服务”。而且,根据定义,数组不能是动态的。您应该使用ArrayList或其他动态大小的数据结构。