Guest User

Untitled

a guest
Nov 25th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. import android.app.ListActivity;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.text.Editable;
  6. import android.text.TextWatcher;
  7. import android.view.LayoutInflater;
  8. import android.view.View;
  9. import android.widget.AdapterView;
  10. import android.widget.EditText;
  11. import android.widget.ListView;
  12. import java.util.ArrayList;
  13.  
  14.  
  15. public class Group extends ListActivity {
  16.  
  17. ArrayList<Players> originalValues;
  18. LayoutInflater inflater;
  19. int noOfPlayers = 10;
  20.  
  21. @Override
  22. public void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.grouplist);
  25. final EditText searchBox = (EditText) findViewById(R.id.searchBox);
  26. ListView playersListView = (ListView) findViewById(android.R.id.list);
  27.  
  28. inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  29.  
  30. originalValues = new ArrayList<Players>();
  31.  
  32. for (int i = 0; i < noOfPlayers; i++) {
  33. // here you initialise the class with your own data
  34. Players players = new Players(i, "name" + i, "team", R.drawable.ic_launcher);
  35.  
  36. originalValues.add(players);
  37. }
  38.  
  39. final CustomAdapter adapter = new CustomAdapter(this, R.layout.players, originalValues);
  40.  
  41. playersListView.setAdapter(adapter);
  42. searchBox.addTextChangedListener(new TextWatcher() {
  43.  
  44.  
  45. public void onTextChanged(CharSequence s, int start, int before, int count) {
  46. String searchString = searchBox.getText().toString();
  47. int textLength = searchString.length();
  48.  
  49. adapter.clearSearchResult();
  50.  
  51. for (int i = 0; i < originalValues.size(); i++) {
  52. String playerName = originalValues.get(i).getName();
  53. if (textLength <= playerName.length()) {
  54. // compare the String in EditText with Names in the
  55. // ArrayList
  56. if (searchString.equalsIgnoreCase(playerName.substring(0, textLength)))
  57. adapter.addSeachResult(originalValues.get(i));
  58. }
  59. }
  60.  
  61. adapter.notifyDataSetChanged();
  62. }
  63.  
  64. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  65.  
  66. }
  67.  
  68. public void afterTextChanged(Editable s) {
  69.  
  70. }
  71. });
  72.  
  73.  
  74. playersListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  75. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  76.  
  77. switch ((int) adapter.getItemId(position)) {
  78. case 0:
  79. Intent newActivity = new Intent(Group.this, Barca.class);
  80. startActivity(newActivity);
  81. break;
  82. case 1:
  83. etc
  84. }
  85. }
  86. });
  87. }
  88.  
  89. }
  90.  
  91. import android.content.Context;
  92. import android.view.LayoutInflater;
  93. import android.view.View;
  94. import android.view.ViewGroup;
  95. import android.widget.BaseAdapter;
  96. import android.widget.ImageView;
  97. import android.widget.TextView;
  98. import java.util.ArrayList;
  99.  
  100.  
  101. class CustomAdapter extends BaseAdapter {
  102.  
  103. ArrayList<Players> searchResults;
  104.  
  105. ViewHolder viewHolder;
  106.  
  107. public CustomAdapter(Context context, int textViewResourceId, ArrayList<Players> results) {
  108. searchResults = new ArrayList<>(results);
  109. }
  110.  
  111. @Override
  112. public View getView(int position, View convertView, ViewGroup parent) {
  113.  
  114. if (convertView == null) {
  115. convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.players, null);
  116. viewHolder = new ViewHolder();
  117.  
  118. viewHolder.photo = (ImageView) convertView.findViewById(R.id.photo);
  119. viewHolder.name = (TextView) convertView.findViewById(R.id.name);
  120. viewHolder.team = (TextView) convertView.findViewById(R.id.team);
  121.  
  122.  
  123. convertView.setTag(viewHolder);
  124.  
  125. } else
  126. viewHolder = (ViewHolder) convertView.getTag();
  127.  
  128. int photoId = (Integer) searchResults.get(position).getPicture();
  129.  
  130. viewHolder.photo.setImageDrawable(parent.getContext().getResources().getDrawable(photoId));
  131. viewHolder.name.setText(searchResults.get(position).getName());
  132. viewHolder.team.setText(searchResults.get(position).getTeam());
  133.  
  134.  
  135. return convertView;
  136.  
  137. }
  138.  
  139. public void clearSearchResult() {
  140. searchResults.clear();
  141. }
  142.  
  143. public void addSeachResult(Players result) {
  144. this.searchResults.add(result);
  145. }
  146.  
  147. private class ViewHolder {
  148. ImageView photo;
  149. TextView name, team;
  150.  
  151. }
  152.  
  153. @Override
  154. public int getCount() {
  155. return searchResults.size();
  156. }
  157.  
  158. @Override
  159. public Object getItem(int position) {
  160. return searchResults.get(position);
  161. }
  162.  
  163. @Override
  164. public long getItemId(int position) {
  165. return searchResults.get(position).getId();
  166. }
  167. }
  168.  
  169. public class Players {
  170.  
  171. public Players(int id,String name,String team,int picture){
  172. this.id = id;
  173. this.name = name;
  174. this.team = team;
  175. this.picture = picture;
  176. }
  177.  
  178. private int id;
  179. private String name;
  180. private String team;
  181. private int picture;
  182.  
  183. public String getName() {
  184. return name;
  185. }
  186.  
  187. public void setName(String name) {
  188. this.name = name;
  189. }
  190.  
  191. public String getTeam() {
  192. return team;
  193. }
  194.  
  195. public void setTeam(String team) {
  196. this.team = team;
  197. }
  198.  
  199. public int getPicture() {
  200. return picture;
  201. }
  202.  
  203. public void setPicture(int picture) {
  204. this.picture = picture;
  205. }
  206.  
  207. public int getId() {
  208. return id;
  209. }
  210. }
Add Comment
Please, Sign In to add comment