Add search to ListView using SimpleAdapter in Android - android

I have a ListView from json file that is in my assets folder. The listView works perfectly, but I want to add Search to it. I want to add editText before the listView, and when the user types some text, the list will be filtered.
I'm using SimpleAdapter for the listView.
I've tried some tutorials but they don't work, or when I type in the editText textBox the items of the list are duplicated. Sorry for my bad English.
this is the code:
ListAdapter adapter = new SimpleAdapter(this, herbList,
R.layout.caevi_list_item, new String[] { HERB_ID, HERB_NAME, HERB_DISCRIPTION},
new int[] { R.id.idbilka, R.id.name, R.id.discription});
setListAdapter(adapter);
inputSearch = (EditText) findViewById(R.id.inputSearch);
inputSearch.setSingleLine();
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
((Filterable) Caevi.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
}
});

Related

listView filter search

I was looking to filter a listView by an editText.
My ListView is populate by an ArrayString, and when I filter it everything goes right. But I have a problem to recognize the element clicked. When I filter the listView and I click on the item filtered I want to have the number of the row in the original arrayString in order to make an Intent. Whit my code when I click the item in the filtered arrayString the row is 0 and it's not correct. If I search "Anche Se Pesa" I want to return the row 2 like the position in the original ArrayString.
<string-array name="titoli">
<item>An Val Dondona</item>
<item>Anche Mio Padre</item>
<item>Anche Se Pesa</item>
<item>Andremo In Francia</item>
<item>Aprite Le Porte</item>
<item>Arda Berghem</item>
<item>Ascoltate Amici Cari</item>
<item>Ave, O Vergine</item>
<item>Aver na Figlia Sola Soletta</item>
<item>Ancora sti quatro</item>
<item>Andouma Prou</item>
</string-array>
Here Is the code
public class activity_titoli extends AppCompatActivity {
Button btt_backHome;
ListView lV_titoli;
EditText eT_search;
private ArrayAdapter<CharSequence> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_titoli);
btt_backHome = (Button) findViewById(R.id.btt_backHome);
lV_titoli = (ListView) findViewById(R.id.lV_titoli);
eT_search = (EditText) findViewById(R.id.eT_search);
adapter = ArrayAdapter.createFromResource(this, R.array.titoli, android.R.layout.simple_list_item_1);
lV_titoli.setAdapter(adapter);
final Intent to_Home = new Intent (this , Activity_Main.class);
eT_search.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
(activity_titoli.this).adapter.getFilter().filter(arg0);
}
});
btt_backHome.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(to_Home);
}
});
lV_titoli.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.e("Element: " , Integer.toString(i));
}
});
}
}
Try this code :
lV_titoli.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Resources res = getResources();
List<CharSequence> list = Arrays.asList(res.getStringArray(R.array.titoli));
Log.e("Element: " , Integer.toString(list.indexOf(adapter.filgetItem(i))));
}
});
Hope this helps

ListView filtering not working

I have created a list view in android and add edit text above the list and when the user enter text the list will be filtered according to user input but notworking :
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
MainActivity.this.myadpt.getFilter().filter(arg0);
}
});
try this one:
adapter = new ListViewAdapter(this, arraylist);
list.setAdapter(adapter);
editsearch = (EditText) findViewById(R.id.search);
editsearch.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable arg0) {
String text = editsearch.getText().toString().toLowerCase(Locale.getDefault());
adapter.filter(text);
}
});
see this one: List View Filter Android

Android Listview with Search filter

I am trying to make a listView search for Android.I am using SimpleCursorAdapter on onTextChanged
i have found a error adapter cannot be resolved or is not a field.how can resolve this error .I am using SimpleCursorAdapter .I am not able to use adapter.getFilter().filter(cs);
ListView lv;
Cursor cursur1;
EditText search ;
int textlength=0;
ArrayAdapter<String> adapter1;
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
search = (EditText) findViewById(R.id.inputSearch);
final String [] from={ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone._ID};
int [] m={android.R.id.text1,android.R.id.text2};
lv=getListView();
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
cursur1=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
startManagingCursor(cursur1);
#SuppressWarnings("deprecation")
SimpleCursorAdapter adapter=new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursur1, from,m);
setListAdapter(adapter);
search.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
MainActivity.this.adapter.getFilters().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
}
});
}
#Override
public long getSelectedItemId() {
// TODO Auto-generated method stub
return super.getSelectedItemId();
}
#Override
public int getSelectedItemPosition() {
// TODO Auto-generated method stub
return super.getSelectedItemPosition();
}
}
Try this.. inside add Text change listener
YourAdapterOrWhateverAdapter adapter = (YourAdapterOrWhateverAdapter) listView.getAdapter();
adapter.getFilter().filter(s);
adapter.notifyDataSetChanged()
you can use AutoCompleteTextView, e.g -
private static final String[] DATE_RANGE = new String[] { "1 Day",
"2 Days", "3 Days", "6 Months", "1 Year", "Blank" };
AutoCompleteTextView dateRangeTV = (AutoCompleteTextView)findViewById(R.id.tv_date_range);
dateRangeTV.setThreshold(0);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.default_date_range_list, DATE_RANGE);
dateRangeTV.setAdapter(adapter);
In Touch event, you could see the dropdownlist :
dateRangeTV.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
dateRangeTV.showDropDown();
return false;
}
});
Type any text, It'll show your matching text from the list.
For more see the tutorial of the AutoCompleteTextView

how to take the listview item from addTextChangedListener and display it in the corresponding edittext box

//Listview adapter
adapter_q = new ArrayAdapter<String>(Authors.this, R.layout.list_item_author,R.id.product_name, Quotes);
lv.setAdapter(adapter_q);
//textViewq=(MultiAutoCompleteTextView)findViewById(R.layout.list_item_author);
System.out.println("before printing");
// filtering text
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
Authors.this.adapter_q.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
}
});
taking the selected item from the list and seeting it to the edittext box
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg01, View arg11, int arg21, long arg31) {
valc= arg01.getItemAtPosition(arg21).toString();
System.out.println(valc);
inputSearch.setText(valc);
}
});
This is my search edittext box,i am getting values in it and it is filtering correctly,
but my problem is that when i select a value from the list view, the listview still remains there,
how to dismiss it as i take value from that list view?
Assuming that Quotes is your arraylist of string that you passed into the adapter, you need to do the following:
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg01, View arg11, int arg21, long arg31) {
System.out.println(Quotes.get(arg21));
inputSearch.setText(Quotes.get(arg21));
}
});
Because the data you want to display is stored in the array list instead.

Android Listview

now i am making a program in android using edittext and listview.
I want to search the listview item using edittext above.
After populate data to listview, when user type text in edittext, the listview will
scroll to the position start with that text.
Example: i have item:
apple,
application,
book,
boy,
car,
cat,
cash.....
when i type b in edittext then listview will scroll to book.
I want to use the listview.setSelection(position), but i don't know how can i get the position from my edittext search.
I hope everybody can help me. Thanks in advances !
you can make implement like the below code:
YOUR_EDITTEXT.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
//LOGIC MAY DIFFER ACCORDING TO YOUR REQUIREMENT..
int POSITION = 0;
for(int i =0;i<list.size();i++) {
if(list.get(i).startsWith(s.toString()))
{
POSITION = i;
break;
}
}
listview.smoothScrollToPosition(POSITION);
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
});
Hope it will help you..!!!
try this example,
public class MainActivity extends Activity {
private ListView list1;
// private String array[] ;
EditText inputSearch;
ArrayAdapter<String> adapter;
String[] testArray = new String[] { "one", "two", "three", "etc" };
#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);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, testArray);
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) {
Log.i("check", "" + position);
}
});
}
}

Categories

Resources