我在数据库中搜索数据时遇到了很大的问题。
我想从数据库获取数据到另一个活动,您可以在其中搜索数据库中的特定数据。
在DatabaseHandler的代码下面:
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 2;
private static final String TAG = "DatabaseHandler";
private static final String DATABASE_NAME = "studentsManager",
TABLE_STUDENTS = "students",
KEY_ID = "id",
KEY_FIRSTNAME = "imie",
KEY_SURNAME = "nazwisko",
KEY_INDEKS = "indeks",
KEY_EMAIL = "email",
KEY_DEPARTMENT = "wydzial",
KEY_NUMER = "numer";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_STUDENTS + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_FIRSTNAME + " TEXT NOT NULL," + KEY_SURNAME + " TEXT NOT NULL," + KEY_INDEKS + " INTEGER," + KEY_EMAIL + " TEXT NOT NULL," + KEY_DEPARTMENT + " TEXT NOT NULL," + KEY_NUMER + " TEXT NOT NULL)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data.");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_STUDENTS);
onCreate(db);
}
}
和向数据库添加数据的AddActivity代码。
public class AddActivity extends AppCompatActivity implements OnClickListener, AdapterView.OnItemSelectedListener {
private DatabaseHandler dbCreate;
EditText etImie, etNazwisko, etIndeks, etEmail, etTelefon, etShow;
Button btnSave, btnDisplay;
Spinner sSpinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
etImie = (EditText) findViewById(R.id.etImie);
etNazwisko = (EditText) findViewById(R.id.etNazwisko);
etIndeks = (EditText) findViewById(R.id.etIndeks);
etEmail = (EditText) findViewById(R.id.etEmail);
etTelefon = (EditText) findViewById(R.id.etTelefon);
etShow = (EditText) findViewById(R.id.etShow);
sSpinner = (Spinner) findViewById(R.id.sSpinner);
sSpinner.setOnItemSelectedListener(this);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.departments_list, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sSpinner.setAdapter(adapter);
btnSave = (Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(this);
btnDisplay = (Button) findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(this);
dbCreate = new DatabaseHandler(this.getApplicationContext());
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_baza_stud, menu);
return true;
}
private void insertInUserTable(String imie, String nazwisko, int indeks, String email, String wydzial, String telefon){
SQLiteDatabase db = dbCreate.getReadableDatabase();
ContentValues data = new ContentValues();
data.put("imie", imie);
data.put("nazwisko", nazwisko);
data.put("indeks", indeks);
data.put("email", email);
data.put("wydzial", wydzial);
data.put("numer", telefon);
db.insertOrThrow("students", null, data);
}
public void onClick(View v) {
if(v.getId() == R.id.btnSave) {
try {
String imie = etImie.getText().toString();
String nazwisko = etNazwisko.getText().toString();
String sIndex = etIndeks.getText().toString();
int indeks = Integer.parseInt(sIndex);
String email = etEmail.getText().toString();
String wydzial = sSpinner.getSelectedItem().toString();
String telefon = etTelefon.getText().toString();
if(imie.length() == 0 || nazwisko.length() == 0 || sIndex.length() == 0 || email.length() == 0 || wydzial.length() == 0 || telefon.length() == 0){
Toast.makeText(getApplicationContext(),"Uzupełnij brakujące pola.", Toast.LENGTH_SHORT).show();
}else{
insertInUserTable(imie, nazwisko, indeks, email, wydzial, telefon);
Toast.makeText(getApplicationContext(), "Utworzono studenta: " + imie + " " + nazwisko + " .", Toast.LENGTH_SHORT).show();
}
} catch (SQLiteException e) {
System.err.println(e.toString());
} finally {
dbCreate.close();
}
}
else if(v.getId() == R.id.btnDisplay){
try{
displayData();
} catch(SQLiteException e){
System.err.println(e.toString());
} finally {
dbCreate.close();
}
}
}// end of onClick method
private static String[] allCol = {"id","imie","nazwisko","indeks","email","wydzial","numer"};
private void displayData() {
SQLiteDatabase db = dbCreate.getReadableDatabase();
Cursor c = db.query("students", allCol, null, null, null, null, null);
startManagingCursor(c);
StringBuilder resultString;
resultString = new StringBuilder("Studenci: ");
while(c.moveToNext()) {
resultString.append(" \n").append(c.getLong(0)).append(". ").append(c.getString(1));
resultString.append(" ").append(c.getString(2)).append(", ").append(c.getInt(3));
resultString.append(", ").append(c.getString(4)).append(", ");
resultString.append(c.getString(5)).append(", ").append(c.getString(6));
}
etShow.setText(resultString);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String item = parent.getItemAtPosition(position).toString();
Toast.makeText(getApplicationContext(),
"Selected department: " + item, Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getApplicationContext(),
"Please, select department.", Toast.LENGTH_SHORT).show();
}
}