我对android开发很新。我知道我不应该使用重复onCreate()
,但我不知道如何做到这一点。我正在同一个布局上从sqlite数据库创建两个微调器。
我的代码是:
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import java.util.ArrayList;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create DataHelper object and insert some sample data
DataHelper datahelper = new DataHelper(this);
datahelper.insertBreed ("Beagle");
datahelper.insertBreed ("Bulldog");
datahelper.insertBreed ("Chow Chow");
// Get sample data from the database and display them in the spinner
Spinner spinner = (Spinner) findViewById(R.id.spinnerBreeds);
ArrayList<String> list = datahelper.getAllBreeds();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_layout, R.id.text, list);
spinner.setAdapter(adapter);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create DataHelper object and insert some sample data
DataHelper2 datahelper2 = new DataHelper2(this);
datahelper2.insertAge("1");
datahelper2.insertAge ("2");
datahelper2.insertAge("3");
// Get sample data from the database and display them in the spinner
Spinner spinner = (Spinner) findViewById(R.id.spinnerAge);
ArrayList<String> list = datahelper2.getAllAge();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_layout, R.id.text, list);
spinner.setAdapter(adapter);
}
}
答案 0 :(得分:0)
你有两种方法
protected void onCreate(Bundle savedInstanceState)
在您的代码中,这就是您收到错误的原因。
您应该删除其中一个并将代码放入另一个方法中。
答案 1 :(得分:0)
你不能在你的班级中有2个覆盖相同的方法。
只需将初始化这两个微调器的代码从第二个onCreate()
复制到第一个spinner
。将微调器和适配器的变量名称更改为避免重复。 (例如:spinnerBreeds
- &gt; spinnerAge
/ datahelper
,breedsDataHelper
- &gt; ageDataHelper
/ adapter
,breedsAdapter
- &gt ; ageAdapter
/ @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create DataHelper object and insert some sample data
DataHelper datahelper = new DataHelper(this);
datahelper.insertBreed ("Beagle");
datahelper.insertBreed ("Bulldog");
datahelper.insertBreed ("Chow Chow");
// Get sample data from the database and display them in the spinner
Spinner spinnerBreeds= (Spinner) findViewById(R.id.spinnerBreeds);
ArrayList<String> list = datahelper.getAllBreeds();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_layout, R.id.text, list);
spinnerBreeds.setAdapter(adapter);
//Init second spinner
DataHelper2 datahelper2 = new DataHelper2(this);
datahelper2.insertAge("1");
datahelper2.insertAge ("2");
datahelper2.insertAge("3");
// Get sample data from the database and display them in the spinner
Spinner spinnerAge = (Spinner) findViewById(R.id.spinnerAge);
ArrayList<String> list2 = datahelper2.getAllAge();
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, R.layout.spinner_layout, R.id.text, list2);
spinnerAge.setAdapter(adapter2);
}
)
set