我一直致力于一个项目,其中我有一个主要活动,其中我有listview和一个搜索栏(编辑文本栏),我的listview的onitemclicklistener将行位置传递给具有webview和webview的活动将html页面打开到各自的位置。我也将listview项存储在arrays.xml中
我的问题从这里开始 - 我的Listview Works文件并在行单击时打开相应的活动(当我不使用搜索栏时只需滚动并单击行。)
但是当我使用搜索栏搜索项目(html页面)时,让它为“c”并点击它,列表视图不会打开正确的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);
}
});
}
}
这是我的网页浏览活动
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);
}
}
}
答案 0 :(得分:0)
public class MainActivity extends Activity {
// List view
private ListView lv;
// Listview Adapter
ArrayAdapter<String> adapter;
// Search EditText
EditText inputSearch;
String[] productsList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
productsList = getResources().getStringArray(R.array.list);
lv = (ListView) findViewById(R.id.list_view);
inputSearch = (EditText) findViewById(R.id.inputSearch);
// Adding items to listview
adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, getResources().getStringArray(R.array.list));
lv.setAdapter(adapter);
/**
* Enabling Search Filter
* */
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
}
});
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String productName = adapter.getItem(position);
for ( int i=0;i<productsList.length;i++){
String product = productsList[i];
if ( product.equals(productName)){
Toast.makeText(MainActivity.this, "Original Position of Item in listview "+i, Toast.LENGTH_SHORT).show();
break;
}
}
}
});
}
}
请参阅:http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/
答案 1 :(得分:0)
正如你所说,你有成千上万的物品所以检查比赛不是一个好选择。您可以做的是,您可以创建自定义列表适配器并为每个项目设置标记。在onview上查看列表视图,您可以获取该标记并使用它。