我一直在做一個項目,其中我有一個主要活動,其中我有listview和一個搜索欄(編輯文本欄),並且我的listview的onitemclicklistener將行位置傳遞給具有webview和webview的活動將html頁面打開到它們各自的位置。我也有我的存儲列表視圖項arrays.xml搜索欄由於過濾而改變位置
我的問題開始這裏 - 我的列表視圖工作文件和行即可打開相應的活動(當我不使用搜索欄,只是滾動和點擊一行。)
但是當我使用搜索欄來搜索一個項目(html頁面)例如讓它成爲「c」,我點擊它,listview DoesNot打開正確的html頁面!希望這是明確的。我有大約數千個html文件,因此在java文件中記錄所有位置是不可能的,因爲堆大小有限。我在這裏找到了一個解決方案After filter a listview,how can I obtain the position of the first listview? 但我不知道如何在代碼中實現這一點。
here is my main activity
package com.Sample.htmlOpener;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView list1;
private String array[] ;
EditText inputSearch;
ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputSearch = (EditText) findViewById(R.id.inputSearch);
list1 = (ListView) findViewById(R.id.ListView01);
array = getResources().getStringArray(R.array.myArray);
adapter =new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array);
list1.setAdapter(adapter);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
list1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent myIntent = new Intent(MainActivity.this,WebViewActivity.class);
myIntent.putExtra("key",position);
startActivity(myIntent);
}
});
}
}
這裏是我的WebView活動
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewActivity extends Activity {
WebView web;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
web = (WebView) findViewById(R.id.webView1);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
int pos = getIntent().getIntExtra("key",0);
if(pos==0)
{
web.loadUrl("file:///android_asset/file.html");
}
else
{
web.loadUrl("file:///android_asset/file" + pos + ".html");
}
// similarly for 4 and 5 and so on.
}
public class myWebClient extends WebViewClient
{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
}
}
??????????????? – Anonymous
我告訴你,我有成千上萬的html文件,請仔細閱讀說明 – Anonymous
檢查我編輯的答案 –