从SQlite中提取Lat-Long并在网络视图中显示

Extract Lat Long from SQlite and display on webview

本文关键字:网络 视图 显示 Lat-Long SQlite 提取      更新时间:2023-12-29

目前我想通过从javascript访问java方法在webview上显示所有的lat-long值,但它没有显示lat-long的值。

请帮忙检测

谢谢

这是我的WebviewActivity.java

static final String TAG = "JavascriptDataDemo";
//double[] data = new double[] {42.6, 24, 17, 15.4};
DataHelper myDB=new DataHelper(this);
/** This passes our data out to the JS */
@JavascriptInterface
public String getData() {
    myDB.insert(16.5048, 80.6338);
    myDB.insert(16.5024,80.6432);
    myDB.insert(16.512,80.6216);
    myDB.insert(16.5124,80.6219);
    Cursor cursor = myDB.fetchAllCountries();
    double[] array = new double[cursor.getCount()];
    double[] array1=new double[cursor.getCount()];
    int i = 0;
    if (cursor.moveToFirst()) {
        do {
            double data = cursor.getDouble(cursor.getColumnIndex("lat"));
            double data1=cursor.getDouble(cursor.getColumnIndex("longt"));
            array[i] = data;
            array1[i]=data1;
            i++;
        } while (cursor.moveToNext());
    }
    Log.d(TAG, "getData() called");
    return a1dToJson(array,array1).toString();
}
/** Allow the JavaScript to pass some data in to us.
@JavascriptInterface
public void setData(String newData) throws JSONException {
    Log.d(TAG, "MainActivity.setData()");
    JSONArray streamer = new JSONArray(newData);
    data = new double[streamer.length()];
    for (int i = 0; i < streamer.length(); i++) {
        Double n = streamer.getDouble(i);
        data[i] = n;
    }
}*/
private Activity activity;
public Context getActivity() {
    return activity;
}
public void setActivity(Activity app) {
    this.activity = app;
}
@JavascriptInterface
public void finish() {
    Log.d(TAG, "ArrayApplication.finish()");
    activity.finish();
}
/** Sorry for not using the standard org.json.JSONArray but even in Android 4.2 it lacks
 * the JSONArray(Object[]) constructor, making it too painful to use.
 */
private String a1dToJson(double[] data,double[] data1) {
    StringBuffer sb = new StringBuffer();
    sb.append("[");
    for (int i = 0,j=0; (i < data.length) && (j<data1.length); i++,j++) {
        double d = data[i];
        double s=data1[j];
        if (i > 0 && j>0) {
            sb.append("[");
            sb.append(d);
            sb.append(",");
            sb.append(s);
            sb.append("]");
        }
    }
    sb.append("]");
    return sb.toString();
}
}

myindex.html访问android活动方法显示数据

<html>
<head>
<script> 
    var showData = function() {
        var data = android.getData();
        data = JSON.parse(data);
        window.alert("Hello! Data are: " + data + "; first = " + data[0]);
    }
 </script>
 </head>
 <body>

 <input type="button" value="Display data" onclick="showData()">
 <input type="button" value="Update data" onclick="setData();">
 <br/>
 <input type="button" value="Done" onclick="android.finish();">
 </body>
 </html>

最后我得到了答案

DataHelper myDB=new DataHelper(this);

//double[] data = new double[] {42.6, 24, 17, 15.4};
/** This passes our data out to the JS */
@JavascriptInterface
public String getData() {
    myDB.insert(16.5048, 80.6338);
    myDB.insert(16.5024,80.6432);
    myDB.insert(16.512,80.6216);
    myDB.insert(16.5124,80.6219);
    Cursor cursor=myDB.fetchAllCountries();
    double[] arr=new double[cursor.getCount()];
    int i=0;
    if(cursor.moveToFirst())
    {
        do {
            double data=cursor.getDouble(cursor.getColumnIndex("lat"));
           arr[i]=data;
            i++;
        }while (cursor.moveToNext());
    }


    Log.d(TAG, "getData() called");
    return a1dToJson(arr).toString();
}

@JavascriptInterface
public String getLong() {
    Cursor cursor1=myDB.fetchAllCountries();
    double[] arr1=new double[cursor1.getCount()];
    int i=0;
    if(cursor1.moveToFirst())
    {
        do {
            double data1=cursor1.getDouble(cursor1.getColumnIndex("longt"));
            arr1[i]=data1;
            i++;
        }while (cursor1.moveToNext());
    }


    Log.d(TAG, "getData() called");
    return a1dToJson(arr1).toString();
}

private Activity activity;
public Context getActivity() {
    return activity;
}
public void setActivity(Activity app) {
    this.activity = app;
}
@JavascriptInterface
public void finish() {
    Log.d(TAG, "ArrayApplication.finish()");
    activity.finish();
}
/** Sorry for not using the standard org.json.JSONArray but even in Android 4.2 it lacks
 * the JSONArray(Object[]) constructor, making it too painful to use.
 */
private String a1dToJson(double[] data) {
    StringBuffer sb = new StringBuffer();
    sb.append("[");
    for (int i = 0; i < data.length; i++) {
        double d = data[i];
        if (i > 0)
            sb.append(",");
        sb.append(d);
    }
    sb.append("]");
    return sb.toString();
}

javascript文件是

<html>
<head>
<script>
    var showData = function() {
        var data = android.getData();
        data=JSON.parse(data);
         var data1 = android.getLong();
        data1=JSON.parse(data1);
        window.alert("Hello! Data are: " + data );
        window.alert("Hello! Data are: " + data1 );
    }
 </script>
   </head>
 <body>
    <input type="button" value="Display data" onclick="showData()">

    <input type="button" value="Done" onclick="android.finish();">
     </body>
      </html>