Guest User

Untitled

a guest
Jul 31st, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. Android - Only show 1 value in Spinner from Adapter
  2. John Brown john@email.com
  3. Sue Fields sue@email.com
  4. Mark Twain mark.twain@email.com
  5.  
  6. John Brown
  7. Sue Fields
  8. Mark Twain
  9.  
  10. public class POCInfoAdapter extends BaseAdapter {
  11.  
  12. //Private objects.
  13. private List<POCInfo> mListPOCInfo;
  14. private LayoutInflater mInflater;
  15.  
  16. //constructor.
  17. public POCInfoAdapter(Context c, List<POCInfo> list){
  18. mListPOCInfo = list;
  19.  
  20. //create layout inflater.
  21. mInflater = LayoutInflater.from(c);
  22. }
  23.  
  24. @Override
  25. public int getCount(){
  26. return mListPOCInfo.size();
  27. }
  28.  
  29. @Override
  30. public Object getItem(int position){
  31. return mListPOCInfo.get(position);
  32. }
  33.  
  34. @Override
  35. public long getItemId(int position){
  36. return position;
  37. }
  38.  
  39. @Override
  40. public View getView(int position, View convertView, ViewGroup parent){
  41. //Get view reference.
  42. View view = convertView;
  43.  
  44. //If view is null.
  45. if(view == null){
  46. //Inflate new layout.
  47. view = mInflater.inflate(R.layout.poc_list, null);
  48.  
  49. //create holder.
  50. ViewHolder holder = new ViewHolder();
  51.  
  52. //Find Controls.
  53. holder.txtFullName = (TextView)view.findViewById(R.id.txtFullName);
  54. holder.txtEmailAddress = (TextView)view.findViewById(R.id.txtEmailAddress);
  55.  
  56. //Set data structure to view.
  57. view.setTag(holder);
  58. }
  59.  
  60. //Get selected POC Info.
  61. POCInfo pocInfo = mListPOCInfo.get(position);
  62.  
  63. if(pocInfo != null){
  64. //query data structure.
  65. ViewHolder holder = (ViewHolder)view.getTag();
  66.  
  67. //Set data to display.
  68. holder.txtFullName.setText(pocInfo.getFullName());
  69. holder.txtEmailAddress.setText(pocInfo.getEmailAddress());
  70. }
  71.  
  72. return view;
  73. }
  74.  
  75. //Class to hold data structure on view with POC Information.
  76. static class ViewHolder{
  77. private TextView txtFullName;
  78. private TextView txtEmailAddress;
  79. }
  80. }
  81.  
  82. Spinner spSpinner = (Spinner)findViewById(R.id.spPOCs);
  83. spSpinner.setAdapter(new POCInfoAdapter(this, DBAdapter.queryAll()));
Add Comment
Please, Sign In to add comment