Using edittext to search listview - android

I have this Listview:
#Override
public void onComplete(List<Profile> friends) {
runOnUiThread(new Runnable() {
#Override
public void run() {
mSpinner.setVisibility(View.GONE);
mSpinner.clearAnimation();
}
});
// populate list
List<String> values = new ArrayList<String>();
for (Profile profile : friends) {
//profile.getInstalled();
values.add(profile.getName());
}
listView = (ListView) findViewById(R.id.list);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> av, final View view, final int i, long i2) {
Animation pushLeftIn = AnimationUtils.loadAnimation(CallActivity.this, R.anim.jump_no_fade);
view.startAnimation(pushLeftIn);
}
});
ArrayAdapter<String> friendsListAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.list_items2, values);
friendsListAdapter.sort(new Comparator<String>() {
#Override
public int compare(String lhs, String rhs) {
return lhs.compareTo(rhs);
}
});
mFriendsList.setAdapter(friendsListAdapter);
}
};
I am trying to make the edittext filter the listview, this is what i have tried:
mySearchView.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
CallActivity.this.friendsListAdapter.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
}
});
I get no errors in the code but when i try to search in the edittext it doesn't work at all, what is the problem or how should i do instead?

Try do it in afterTextChanged(Editable arg0).
And did you implement your getFilter().filter(cs). Can you show implementation?

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

To Do List Android app crashes after search

The problem that I am having is that once I perform a search on my ListView, I am unable to delete or add anything else to my List. Additionally, I am getting an IndexOutofBounds error. My code is as follows:
public class MainActivity extends AppCompatActivity {
public ArrayList<String> array;
public ArrayAdapter<String> adapt;
public ListView list;
public EditText search;
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.list_item);
array = new ArrayList<>();
adapt = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, array);
list.setAdapter(adapt);
setupListViewListener();
search();
}
public void onAddItem(View v) {
EditText edit = (EditText) findViewById(R.id.edit);
String item = edit.getText().toString();
adapt.add(item);
edit.setText("");
}
public void setupListViewListener() {
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View v, int i, long l) {
array.remove(i);
adapt.notifyDataSetChanged();
return true;
}
});
}
public void search()
{
search = (EditText) findViewById(R.id.inputSearch);
search.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
adapt.getFilter().filter(cs);
adapt.notifyDataSetChanged();
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
#Override
public void afterTextChanged(Editable arg0) {
}
});
}
public void deleteAll(View view) {
adapt.clear();
adapt.notifyDataSetChanged();
}
}

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

simple adapter issue for multiple words String in android

inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
ParseXMLDemo.this.adapter1.getFilter().filter(cs.toString().trim());
if(list2.getVisibility() == View.INVISIBLE)
list2.setVisibility(View.VISIBLE);
list2 = (ListView) findViewById(R.id.list2);
if(inputSearch.getText().toString().equals(""))
{
list2.setVisibility(View.INVISIBLE);
text.setVisibility(View.VISIBLE);
}
else
{
list2.setVisibility(View.VISIBLE);
text.setVisibility(View.INVISIBLE);
}
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
if inputSearch = "India Gate"
after India the later word after space shows but if i type G after space the reult hides automatically.

List with search and mark

I am trying to implement a contact list with a search and marking feature; everything except search is working properly. The code I am using is given below. Could someone please help me to sort this out?
public class MainActivity extends ListActivity implements Runnable {
Cursor phoneCursor;
public static Button done;
ListView lv;
/* OpenHelper db_obj;*/
final int PROGRESS_DIALOG = 0;
ProgressDialog progressDialog;
ArrayAdapter<ContactModel> adapter;
ArrayList<HashMap<String, String>> CONTACT_DATA = new ArrayList<HashMap<String, String>>();
#Override
public void onCreate(Bundle saveinst) {
super.onCreate(saveinst);
progressDialog=ProgressDialog.show(MainActivity.this, "LoadContacts", "Please wait...", true, false);
Thread loadContact = new Thread(this);
loadContact.start();
}
public void run() {
List<ContactModel>contactList = new ArrayList<ContactModel>();
try {
Looper.prepare();
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, Phone.DISPLAY_NAME + " ASC");
while (phones.moveToNext()) {
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber11 = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactList.add(get(name,phoneNumber11));
}
adapter = new MyContactListAdapter(MainActivity.this, contactList);
handler.sendEmptyMessage(0);
} catch(Exception exce) {
exce.printStackTrace();
}
private final Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
progressDialog.dismiss();
setListAdapter(adapter);
setContentView(R.layout.activity_main);
EditText editText;
/* db_obj = new OpenHelper(getApplicationContext());*/
lv = getListView();
editText = (EditText)findViewById(R.id.editText1);
editText.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);
setListAdapter(adapter);
}
#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
}
});
done = (Button)findViewById(R.id.button1done);
done.setVisibility(View.INVISIBLE);
done.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
MainActivity.this.finish();
Intent in = new Intent();
in.setClass(getApplicationContext(), MainActivity.class);
startActivity(in);
}
});
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
System.out.println("Entered List Activity");
}
});
}
};
public ContactModel get(String name,String number)
{
return new ContactModel(name,number);
}
Instead of an EditText try an AutoCompleteTextView. It works similar to what you have here except the searching is handled by Android without have to use filters in the onTextChanged.
myACTV.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
myACTV.showDropDown();
myACTV.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
//Do something here
}
});
return false;
}
});
For a better Idea of how this works check out http://developer.android.com/reference/android/widget/AutoCompleteTextView.html

Categories

Resources