Skip to content

Instantly share code, notes, and snippets.

@Singhak
Last active December 18, 2015 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Singhak/40d8fb5725c3bc3720f6 to your computer and use it in GitHub Desktop.
Save Singhak/40d8fb5725c3bc3720f6 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends ActionBarActivity implements TextWatcher{
EditText searchText;
CustomAdapter listAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// This is EditText
searchText = (EditText) findViewById(R.id.searchView);
// We are adding text change listener so that we can take care of each character which we type
searchText.addTextChangedListener(this);
// This is arraylist which we are initiallising with string and show in list
ArrayList<String> stringList = new ArrayList<String>();
for (int i = 0; i < Cheeses.sCheeseStrings.length; i++) {
stringList.add(Cheeses.sCheeseStrings[i]);
}
ListView listView = (ListView) findViewById(R.id.listadapter);
// Here we are creating object of our custum adapter class
listAdapter = new CustomAdapter(this, stringList);
//here we are setting adapter to list view
listView.setAdapter(listAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Here we tatking string from the edittextview passing it to filter which is define in custum adapter
listAdapter.getFilter().filter(searchText.getText().toString());
}
@Override
public void afterTextChanged(Editable s) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment