我真的被卡住了。我熟悉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;
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扩展了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();
} }
答案 0 :(得分:0)
除非我错过了解问题以从游标中获取查询中的所有项目,循环遍历所有项目并连接成字符串,否则您将执行以下操作:
// constants of column indexes
int INDEX_ROWID = 0;
int INDEX_TITLE = 1;
...
db = new MyDBAdapter(this);
db.open();
Cursor c = db.getTitleList();
startManagingCursor(c);
String allTitles;
// loop through cursor
while(c.moveToNext()) {
allTitles += c.getString(INDEX_TITLE);
}
希望有所帮助