1
我有一個數據庫,允許用戶查詢數據庫中的書籍。如何將查詢結果保存到單獨的表中?
我在嘗試將查詢結果保存爲「書籤」。用戶輸入一個查詢,然後可以點擊一個結果來查看該特定書籍的詳細信息。
然後有一個菜單選項允許用戶將查詢保存到他們的書籤。保存的查詢被插入到數據庫表中。書籤是一種將保存的數據加載爲列表視圖的活動,允許用戶管理其書籤。
除保存外,所有設置和工作都很精彩。我不知道如何,或者當選擇保存的菜單選項時,將查詢結果導入數據庫的最佳方式是什麼。
一個標準的sqlite插入工作,但沒有具體的查詢結果數據。
某些代碼:
package com.example.newdatabase;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
public class DetailView extends Activity
{
private DBAdapter dbHelper;
private SimpleCursorAdapter dataAdapter;
private long rowid;//row for book from position in database
public static final String BOOKM_ID = "rowid";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.detailviewlist);
rowid=getIntent().getExtras().getLong(TitleSearch.ROW_ID);//passed from TitleSearch pass the row id
dbHelper = new DBAdapter(this);
dbHelper.open();
//loads data into list
displayListView();
}
private void displayListView()
{
Cursor cursor=dbHelper.getABOOK(rowid);//cursor and query method only shows one book
//defines columns to bind
String [] columns =new String[]
{
DBAdapter.KEY_TITLE,
DBAdapter.KEY_AUTHOR,
DBAdapter.KEY_EDITION,
DBAdapter.KEY_PUBLISHER,
DBAdapter.KEY_DESCRIPTION,
DBAdapter.KEY_OPAC,
DBAdapter.KEY_LOCATION,
DBAdapter.KEY_CALL,
DBAdapter.KEY_COPIES,
DBAdapter.KEY_STATUS,
};
// the XML defined views which the data will be bound to
int[] to = new int[]
{
R.id.title,
R.id.author,
R.id.edition,
R.id.publisher,
R.id.description,
R.id.subject,
R.id.location,
R.id.callnum,
R.id.copies,
R.id.status,
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
this, R.layout.detailbook,
cursor,
columns,
to,
0);
ListView listView = (ListView) findViewById(R.id.listView2);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) //save menu
{
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.savemenu, menu);
return true;
}
/* @Override
public boolean onOptionsItemSelected(MenuItem item) //lauches the bookmark when saved
{
Intent addContact = new Intent(DetailView.this, Bookmark.class);
startActivity(addContact);
return super.onOptionsItemSelected(item);
}*/
public boolean onOptionsItemSelected(MenuItem item)//menu action
{
switch (item.getItemId())
{
case R.id.addsave:
// Single menu item is selected to save book info
//adds info into bookmark on save click
//this is where i am having an issue
Toast.makeText(DetailView.this, "The book has been saved!", Toast.LENGTH_SHORT).show();//feedback for user
return true;
default:
return super.onOptionsItemSelected(item);
}
} //end of options
}//end of detail view
我試圖從該getABOOK(ROWID)查詢的結果保存成書籤使用的數據庫表。
除非有更好的方法來保存結果。
感謝您的迴應和建議。我會試試看。 – sherwood
再次感謝您的建議。它工作得很好,並防止頭痛:) – sherwood
很高興,它的工作! – PravinCG