Here is my code which stores and extracts lat long values from database and displaying in html file. What wrong with this code Can anyone help me to findout Thankyou
MainACtivity.java
DataHelper mydb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mydb.insertProvince(16.5024, 80.6432);
mydb.insertProvince(16.5048, 80.6338);
mydb.insertProvince(16.512, 80.6216);
mydb.insertProvince(16.5124, 80.6219);
//ArrayList<Double> listpro=mydb.getAllProvinces();
WebView webView=(WebView)findViewById(R.id.webView);
final MyJavaScriptInterface myJavaScriptInterface
= new MyJavaScriptInterface(this);
webView.addJavascriptInterface(myJavaScriptInterface, "AndroidFunction");
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/www/index.html");
}
public class MyJavaScriptInterface {
Context mContext;
MyJavaScriptInterface(Context c) {
mContext = c;
}
public void showData(){
ArrayList<Double> list=mydb.getAllProvinces();
for(int i=0;i<list.size();i++)
{
double data=list.get(i);
System.out.println(data + " ");
}
}
}
DataHelper.java
public class DataHelper extends SQLiteOpenHelper {
public static final String DB_NAME="mydb.db";
public static final String Table_Pro="latlong";
public static final String Create_Pro="Create table if not exists "+Table_Pro+"(id integer primary key autoincrement,latitude not null unique,longitude not null unique)";
public static final String Delete_Pro="Drop table if exists "+Table_Pro;
public DataHelper(Context context) {
super(context, DB_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(Create_Pro);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(Delete_Pro);
onCreate(db);
}
public void insertProvince(double latitude,double longitude)
{
SQLiteDatabase db=this.getWritableDatabase();
db.beginTransaction();
ContentValues values;
try
{
values=new ContentValues();
values.put("latitude",latitude);
values.put("longitude",longitude);
db.insert(Table_Pro, null, values);
db.setTransactionSuccessful();
}catch (Exception e)
{
e.printStackTrace();
}
finally {
db.endTransaction();
db.close();
}
}
public ArrayList<Double> getAllProvinces()
{
ArrayList<Double> list=new ArrayList<Double>();
SQLiteDatabase db=this.getReadableDatabase();
db.beginTransaction();
try
{
String SelectQuery="select *from "+Table_Pro;
Cursor cursor=db.rawQuery(SelectQuery,null);
if(cursor.getCount()>0)
{
while (cursor.moveToNext())
{
double latitude=cursor.getDouble(cursor.getColumnIndex("latitude"));
list.add(latitude);
}
}
db.setTransactionSuccessful();
}catch (Exception e)
{
e.printStackTrace();
}
finally {
db.endTransaction();
db.close();
}
return list;
}
}
Here index.html
<html>
<body>
<p id="mytext">Hello!</p>
<input type="button" value="Say hello"
onClick="showAndroidData()" />
<script language="javascript">
function showAndroidData() {
AndroidFunction.showData();
}
function callFromActivity(msg){
document.getElementById("mytext").innerHTML = msg;
}
</script>
</body>
Aucun commentaire:
Enregistrer un commentaire