1
html 페이지에서 표 행을 클릭하면 웹보기에서 전화를 다시 받으려고합니다. 나의 활동은 다음과 같습니다 :웹보기 addJavascriptInterface 문제
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);
}
}
내 자바 스크립트 클래스는 다음과 같습니다 :
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;
}
}
통화가 다시 잘 작동합니다. 하지만 직원 목록에 콜백 메서드에서 액세스하려고하면 목록이 항상 null이지만 내 활동 클래스에서 목록 값을 설정하고 있습니다. 나는 무엇인가 놓치고 있니? 누군가가 친절하게 나를 도와 줄 수 있었습니까?
감사합니다. 나는 정말로 그것을 알아 차리지 못해서 절름발이였다. :-) –