Advertisement
erwinprasetyo97

WeatherAdapter.java

Aug 19th, 2018
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. package com.erprasetyo.myasynctaskloader;
  2.  
  3. import android.content.Context;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.BaseAdapter;
  8. import android.widget.TextView;
  9.  
  10. import java.util.ArrayList;
  11.  
  12. public class WeatherAdapter extends BaseAdapter
  13. {
  14. private ArrayList<WeatherItems> mData = new ArrayList<>();
  15. private LayoutInflater mInflater;
  16. private Context context;
  17.  
  18. public WeatherAdapter(Context context) {
  19. this.context = context;
  20. mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  21. }
  22.  
  23. public void setData(ArrayList<WeatherItems> items) {
  24. mData = items;
  25. notifyDataSetChanged();
  26. }
  27.  
  28. public void addItem(final WeatherItems item) {
  29. mData.add(item);
  30. notifyDataSetChanged();
  31. }
  32.  
  33. public void clearData() {
  34. mData.clear();
  35. }
  36.  
  37. @Override
  38. public int getItemViewType(int position) {
  39. return 0;
  40. }
  41.  
  42. @Override
  43. public int getViewTypeCount() {
  44. return 1;
  45. }
  46.  
  47. @Override
  48. public int getCount() {
  49. if(mData == null) return 0;
  50. return mData.size();
  51. }
  52.  
  53. @Override
  54. public WeatherItems getItem(int position) {
  55. return mData.get(position);
  56. }
  57.  
  58. @Override
  59. public long getItemId(int position) {
  60. return position;
  61. }
  62.  
  63. @Override
  64. public View getView(int position, View convertView, ViewGroup parent) {
  65. ViewHolder holder = null;
  66. if (convertView == null) {
  67. holder = new ViewHolder();
  68. convertView = mInflater.inflate(R.layout.weather_items, null);
  69. holder.textViewNamaKota = (TextView) convertView.findViewById(R.id.textKota);
  70. holder.textViewTemperature = (TextView) convertView.findViewById(R.id.textDesc);
  71. convertView.setTag(holder);
  72.  
  73. } else {
  74. holder = (ViewHolder)convertView.getTag();
  75. }
  76. holder.textViewNamaKota.setText(mData.get(position).getNama());
  77. holder.textViewTemperature.setText(mData.get(position).getTemperature());
  78. holder.textViewDescription.setText(mData.get(position).getDescription());
  79.  
  80. return convertView;
  81. }
  82.  
  83. private static class ViewHolder {
  84. TextView textViewNamaKota;
  85. TextView textViewTemperature;
  86. TextView textViewDescription;
  87. }
  88.  
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement