问题
I have been working on a project, in which i have a main activity in which i have listview and one search bar(edit text bar) and the onitemclicklistener of my listview passes the row position to a activity having webview and the webview opens html pages to their respective positions. i also have my listview items stored in arrays.xml
my problem starts here- my Listview Works file and opens respective activity on row click (when i dont use search bar and just scroll and click on row.)
but when i use the searchbar to search for an item (html page) for instance let it be "c" and i click on it , the listview DoesNot open the correct html page!. hope this is clear. i have around thousands of html files so stroring all the positions in the java file is not possible because of limited heap size. i found one solution here After filter a listview,how can I obtain the position of the first listview? but i have no idea how do i implement this in my code.
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);
}
});
}
}
and here is my webview activity
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);
}
}
}
回答1:
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;
}
}
}
});
}
}
See : http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/
回答2:
As you said you have thousands of items so checking for a match is not a good option. What you can do is , you can create a custom list adapter and set a tag for each item. And in onclick on listview you can get that tag and use it.
来源:https://stackoverflow.com/questions/20487851/searchbar-changing-positions-due-to-filtering