title: Android How to change ListView adapter
body: <p>I have an <strong>Activity</strong> with a <strong>TextBox</strong>, a <strong>ListView</strong> with some options that can be placed if I push on it and a <strong>button</strong> to validate data on the textbox.</p>
  
  <p>It works fine. My problem arrives when I want to change the ListView adapter to modify the elements that are displayed on it. And in this moment I get a <strong>LogCat Error</strong> like this:</p>
  
  <pre><code>01-04 16:29:00.157: E/AndroidRuntime(1825): FATAL EXCEPTION: main 
  01-04 16:29:00.157: E/AndroidRuntime(1825): Process: es.sw.android, PID: 1825 
  01-04 16:29:00.157: E/AndroidRuntime(1825): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(16908298, class android.widget.ListView) with Adapter(class es.tlsi.sw.ventanas.UbicacionAdapter)] 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.widget.ListView.layoutChildren(ListView.java:1582) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.widget.AbsListView.onLayout(AbsListView.java:2206) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.view.View.layout(View.java:16075) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.view.ViewGroup.layout(ViewGroup.java:5300) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1466) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.view.View.layout(View.java:16075) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.view.ViewGroup.layout(ViewGroup.java:5300) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.view.View.layout(View.java:16075) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.view.ViewGroup.layout(ViewGroup.java:5300) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.widget.FrameLayout.onLayout(FrameLayout.java:514) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.view.View.layout(View.java:16075) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.view.ViewGroup.layout(ViewGroup.java:5300) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:494) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.view.View.layout(View.java:16075) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.view.ViewGroup.layout(ViewGroup.java:5300) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.widget.FrameLayout.onLayout(FrameLayout.java:514) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.view.View.layout(View.java:16075) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.view.ViewGroup.layout(ViewGroup.java:5300) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2119) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1873) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1073) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5988) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.view.Choreographer.doCallbacks(Choreographer.java:580) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.view.Choreographer.doFrame(Choreographer.java:550) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.os.Handler.handleCallback(Handler.java:739) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.os.Handler.dispatchMessage(Handler.java:95) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.os.Looper.loop(Looper.java:135) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at android.app.ActivityThread.main(ActivityThread.java:5910) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at java.lang.reflect.Method.invoke(Native Method) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at java.lang.reflect.Method.invoke(Method.java:372) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
  01-04 16:29:00.157: E/AndroidRuntime(1825):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
  </code></pre>
  
  <p>As a resume, I want to modify the ListView adapter depending of the data that I must to input in this moment.</p>
  
  <p>Someone know how to do this? I don't post my code because it don't work. I only need a litte guide or link to see some example.</p>
  
  <p>Thanks!</p>
  
  <p><strong>EDIT 1</strong></p>
  
  <p>That's the code that I have to asign to ListView an adapter:</p>
  
  <pre><code>public void onEventMainThread(Event1 event){
      // Charge data list
      this.setListAdapter(new Adapter1(getApplicationContext(),R.id.tv_items,event.getItems1()));
  }
  
  public void onEventMainThread(Event2 event){
      // Charge data list
      this.setListAdapter(new Adapter2(getApplicationContext(),R.id.tv_items,event.getItems2()));
  }
  </code></pre>
  
  <p>And adapters code:</p>
  
  <pre><code>public class Adapter1 extends ArrayAdapter&lt;ObjectX&gt;{
  final Context context;
  final List&lt;ObjectX&gt; objectX;
  
  public Adapter1(Context context, int resource, List&lt;ObjectX&gt; objects){
      super(context,resource,objects);
      this.context = context;
      this.objectX = objects;
  }
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
      View rowView = convertView;
      LayoutInflater inflater = (LayoutInflater) context
              .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      rowView = inflater.inflate(R.layout.listView, null);
  
      TextView location = (TextView) rowView.findViewById(R.id.tv_1);
      TextView type = (TextView) rowView.findViewById(R.id.tv_2);
  
      location.setText("Location: " + objectX.getLocation());
      type.setText("Type: " + objectX.getType());
  
      return rowView;
  }
  }
  </code></pre>
  
  <p>The only thing that change on it is the setText values and typo of object(instead ObjectX is ObjectY)</p>