简体   繁体   中英

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.

Please please help to cottect it

Thank you

this my 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

<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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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