Skip to content

Instantly share code, notes, and snippets.

@Phonbopit
Created June 20, 2013 07:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Phonbopit/5820983 to your computer and use it in GitHub Desktop.
Save Phonbopit/5820983 to your computer and use it in GitHub Desktop.
public class CustomBaseAdapter extends BaseAdapter {
Activity activity;
String[] values;
public CustomBaseAdapter(Activity context, String[] nameList) {
this.activity = context;
this.values = nameList;
}
public int getCount() {
return values.length;
}
public Object getItem(int position) {
return values[position];
}
public long getItemId(int position) {
return values.length;
}
public View getView(final int position, View view, ViewGroup parent) {
LayoutInflater inflater = activity.getLayoutInflater();
View row = inflater.inflate(R.layout.team_list, null);
TextView teamName = (TextView) view.findViewById(R.id.team_name);
ImageView nation = (ImageView) view.findViewById(R.id.nation);
String name = values[position];
teamName.setText(name);
if (position < 5) {
nation.setImageResource(R.drawable.thailand);
} else if (position < 11) {
nation.setImageResource(R.drawable.england);
} else {
nation.setImageResource(R.drawable.spain);
}
return row;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<EditText
android:id="@+id/search"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="Search Here"/>
<ListView
android:id="@android:id/list"
android:cacheColorHint="#00000000"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
public class TeamListActivity extends ListActivity {
EditText searchBox;
ListView list;
ArrayList<String> teamList;
static final String[] TEAM = new String[] { "Chonburi FC", "Muangthong",
"Buriram Utd", "BEC Tero", "Bangkok Glass", "Liverpool", "Man Utd",
"Chelsea", "Arsenal", "Man City", "Barcelona", "Real Madrid",
"At.Madrid", "Valencia", "At.Bilbao" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_search);
searchBox = (EditText) findViewById(R.id.search);
list = (ListView) findViewById(android.R.id.list);
setListAdapter(new CustomAdapter(this, TEAM));
teamList = new ArrayList<String> (Arrays.asList(TEAM));
searchBox.addTextChangedListener(textSearchOnChanged);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String name = (String) getListAdapter().getItem(position);
Toast.makeText(this, name, Toast.LENGTH_SHORT).show();
}
private TextWatcher textSearchOnChanged = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
String text = searchBox.getText().toString();
int textLength = text.length();
int size = TEAM.length;
teamList.clear();
for (int i = 0; i < size; i++) {
String profileName = TEAM[i];
if (textLength <= profileName.length()) {
if (text.equalsIgnoreCase(profileName.substring(0,
textLength))) {
teamList.add(TEAM[i]);
}
}
}
String[] NEW_TEAM_LIST = new String[teamList.size()];
NEW_TEAM_LIST = teamList.toArray(NEW_TEAM_LIST);
setListAdapter(new CustomAdapter(TeamListActivity.this, NEW_TEAM_LIST));
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment