我有一个数据库,允许用户在数据库中查询书籍。
我正在尝试将查询结果保存为“书签”。用户输入查询,然后能够单击结果以查看该特定书籍的详细信息。
然后有一个菜单选项允许用户将查询“保存”到他们的书签中。保存的查询被插入到数据库表中。Bookmarks 是一个将保存的数据加载为列表视图的活动,允许用户管理他们的书签。
除了保存之外,一切都已设置好并且运行良好。I do not know how, or what would be the best way to get the results from the query into the database when the saved menu option is picked.
标准的 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) 查询的结果保存到 Bookmarks 使用的数据库表中。
除非有更好的方法来保存结果。