我正在尝试从listview项目上的assets文件夹加载html文件。 问题是,点击它会显示干净的页面,检查html文件。 这是一段代码。
public static ListView listview;
public static ArrayList<String> interviews;
public static ArrayAdapter<String> adapter;
public WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kochergin);
listview = (ListView) findViewById(R.id.mainListView);
myWebView = (WebView)findViewById(R.id.webview);
// myWebView = new WebView(this);
listview.setBackgroundColor(Color.BLACK);
listview.setCacheColorHint(Color.BLACK);
String[] interviewlist = new String[] {"Interview1", "Interview2","Interview3","Interview4","Interview5","Interview6"};
ArrayList<String> interviews = new ArrayList<String>();
interviews.addAll( Arrays.asList(interviewlist) );
ArrayAdapter <String> adapter = new ArrayAdapter<String>(this, R.layout.simplerow, R.id.rowTextView, interviews);
listview.setAdapter(adapter);
Collections.sort(interviews);
listview.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
setContentView(R.layout.webview);
// myWebView.setVisibility(View.VISIBLE);
// myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("file:///android_asset/2.html");
}
});
我做错了什么?
答案 0 :(得分:4)
您正在加载myWebView
的{{1}}网址,而R.layout.activity_kochergin
中的网址是R.layout.webview
中您未设置为onItemClick
中的活动内容视图。
请从setContentView(R.layout.webview);
移除onItemClick
,然后就可以了。
<强>更新强>
将setOnItemClickListener
更改为: -
listview.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
setContentView(R.layout.webview);
myWebView = (WebView)findViewById(R.id.webview);
myWebView.loadUrl("file:///android_asset/2.html");
}
});