Android 我有一个微调器,可以通过EditText动态获取其项。它存储在SQLite中。如何在开始处添加单个项目?
我试图添加一个对微调器没有价值的项目。我希望用户在活动启动时查看、选择项目。如何在onCreate中将单个项目添加到微调器?请记住,用户可以通过按钮向微调器动态添加项目并编辑文本。Rite现在微调器中的第一件事是添加到数据库中的第一项。如果未向数据库添加任何内容,则微调器为空 这是我的密码 主要活动Android 我有一个微调器,可以通过EditText动态获取其项。它存储在SQLite中。如何在开始处添加单个项目?,android,sqlite,android-sqlite,android-spinner,Android,Sqlite,Android Sqlite,Android Spinner,我试图添加一个对微调器没有价值的项目。我希望用户在活动启动时查看、选择项目。如何在onCreate中将单个项目添加到微调器?请记住,用户可以通过按钮向微调器动态添加项目并编辑文本。Rite现在微调器中的第一件事是添加到数据库中的第一项。如果未向数据库添加任何内容,则微调器为空 这是我的密码 主要活动 public class MainActivity extends Activity implements OnItemSelectedListener { Button AddBtn; Edit
public class MainActivity extends Activity implements OnItemSelectedListener {
Button AddBtn;
EditText et;
EditText cal;
Spinner spn;
SQLController SQLcon;
ProgressDialog PD;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AddBtn = (Button) findViewById(R.id.addbtn_id);
et = (EditText) findViewById(R.id.et_id);
cal = (EditText) findViewById(R.id.et_cal);
spn = (Spinner) findViewById(R.id.spinner_id);
spn.setOnItemSelectedListener(this);
SQLcon = new SQLController(this);
// opening database
SQLcon.open();
loadtospinner();
AddBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new MyAsync().execute();
}
});
}
public void loadtospinner() {
Cursor c = SQLcon.readData();
ArrayList<String> al = new ArrayList<String>();
c.moveToFirst();
while (!c.isAfterLast()) {
String name = c.getString(c.getColumnIndex(DBhelper.MEMBER_NAME));
al.add(name);
c.moveToNext();
}
ArrayAdapter<String> aa1 = new ArrayAdapter<String>(
getApplicationContext(), R.layout.spinner_item, R.id.textView1,
al);
spn.setAdapter(aa1);
// closing database
SQLcon.close();
}
private class MyAsync extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
PD = new ProgressDialog(MainActivity.this);
PD.setTitle("Please Wait..");
PD.setMessage("Loading...");
PD.setCancelable(false);
PD.show();
}
@Override
protected Void doInBackground(Void... params) {
String name = et.getText().toString();
String calories = cal.getText().toString();
// opening database
SQLcon.open();
// insert data into table
SQLcon.insertData(name, calories);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
loadtospinner();
PD.dismiss();
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
String getName;
getName = "";
String getCalories;
getCalories = "";
SQLController handlerInfo = new SQLController(getBaseContext());
handlerInfo.open();
Cursor cursor = handlerInfo.readData();
if (cursor.moveToFirst()) {
do {
getName = cursor.getString(1);
getCalories = cursor.getString(2);
} while (cursor.moveToNext());
}
handlerInfo.close();
// Showing selected spinner item
Toast.makeText(parent.getContext(), "You selected: " + getCalories,
Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
数据库和助手
public class DBhelper extends SQLiteOpenHelper {
// TABLE INFORMATTION
public static final String TABLE_MEMBER = "member";
public static final String MEMBER_ID = "_id";
public static final String MEMBER_NAME = "name";
public static final String KEY_CALORIES = "calories";
// DATABASE INFORMATION
static final String DB_NAME = "MEMBER.DB";
static final int DB_VERSION = 2;
// TABLE CREATION STATEMENT
private static final String CREATE_TABLE = "create table " + TABLE_MEMBER
+ "(" + MEMBER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ MEMBER_NAME + " TEXT NOT NULL," + KEY_CALORIES
+ " INT NOT NULL);";
public DBhelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TABLE_MEMBER);
onCreate(db);
}
}
public class SQLController {
private DBhelper dbhelper;
private Context ourcontext;
private SQLiteDatabase database;
public SQLController(Context c) {
ourcontext = c;
}
public SQLController open() throws SQLException {
dbhelper = new DBhelper(ourcontext);
database = dbhelper.getWritableDatabase();
return this;
}
public void close() {
dbhelper.close();
}
public void insertData(String name, String calories) {
ContentValues cv = new ContentValues();
cv.put(DBhelper.MEMBER_NAME, name);
cv.put(DBhelper.KEY_CALORIES, calories);
database.insert(DBhelper.TABLE_MEMBER, null, cv);
}
public Cursor readData() {
String[] allColumns = new String[] { DBhelper.MEMBER_ID,
DBhelper.MEMBER_NAME, DBhelper.KEY_CALORIES };
Cursor c = database.query(DBhelper.TABLE_MEMBER, allColumns, null,
null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
}
Urrrm-只需在用于填充微调器的ArrayList中添加一项即可?@Squonk Urrrm-您完全正确。哈哈。谢谢。@Squonk我可以将它添加到列表中,但它没有显示应用程序首次安装的时间。单击微调器时显示,但在单击之前不可见。我的目标是让它在一开始就可见。有什么建议吗?