title: How to properly implement an Adapter for a ListView
body: <p>I have a <code>ListView</code> in a <code>Fragment</code> and I want to update the data in the <code>ListView</code> when I return from another <code>Activity</code>. I have overwritten the <code>onResume()</code> method in the <code>Fragment</code>, modify the data in the <code>Adapter</code> and call <code>notifyDataSetChanged()</code> on the <code>Adpater</code> but somehow the <code>ListView</code> is not being updated. I suspect that there is something wrong with my <code>Adapter</code>, but I just can't seem to find the error.</p>
  
  <p>Here's code of my <code>Adpater</code>:</p>
  
  <pre><code>class ManualExceptionsListAdapter extends BaseAdapter {
  
      private LayoutInflater mInflater;
      private TextView mManualExceptions;
      SwitchCompat mSwitch;
      TextView name;
      final Context context = getActivity();
      final SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
      int a;
      int ifUse = 0;
  
      ManualExceptionsListAdapter(LayoutInflater inflater) {
          mInflater = inflater;
      }
  
      @Override
      public int getCount() {
          return (mPermanentManualException.size()+mContactsExceptionNumber.size());
      }
  
      @Override
      public Object getItem(int i) {
          return null;
      }
  
      @Override
      public long getItemId(int i) {
          return 0;
      }
  
      @Override
      public int getItemViewType(int position) {
          if (position &lt; (mContactsExceptionNumber.size())) {
              a = 0;
              if(position == (mContactsExceptionNumber.size()-1)){
                  ifUse = 1;
              }
              return a;
          } else {
              a = 1;
              return a;
          }
      }
  
      @Override
      public int getViewTypeCount() {
          return 2;
      }
  
      @Override
      public void notifyDataSetChanged() {
          super.notifyDataSetChanged();
      }
  
      @Override
      public View getView(int i, View view, ViewGroup viewGroup) {
          final int pos;
          if(mContactsExceptionNumber.size()&gt;0) {
              pos = i - (mContactsExceptionNumber.size());
          }else{
              pos = 0;
          }
          int pos2 = 0;
          int type = getItemViewType(i);
          if(ifUse == 1){
              if(mContactsExceptionNumber.size()&gt;0) {
                  pos2 = i - (mContactsExceptionNumber.size());
                  Exceptions.index = pos2;
              }
          }
          View v = view;
          if (view == null) {
              switch (type) {
                  case 0:
                      v = mInflater.inflate(R.layout.contacts_exception_row, null);
                      name = (TextView) v.findViewById(R.id.contact_name);
                      name.setText(mContactsExceptionNames.get(i));
                      break;
                  case 1:
                      v = mInflater.inflate(R.layout.manual_exception_row, null);
                      mManualExceptions = (TextView) v.findViewById(R.id.manual_exception_number);
                      mSwitch = (SwitchCompat) v.findViewById(R.id.manual_exception_switch);
                      mManualExceptions.setText(mPermanentManualException.get(pos2));
                      mSwitch.setTag(i);
                      try {
                          if (mManualExceptionList.contains(mPermanentManualException.get(pos2))) {
                              mSwitch.setChecked(true);
                          }
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
                      break;
              }
          }else{
              switch (type) {
                  case 0:
                      v = mInflater.inflate(R.layout.contacts_exception_row, null);
                      name = (TextView) v.findViewById(R.id.contact_name);
                      name.setText(mContactsExceptionNames.get(i));
                      break;
                  case 1:
                      v = mInflater.inflate(R.layout.manual_exception_row, null);
                      mManualExceptions = (TextView) v.findViewById(R.id.manual_exception_number);
                      mSwitch = (SwitchCompat) v.findViewById(R.id.manual_exception_switch);
                      mManualExceptions.setText(mPermanentManualException.get(pos2));
                      mSwitch.setTag(i);
                      try {
                          if (mManualExceptionList.contains(mPermanentManualException.get(pos2))) {
                              mSwitch.setChecked(true);
                          }
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
                      break;
              }
          }
  
          try {
              mSwitch.setOnTouchListener(new View.OnTouchListener() {
                  @Override
                  public boolean onTouch(View view, MotionEvent motionEvent) {
                      isTouched = true;
                      return false;
                  }
              });
          } catch (NullPointerException e) {
              e.printStackTrace();
          }
          try {
              mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                  @Override
                  public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                      if (isTouched) {
                          if (b) {
                              if (!mManualExceptionList.contains((mPermanentManualException.get(pos)))) {
                                  mManualExceptionList.add((mPermanentManualException.get(pos)));
                              }
                              mSharedPreferences.edit().putString("ManualExceptions", TextUtils.
                                      join(",", mManualExceptionList)).apply();
  
                          } else {
                              try {
                                  mManualExceptionList.remove((mPermanentManualException.get(pos)));
                                  mSharedPreferences.edit().putString("ManualExceptions", TextUtils.
                                          join(",", mManualExceptionList)).apply();
                              } catch (Exception e) {
                                  e.printStackTrace();
                              }
                          }
                          Log.d("RejectCall", "Permanent " + TextUtils.join(",", mPermanentManualException));
                          Log.d("RejectCall", TextUtils.join(",", mManualExceptionList));
                      }
                  }
              });
          } catch (NullPointerException e) {
              e.printStackTrace();
          }
  
          return v;
      }
  
  }
  </code></pre>