Gaurav Gupta
Gaurav Gupta

Reputation: 1600

Retrieve data from sqlite database and display in assest folder's HTML page in android

I want to make a dynamic webpage that can display my sqlite database's element in tabular format. How can I create a dynamic webpage in android? I make a static HTML page and put in assest folder. It works but now I want a dynamic webpage. Please help:

package com.Htmlview;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class Htmlview extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        WebView webview = new WebView(this);
        setContentView(webview);
        try {
            InputStream fin = getAssets().open("index3.html");
                byte[] buffer = new byte[fin.available()];
                fin.read(buffer);
                fin.close();
                webview.loadData(new String(buffer), "text/html", "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
} 

This page works ... Help to make it dynamic through code.

gaurav gupta

Upvotes: 0

Views: 1866

Answers (2)

P. Taylor Goetz
P. Taylor Goetz

Reputation: 1141

Use java.text.MessageFormat:

  1. Put "{0}" markers in your html file.
  2. Read your html file into a string
  3. Create an arguments array from the database record
  4. Call MessageFormat.format(htmlString, dbArgs)
  5. Load the resulting string into the webview.

Upvotes: 2

Vladimir Ivanov
Vladimir Ivanov

Reputation: 43108

You can't modify the asset folder in runtime as it compiled at build time. So, you have 2 variants:

  1. You content from database as it is. Just read the bytes and pass them to the webview without storing to anywhere.

  2. Store the content to the file in internal memory and do like you did it with assets. But there is no sence as you already have your data in db.

Upvotes: 0

Related Questions