Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.app.ListActivity;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.text.Editable;
- import android.text.TextWatcher;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.EditText;
- import android.widget.ListView;
- import java.util.ArrayList;
- public class Group extends ListActivity {
- ArrayList<Players> originalValues;
- LayoutInflater inflater;
- int noOfPlayers = 10;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.grouplist);
- final EditText searchBox = (EditText) findViewById(R.id.searchBox);
- ListView playersListView = (ListView) findViewById(android.R.id.list);
- inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- originalValues = new ArrayList<Players>();
- for (int i = 0; i < noOfPlayers; i++) {
- // here you initialise the class with your own data
- Players players = new Players(i, "name" + i, "team", R.drawable.ic_launcher);
- originalValues.add(players);
- }
- final CustomAdapter adapter = new CustomAdapter(this, R.layout.players, originalValues);
- playersListView.setAdapter(adapter);
- searchBox.addTextChangedListener(new TextWatcher() {
- public void onTextChanged(CharSequence s, int start, int before, int count) {
- String searchString = searchBox.getText().toString();
- int textLength = searchString.length();
- adapter.clearSearchResult();
- for (int i = 0; i < originalValues.size(); i++) {
- String playerName = originalValues.get(i).getName();
- if (textLength <= playerName.length()) {
- // compare the String in EditText with Names in the
- // ArrayList
- if (searchString.equalsIgnoreCase(playerName.substring(0, textLength)))
- adapter.addSeachResult(originalValues.get(i));
- }
- }
- adapter.notifyDataSetChanged();
- }
- public void beforeTextChanged(CharSequence s, int start, int count, int after) {
- }
- public void afterTextChanged(Editable s) {
- }
- });
- playersListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- switch ((int) adapter.getItemId(position)) {
- case 0:
- Intent newActivity = new Intent(Group.this, Barca.class);
- startActivity(newActivity);
- break;
- case 1:
- etc
- }
- }
- });
- }
- }
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.ImageView;
- import android.widget.TextView;
- import java.util.ArrayList;
- class CustomAdapter extends BaseAdapter {
- ArrayList<Players> searchResults;
- ViewHolder viewHolder;
- public CustomAdapter(Context context, int textViewResourceId, ArrayList<Players> results) {
- searchResults = new ArrayList<>(results);
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- if (convertView == null) {
- convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.players, null);
- viewHolder = new ViewHolder();
- viewHolder.photo = (ImageView) convertView.findViewById(R.id.photo);
- viewHolder.name = (TextView) convertView.findViewById(R.id.name);
- viewHolder.team = (TextView) convertView.findViewById(R.id.team);
- convertView.setTag(viewHolder);
- } else
- viewHolder = (ViewHolder) convertView.getTag();
- int photoId = (Integer) searchResults.get(position).getPicture();
- viewHolder.photo.setImageDrawable(parent.getContext().getResources().getDrawable(photoId));
- viewHolder.name.setText(searchResults.get(position).getName());
- viewHolder.team.setText(searchResults.get(position).getTeam());
- return convertView;
- }
- public void clearSearchResult() {
- searchResults.clear();
- }
- public void addSeachResult(Players result) {
- this.searchResults.add(result);
- }
- private class ViewHolder {
- ImageView photo;
- TextView name, team;
- }
- @Override
- public int getCount() {
- return searchResults.size();
- }
- @Override
- public Object getItem(int position) {
- return searchResults.get(position);
- }
- @Override
- public long getItemId(int position) {
- return searchResults.get(position).getId();
- }
- }
- public class Players {
- public Players(int id,String name,String team,int picture){
- this.id = id;
- this.name = name;
- this.team = team;
- this.picture = picture;
- }
- private int id;
- private String name;
- private String team;
- private int picture;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getTeam() {
- return team;
- }
- public void setTeam(String team) {
- this.team = team;
- }
- public int getPicture() {
- return picture;
- }
- public void setPicture(int picture) {
- this.picture = picture;
- }
- public int getId() {
- return id;
- }
- }
Add Comment
Please, Sign In to add comment