我正在尝试使用我在android中设计的简单菜单动态创建按钮。但是,我在创建按钮后出现问题时遇到问题。我将信息(Car对象)保存到ArrayList并使用它来设置按钮的文本。我遇到的另一个问题是,只有最近创建的按钮才会显示在子布局上。 import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private double mpg;
private int counter=0;
private ArrayList<Car> cars = new ArrayList<Car>(0);
private ArrayList<Button> buttons = new ArrayList<Button>(0);
LinearLayout container;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
container = (LinearLayout)findViewById(R.id.layout);
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.add:
Intent intent = new Intent(MainActivity.this, AddActivity.class);
startActivity(intent);
updateView();
return true;
case R.id.delete:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void updateView(){
counter++;
}
}
public class Car {
private double milesPerGallon;
private String make, model, year;
public Car(String ma, String mo, String y, double mpg){
make=ma;
model=mo;
year=y;
milesPerGallon=mpg;
Log.w("d", "in the constructor"+make+model+year);
}
public void setMilesPerGallon(double mpg){
this.milesPerGallon=mpg;
}
public void setMake(String make){
this.make=make;
}
public void setModel(String model){
this.model=model;
}
public void setYear(String year){
this.year=year;
}
public double getMilesPerGallon(){
return milesPerGallon;
}
public String getMake(){
return this.make;
}
public String getModel(){
return this.model;
}
public String getYear(){
return this.year;
}
public String toString(){
return "Make "+make+", Model "+model+", Year"+year+", MPG "+milesPerGallon;
}
}
public class AddActivity extends AppCompatActivity {
static String make, model, year;
static double mpg;
static Car car;
EditText makeF, modelF, yearF, milesOnOneF, tankSizeF;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
makeF = (EditText)findViewById(R.id.makeField);
modelF = (EditText)findViewById(R.id.modelField);
yearF = (EditText)findViewById(R.id.yearField);
milesOnOneF = (EditText)findViewById(R.id.milesField);
tankSizeF = (EditText)findViewById(R.id.tankSizeField);
}
public void createCar(View view){
//Will crash if the Double values don't exist
make=makeF.getText().toString();
model=modelF.getText().toString();
year=yearF.getText().toString();
mpg=(Double.parseDouble(milesOnOneF.getText().toString())/Double.parseDouble(tankSizeF.getText().toString()));
car = new Car(make, model, year, mpg);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
public String getMake(){
return make;
}
public String getModel(){
return model;
}
public String getYear(){
return year;
}
public double getMPG(){
return mpg;
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="tf.tftripbuddy.MainActivity">
<GridLayout
android:id="@+id/gridLayout"
android:layout_width="368dp"
android:layout_height="461dp"
android:layout_marginLeft="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.96">
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></LinearLayout>
</GridLayout>
<TextView
android:id="@+id/textView"
android:layout_width="368dp"
android:layout_height="60dp"
android:layout_marginTop="4dp"
android:text="Garage"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>