Guest User

Untitled

a guest
Mar 25th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. package es.simplelistview.adapter;
  2.  
  3. import android.annotation.SuppressLint;
  4. import android.content.Context;
  5. import android.support.annotation.NonNull;
  6. import android.support.annotation.Nullable;
  7. import android.support.design.widget.Snackbar;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.ArrayAdapter;
  12. import android.widget.TextView;
  13.  
  14. import java.util.List;
  15.  
  16. import es.simplelistview.R;
  17. import es.simplelistview.model.Person;
  18.  
  19. /**
  20. * Created by elissa on 3/12/18.
  21. */
  22.  
  23. public class PersonListAdapter extends ArrayAdapter<Person> implements View.OnClickListener {
  24.  
  25. private Context thisContext;
  26. private List<Person> personList;
  27.  
  28. public PersonListAdapter (@NonNull Context context, int resource, List<Person> personList) {
  29.  
  30. super (context, resource);
  31. this.thisContext = context;
  32. this.personList = personList;
  33. }
  34.  
  35. /**
  36. * To get List Size
  37. */
  38. @Override public int getCount () {
  39.  
  40. return personList.size ();
  41. }
  42.  
  43. /**
  44. * To get List Position
  45. */
  46. @Nullable @Override public Person getItem (int position) {
  47.  
  48. return personList.get (position);
  49. }
  50.  
  51. @SuppressLint("ViewHolder") @NonNull @Override public View getView (
  52. int position, @Nullable View convertView, @NonNull ViewGroup parent) {
  53.  
  54. //Get the data item base on position
  55. Person person = personList.get (position);
  56.  
  57. //Inflate the item template
  58. LayoutInflater li = LayoutInflater.from (thisContext);
  59. convertView = li.inflate (R.layout.list_item, parent, false);
  60.  
  61. //Click on list view item
  62. convertView.setTag (position);
  63. convertView.setOnClickListener (this);
  64.  
  65. //Assign data
  66. TextView txtName = convertView.findViewById (R.id.lblName);
  67. TextView txtPhoneNumber = convertView.findViewById (R.id.lblPhoneNumber);
  68. txtName.setText (person.getName ());
  69. txtPhoneNumber.setText (person.getPhNo ());
  70.  
  71. //return the view
  72. return convertView;
  73. }
  74.  
  75. @Override public void onClick (View view) {
  76.  
  77. int position = (Integer) view.getTag ();
  78. Person person = personList.get (position);
  79. Snackbar.make (view, person.getName (), Snackbar.LENGTH_LONG).show ();
  80. }
  81. }
Add Comment
Please, Sign In to add comment