我有一个 sqlite 数据库,其中有我想在 webview 中加载的内容,, 因为我想从数据库中选择并在 WebView 中显示,有什么办法吗?
public class TataworatYawmeeh extends Activity {
WebView webView;
String javascrips;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tataworat);
webView = (WebView) findViewById(R.id.tatworatWebView);
webView.setBackgroundColor(0x00000000);
try {
DataBaseHelper myDbHelper = new DataBaseHelper(this);
myDbHelper = new DataBaseHelper(this);
try {
myDbHelper.createDataBase();
String selectQuery = "SELECT * FROM 'nnn'";
SQLiteDatabase db = myDbHelper.getReadableDatabase();
db.execSQL(selectQuery);
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
} catch (SQLException sqle) {
throw sqle;
}
}
}
}
最佳答案
您必须使用 Java 类作为接口(interface):
DataLoader dl = new DataLoader();
// For passing the data
webView.addJavascriptInterface(dl, "accessor");
webView.loadUrl("file:///android_asset/yourpage.html");
在您的 Activity 中,您可以使用 get 方法声明匿名类 DataLoader,您可以在其中检索数据并使用 JSON 返回数据,如下所示:
class ChartDataLoader {
public String getData() {
String selectQuery = "SELECT * FROM 'nnn'";
SQLiteDatabase db = myDbHelper.getReadableDatabase();
result = db.execSQL(selectQuery);
Gson gson = new Gson();
data = gson.toJson(result, resultype.class);
return data;
}
}
最后,您可以使用 javascript 在您的 html 中使用此数据:
<script id="source" language="javascript" type="text/javascript">
var obj = window.accessor;
var data = JSON.parse(obj.getBalances());
/* Loop */
</script>
关于Android:加载sqlite数据库内容到webview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13717851/