1

I am trying to get a call back from my Web View when a table row is clicked in the html page. My activity is as below:

 public class LocalizationTestActivity extends Activity {



/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LinearLayout linearLayout = new LinearLayout(this);
    linearLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
    WebView webView = new WebView(this);
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT));
    JavaScriptInterface javaScriptInterface=new JavaScriptInterface(this);
    webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
    Vector<Employee> test=new Vector<Employee>();
    for (int i=1;i<20;i++){
        Employee employee=new Employee();
        employee.setId(i);
        employee.setName("Norton - "+i);
        employee.setAddress("Indiranagar - " +i);
        test.add(employee);
    }
    javaScriptInterface.setDashboardList(test);

    webView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageFinished(WebView view, String url) {

            view.loadUrl("javascript:populateData()");
        }

    });

    webView.loadUrl("file:///android_asset/html/test2.html");
    linearLayout.addView(webView);
    setContentView(linearLayout);
}
}

My Java script class is as below:

public class JavaScriptInterface {
private Context mContext;
private Vector employeeList;

    /** Instantiate the interface and set the context */
    JavaScriptInterface(Context c) {
        mContext = c;
    }

    /** Show a toast from the web page */
    public void callBack(String toast) {

        int index=Integer.parseInt(toast);
        Intent intent=new Intent(mContext, SampleActivity.class);

        if(employeeList!=null && index<employeeList.size()){
            Employee employee=(Employee) employeeList.elementAt(index);
            intent.putExtra("value", "1");
            intent.putExtra("name", employee.getName());
            intent.putExtra("address", employee.getAddress());
        }
        mContext.startActivity(intent);
    }


    public void setEmployeeList(Vector employeeList) {
        this.employeeList= employeeList;
    }
    }

The call back works fine. But when i try accesing the employeeList from callBack method the list is always null though i am setting the list value from my activity class. Am i missing on something? Could someone kindly help me with this.

1 Answer 1

2

Change

webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");

to

webView.addJavascriptInterface(javaScriptInterface, "Android");
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I was really lame to not notice that. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.