-2

I have an Activity with a TextBox, a ListView with some options that can be placed if I push on it and a button to validate data on the textbox.

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 LogCat Error like this:

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)

As a resume, I want to modify the ListView adapter depending of the data that I must to input in this moment.

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.

Thanks!

EDIT 1

That's the code that I have to asign to ListView an adapter:

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()));
}

And adapters code:

public class Adapter1 extends ArrayAdapter<ObjectX>{
final Context context;
final List<ObjectX> objectX;

public Adapter1(Context context, int resource, List<ObjectX> 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;
}
}

The only thing that change on it is the setText values and typo of object(instead ObjectX is ObjectY)

Jota
  • 127
  • 1
  • 4
  • 13
  • 1
    Hi there, I've edited your question to make the stack trace easier to read, but could you please add the relevant code of you project about your listview? Because without it, it will be very difficult for us to help you find the problem. Thank you. (Unfortunately you aren't suppose to ask for guides or tutorials from this site, so if you can edit that part out and put in your code, we can help you.) – mawalker Jan 04 '16 at 16:04
  • Are you trying to modify the content of `ListView` from a different thread? – K Neeraj Lal Jan 04 '16 at 16:05
  • The text of the error is quite clear: `ListView did not receive a notification`, `Make sure your adapter calls notifyDataSetChanged()`, and `Make sure the content of your adapter is not modified from a background thread`. Have you ensured that all 3 of those things are properly implemented? – NoChinDeluxe Jan 04 '16 at 16:10
  • I editted the post with some information and code – Jota Jan 04 '16 at 16:19

1 Answers1

2

Did you check this answer?

Android, ListView IllegalStateException: "The content of the adapter has changed but ListView did not receive a notification"

I'm posting as answer as I don't have permission to comment yet.

Community
  • 1
  • 1
Anuron
  • 66
  • 6
  • have some rep then young one ;) I was in that position for while and hated it. (plus I was in the process of posting a comment saying the exact same thing, so there you go.) – mawalker Jan 04 '16 at 16:10
  • 1
    Trying to get some :-) – Anuron Jan 04 '16 at 16:13