Android:数据库帮助-返回特定列中的所有项目
我真的被困住了。我熟悉Java和Android开发,但没有任何数据库经验。这是我第一次。我已经阅读了多个教程,但我无法弄清楚如何从特定行返回所有值并将其设置为单个字符串。
下面是我的数据库控制台。我能够获取所有项目并将其显示在我的视图中,但我打算编写一个特定的函数来仅对 TITLE 列中的所有数据执行某些操作。任何人都可以阐明或提供一个小例程来将 TITLE 列中的所有项目放入单个字符串中吗?最后一个函数是我的 getTitleList,这是我想要收集数据的地方
这是函数:
public void getTitleList(){
cursor = db.rawQuery("SELECT TITLE FROM table", null);
cursor.close();
}
这是我的类
package edu.asu;
import java.util.ArrayList;
导入 android.content.ContentValues; 导入 android.content.Context; 导入 android.content.Intent; 导入 android.database.Cursor; 导入 android.database.sqlite.SQLiteDatabase; 导入 android.os.Bundle; 导入 android.view.Menu; 导入 android.view.MenuInflater; 导入 android.view.MenuItem; 导入 android.widget.AdapterView; 导入 android.widget.AdapterView.OnItemClickListener; 导入 android.widget.AdapterView.OnItemSelectedListener; 导入 android.widget.ListView; 导入 android.widget.SimpleCursorAdapter; 导入 android.widget.Toast; 导入 android.view.View; 导入 android.app.ListActivity;
导入静态 android.provider.BaseColumns._ID;
公共类 DbBookConsole 扩展 ListActivity {
private static String[] FROM = { _ID, DbConstants.TITLE, DbConstants.PRICE,
DbConstants.ISBN, DbConstants.CHECKIN };
private DbCreate books;
private static SQLiteDatabase db;
private static int[] TO = { R.id.rowid, R.id.name, R.id.price, R.id.isbn, R.id.checkin};
private ListView lv1;
private static String itemId;
private static String titles;
private static String test;
private Cursor cursor;
static final int BOOK_CANCELED = 0;
static final int BOOK_ADDED = 1;
static final int BOOK_MODIFIED = 2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showDatabaseContent();
lv1 = getListView();
lv1.setTextFilterEnabled(true);
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
cursor = (Cursor) a.getItemAtPosition(position);
itemId = cursor.getString(0);
openOptionsMenu();
}
});
lv1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
// selected item index from ListView
public void showDialogItemId(long itemId) {
Toast.makeText(this,
"Menu item selected index is" + Long.toString(itemId),
Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.modifyitem:
if (null != itemId) {
Bundle bookToModify = new Bundle();
bookToModify.putString("cTitle", cursor.getString(1));
bookToModify.putString("cPrice", cursor.getString(2));
bookToModify.putString("cISBN", cursor.getString(3));
bookToModify.putString("cCheckin", cursor.getString(4));
bookToModify.putString("mod_type", "modifyBook");
Intent intent = new Intent(this, BookDetails.class);
intent.setClass(this, BookDetails.class);
intent.putExtras(bookToModify);
startActivityForResult(intent, BOOK_MODIFIED);
} else {
Toast
.makeText(this, "Select Book to modify",
Toast.LENGTH_LONG).show();
}
break;
case R.id.additem:
Intent i = new Intent(this, BookDetails.class);
Bundle bun = new Bundle();
bun.putString("mod_type", "addBook");
i.setClass(this, BookDetails.class);
i.putExtras(bun);
startActivityForResult(i, BOOK_ADDED);
break;
case R.id.removeitem:
if (null != itemId) {
removeBook(itemId);
showDatabaseContent();
} else {
Toast
.makeText(this, "Select Book to delete",
Toast.LENGTH_LONG).show();
}
break;
}
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
// See which child activity is calling us back.
switch (resultCode) {
case BOOK_ADDED:
// This is the standard resultCode that is sent back if the
// activity crashed or didn't doesn't supply an explicit result.
if (resultCode == RESULT_FIRST_USER) {
Bundle bundle = new Bundle();
bundle = intent.getBundleExtra("bookData");
addBook(bundle);
showDatabaseContent();
} else {
Toast.makeText(this, "CANCEL BOOK BUTTON PRESSED",
Toast.LENGTH_LONG).show();
}
break;
case BOOK_MODIFIED:
if (resultCode == 2) {
Bundle bundle = new Bundle();
bundle = intent.getBundleExtra("bookData");
modifyBook(bundle);
showDatabaseContent();
} else {
Toast.makeText(this, "MODIFY BOOK FAILED", Toast.LENGTH_LONG)
.show();
}
break;
default:
break;
}
}
// method removes item from database
private void removeBook(String itemId) {
db = books.getWritableDatabase();
db.delete(DbConstants.TABLE_NAME, "_ID=" + itemId, null);
}
private void addBook(Bundle bundle) {
// Insert a new record into the Events data source.
// You would do something similar for delete and update.
db = books.getWritableDatabase();
ContentValues vals = new ContentValues();
vals.put(DbConstants.TITLE, bundle.getString("bookTitle"));
vals.put(DbConstants.PRICE, bundle.getString("bookPrice"));
vals.put(DbConstants.ISBN, bundle.getString("bookISBN"));
vals.put(DbConstants.CHECKIN, bundle.getString("bookCheck"));
db.insertOrThrow(DbConstants.TABLE_NAME, null, vals);
}
// method should modify existing Contact
private void modifyBook(Bundle bundle) {
db = books.getWritableDatabase();
ContentValues vals = new ContentValues();
vals.put(DbConstants.TITLE, bundle.getString("bookTitle"));
vals.put(DbConstants.PRICE, bundle.getString("bookPrice"));
vals.put(DbConstants.ISBN, bundle.getString("bookISBN"));
vals.put(DbConstants.CHECKIN, bundle.getString("bookCheck"));
db.update(DbConstants.TABLE_NAME, vals, _ID + "=" + itemId, null);
}
private Cursor getBooks() {
db = books.getReadableDatabase();
cursor = db.query(DbConstants.TABLE_NAME, FROM, null, null, null, null, null,
null);
startManagingCursor(cursor);
return cursor;
}
public void showDatabaseContent() {
books = new DbCreate(this);
try {
cursor = getBooks();
showBooks(cursor);
} finally {
books.close();
db.close();
}
}
private void showBooks(Cursor cursor) {
// set up data binding
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.item, cursor, FROM, TO);
setListAdapter(adapter);
}
公共 void getTitleList(){ 游标 = db.rawQuery("从表中选择标题", null);
cursor.close();
} }
i am really stuck. I am familiar in Java and Android development but i do not have any experience with databases. This is my first time. I have been going through multiple tutorials but i cannot figure out how return all the values from a specific row and set it to a single string.
Below is my database console. I was able to get all the items and display it in my view but i intended on writing a specific function to do something with all the data in only the TITLE column. Can anyone shed any light or provide a small routine to get all the items from the TITLE column into a single string? The very last function is my getTitleList that is where i would like to gather the data
Here is the function:
public void getTitleList(){
cursor = db.rawQuery("SELECT TITLE FROM table", null);
cursor.close();
}
Here is my class
package edu.asu;
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.view.View;
import android.app.ListActivity;
import static android.provider.BaseColumns._ID;
public class DbBookConsole extends ListActivity {
private static String[] FROM = { _ID, DbConstants.TITLE, DbConstants.PRICE,
DbConstants.ISBN, DbConstants.CHECKIN };
private DbCreate books;
private static SQLiteDatabase db;
private static int[] TO = { R.id.rowid, R.id.name, R.id.price, R.id.isbn, R.id.checkin};
private ListView lv1;
private static String itemId;
private static String titles;
private static String test;
private Cursor cursor;
static final int BOOK_CANCELED = 0;
static final int BOOK_ADDED = 1;
static final int BOOK_MODIFIED = 2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showDatabaseContent();
lv1 = getListView();
lv1.setTextFilterEnabled(true);
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
cursor = (Cursor) a.getItemAtPosition(position);
itemId = cursor.getString(0);
openOptionsMenu();
}
});
lv1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
// selected item index from ListView
public void showDialogItemId(long itemId) {
Toast.makeText(this,
"Menu item selected index is" + Long.toString(itemId),
Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.modifyitem:
if (null != itemId) {
Bundle bookToModify = new Bundle();
bookToModify.putString("cTitle", cursor.getString(1));
bookToModify.putString("cPrice", cursor.getString(2));
bookToModify.putString("cISBN", cursor.getString(3));
bookToModify.putString("cCheckin", cursor.getString(4));
bookToModify.putString("mod_type", "modifyBook");
Intent intent = new Intent(this, BookDetails.class);
intent.setClass(this, BookDetails.class);
intent.putExtras(bookToModify);
startActivityForResult(intent, BOOK_MODIFIED);
} else {
Toast
.makeText(this, "Select Book to modify",
Toast.LENGTH_LONG).show();
}
break;
case R.id.additem:
Intent i = new Intent(this, BookDetails.class);
Bundle bun = new Bundle();
bun.putString("mod_type", "addBook");
i.setClass(this, BookDetails.class);
i.putExtras(bun);
startActivityForResult(i, BOOK_ADDED);
break;
case R.id.removeitem:
if (null != itemId) {
removeBook(itemId);
showDatabaseContent();
} else {
Toast
.makeText(this, "Select Book to delete",
Toast.LENGTH_LONG).show();
}
break;
}
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
// See which child activity is calling us back.
switch (resultCode) {
case BOOK_ADDED:
// This is the standard resultCode that is sent back if the
// activity crashed or didn't doesn't supply an explicit result.
if (resultCode == RESULT_FIRST_USER) {
Bundle bundle = new Bundle();
bundle = intent.getBundleExtra("bookData");
addBook(bundle);
showDatabaseContent();
} else {
Toast.makeText(this, "CANCEL BOOK BUTTON PRESSED",
Toast.LENGTH_LONG).show();
}
break;
case BOOK_MODIFIED:
if (resultCode == 2) {
Bundle bundle = new Bundle();
bundle = intent.getBundleExtra("bookData");
modifyBook(bundle);
showDatabaseContent();
} else {
Toast.makeText(this, "MODIFY BOOK FAILED", Toast.LENGTH_LONG)
.show();
}
break;
default:
break;
}
}
// method removes item from database
private void removeBook(String itemId) {
db = books.getWritableDatabase();
db.delete(DbConstants.TABLE_NAME, "_ID=" + itemId, null);
}
private void addBook(Bundle bundle) {
// Insert a new record into the Events data source.
// You would do something similar for delete and update.
db = books.getWritableDatabase();
ContentValues vals = new ContentValues();
vals.put(DbConstants.TITLE, bundle.getString("bookTitle"));
vals.put(DbConstants.PRICE, bundle.getString("bookPrice"));
vals.put(DbConstants.ISBN, bundle.getString("bookISBN"));
vals.put(DbConstants.CHECKIN, bundle.getString("bookCheck"));
db.insertOrThrow(DbConstants.TABLE_NAME, null, vals);
}
// method should modify existing Contact
private void modifyBook(Bundle bundle) {
db = books.getWritableDatabase();
ContentValues vals = new ContentValues();
vals.put(DbConstants.TITLE, bundle.getString("bookTitle"));
vals.put(DbConstants.PRICE, bundle.getString("bookPrice"));
vals.put(DbConstants.ISBN, bundle.getString("bookISBN"));
vals.put(DbConstants.CHECKIN, bundle.getString("bookCheck"));
db.update(DbConstants.TABLE_NAME, vals, _ID + "=" + itemId, null);
}
private Cursor getBooks() {
db = books.getReadableDatabase();
cursor = db.query(DbConstants.TABLE_NAME, FROM, null, null, null, null, null,
null);
startManagingCursor(cursor);
return cursor;
}
public void showDatabaseContent() {
books = new DbCreate(this);
try {
cursor = getBooks();
showBooks(cursor);
} finally {
books.close();
db.close();
}
}
private void showBooks(Cursor cursor) {
// set up data binding
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.item, cursor, FROM, TO);
setListAdapter(adapter);
}
public void getTitleList(){
cursor = db.rawQuery("SELECT TITLE FROM table", null);
cursor.close();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非我误解了从游标中获取查询中的所有项目的问题,循环遍历所有项目并连接成一个字符串,否则您会执行如下操作:
...
希望有帮助
unless i missunderstood the Question to get all items from your query in a cursor, loop through all items and concatenate into a string you would do something like this:
...
Hope that helps