将微调器数据存储在SQLite数据库中,并将其显示在另一个活动中

时间:2016-07-11 07:54:36

标签: android android-sqlite android-spinner

在我的主要活动中,名为EditText的{​​{1}}获得了date 这是我的NullPointerException

MainActivity

这是我的第二个活动,应该在public class MainActivity extends AppCompatActivity implements View.OnClickListener { Spinner menuList; EditText menu; EditText date; EditText time; CheckBox dailyAlarm; CheckBox reminder; String item; String setTime; String setDate; String menuDetails; String spItem; private int mYear,mMonth,mDay,mHour,mMinute; ArrayList<String> a= new ArrayList<String>(); String[] from = {DBHelper.COLUMN_DATE,DBHelper.COLUMN_TIME,DBHelper.COLUMN_MENU}; int[] to = {R.id.spList,R.id.date,R.id.time,R.id.menu}; private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); date = (EditText) findViewById(R.id.datePicker); time = (EditText) findViewById(R.id.timePicker); listView = (ListView) findViewById(R.id.listView); menuList = (Spinner) findViewById(R.id.menuList); //spItem = menuList.getSelectedItem().toString(); menu = (EditText) findViewById(R.id.menuET); DBHelper2 helper2 = new DBHelper2(this); menuDetails = menu.getText().toString(); helper2.insert(setDate,setTime,menuDetails); a = (ArrayList<String>) helper2.getAllInfo(); /* ArrayAdapter adp = new ArrayAdapter(this,android.R.layout.simple_list_item_1,a); listView.setAdapter(adp); */ //ArrayList<String> getInfo = (ArrayList<String>) helper2.getAllInfo(); //SimpleAdapter info = new SimpleAdapter(this,R.layout.activity_show_result,getInfo,from,to,0); DBSpinner dbSpinner = new DBSpinner(this); dbSpinner.insert("Breakfast"); dbSpinner.insert("Lunch"); dbSpinner.insert("Dinner"); ArrayList<String> list = dbSpinner.getAll(); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.spinner,R.id.spinnerID,list); menuList.setAdapter(adapter); menuList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { item = parent.getItemAtPosition(position).toString(); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); date.setOnClickListener(this); time.setOnClickListener(this); } public void onClick(View v){ if (v == date){ final Calendar c = Calendar.getInstance(); mYear = c.get(Calendar.YEAR); mMonth = c.get(Calendar.MONTH); mDay = c.get(Calendar.DAY_OF_MONTH); DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { date.setText(dayOfMonth +"-"+(monthOfYear+1)+"-"+year); } },mYear, mMonth,mDay); datePickerDialog.show(); setDate = date.getText().toString(); } else if (v == time){ final Calendar c = Calendar.getInstance(); mHour = c.get(Calendar.HOUR_OF_DAY); mMinute = c.get(Calendar.MINUTE); TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { time.setText(hourOfDay +" : "+minute); } },mHour,mMinute,false); timePickerDialog.show(); setTime = time.getText().toString(); } } public void save(View view){ Intent intent = new Intent(getApplicationContext(),ShowResult.class); intent.putExtra("menuList",item); intent.putExtra("menu",menuDetails); intent.putExtra("date",setDate); intent.putExtra("time",setTime); startActivity(intent); } } 中显示结果。它没有:

TextView

要将 public class ShowResult extends AppCompatActivity { TextView menulist; TextView menu; TextView date; TextView time; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_show_result); menulist = (TextView) findViewById(R.id.spList); menu = (TextView) findViewById(R.id.menu); date = (TextView) findViewById(R.id.date); time = (TextView) findViewById(R.id.time); Bundle intent = getIntent().getExtras(); String menuListS = intent.getString("menuList"); String menuS = intent.getString("menu"); String dateS = intent.getString("date"); String timeS = intent.getString("time"); menulist.setText(menuListS); menu.setText(menuS); date.setText(dateS); time.setText(timeS); } } 数据存储在Spinner中,请执行以下操作:

SQLiteDatabase

要将其他数据存储在 public class DBSpinner extends SQLiteOpenHelper { public static final String DATABASE_NAME = "spinner.db"; public static final String TABLE_NAME = "spinner"; public static final int DATABASE_VERSION = 1; public static final String CREATE_TABLE = " CREATE TABLE " + TABLE_NAME +" (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NULL UNIQUE )"; public DBSpinner(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_TABLE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(db); } public void insert(String name){ SQLiteDatabase db = this.getWritableDatabase(); db.beginTransaction(); ContentValues values; try{ values = new ContentValues(); values.put("name",name); db.insert(TABLE_NAME,null,values); db.setTransactionSuccessful(); }catch (Exception ex){ ex.printStackTrace(); }finally { db.endTransaction(); db.close(); } } public ArrayList<String> getAll(){ ArrayList<String> list = new ArrayList<String>(); SQLiteDatabase db = this.getReadableDatabase(); db.beginTransaction(); try{ String query = "SELECT * FROM "+ TABLE_NAME; Cursor cursor =db.rawQuery(query,null); if(cursor.getCount() > 0){ while (cursor.moveToNext()){ String name = cursor.getString(cursor.getColumnIndex("name")); list.add(name); } } db.setTransactionSuccessful(); }catch (Exception ex){ ex.printStackTrace(); }finally { db.endTransaction(); db.close(); } return list; } } 中,请执行以下操作:

SQLiteDatabase

我是Android新手。关于如何解决这个问题的任何建议?

0 个答案:

没有答案