我正在學習Android Studio,並在本週開始使用數據庫。我想對這個應用程序有一點幫助。很簡單。用戶輸入一個字符串並在第一個活動中按下一個按鈕。此字符串,然後被髮送到它與數據庫中的值進行比較,並顯示類似的那些第二個活動:將輸入字符串與數據庫中的字符串進行比較
public class DrinkCategoryActivity extends ListActivity {
public static final String EXTRA_MESSAGE = "message";
private SQLiteDatabase db;
private Cursor cursor;
private Cursor newcursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String messageText = intent.getStringExtra(EXTRA_MESSAGE);
//setContentView(R.layout.activity_drink_category);
ListView listDrinks = getListView();
try{
SQLiteOpenHelper starbucksDatabaseHelper = new StarbucksDatabaseHelper(this);
db = starbucksDatabaseHelper.getReadableDatabase();
/*
SELECT _id, NAME FROM DRINKS;
*/
cursor = db.query("DRINK",
new String[]{"_id", "NAME"},
null, null, null, null, null);
newcursor = cursor.equals(messageText);
CursorAdapter listAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
cursor,
new String[]{"NAME"},
new int[]{android.R.id.text1},
0);
listDrinks.setAdapter(listAdapter);
}catch (SQLiteException ex){
Toast toast = Toast.makeText(this, "Database unavailable",
Toast.LENGTH_LONG);
toast.show();
}
我一直在尋找所有網站上,但我不能找到一個很好的解釋。 數據庫
這是數據庫中是飲料
public class StarbucksDatabaseHelper extends SQLiteOpenHelper{
add a varaible for the db name
private static final String DB_NAME = "starbucks";
version of the database
private static final int DB_VERSION = 1;
StarbucksDatabaseHelper (Context context){
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE DRINK ("
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "NAME TEXT, "
+ "DESCRIPTION TEXT, "
+ "IMAGE_RESOURCE_ID INTEGER);");
insert data
insertDrink(db, "Soy Latte", "Esspresso and steamed soy milk", R.drawable.latte);
insertDrink(db, "Mocaccino", "Esspresso, hot milk, steamed milk foam, and cocoa", R.drawable.cappuccino);
insertDrink(db, "American", "Drip coffee", R.drawable.filter);
}
private static void insertDrink (SQLiteDatabase db,
String name,
String description,
int resourceId){
ContentValues drinkValues = new ContentValues();
drinkValues.put("NAME", name);
drinkValues.put("DESCRIPTION", description);
drinkValues.put("IMAGE_RESOURCE_ID", resourceId);
db.insert("DRINK", null, drinkValues);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
爲什麼添加newcursor = cursor.equals(messageText);?另外,你的問題的答案取決於你想如何匹配字符串與你的數據庫 - 完全相同或包含該字符串,從它開始,以它結束,.....? –
我忘了刪除它。我會喜歡它是否可以通過世界檢查世界,因爲數據庫中的字符串可以有更多的兩個世界,而且我希望它顯示所有類似於用戶輸入的字符串的字符串 – djant
您可以通過給出解釋一個例子 ? –