简体   繁体   English

从SQlite中提取Lat Long并在webview上显示

[英]Extract Lat Long from SQlite and display on webview

At present i want to display the all lat long values on webview by accessing java method from javascript but it is not showing the lat long values. 目前我想通过从javascript访问java方法在webview上显示所有lat long值,但它没有显示lat long值。

Please please help to cottect it 请帮忙来帮助它

Thank you 谢谢

this my WebviewActivity.java 这是我的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();
}
}

my index.html which access the android activity method to display the data 我的index.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>

Finally i got the answer 最后我得到了答案

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 file is 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM