Android如何更改ListView适配器

时间:2016-01-04 15:57:05

标签: java android android-layout listview android-listview

我有一个带有 TextBox 活动,一个 ListView ,如果我按下它并且按钮以验证文本框中的数据。

工作正常。当我想更改ListView适配器以修改其上显示的元素时,我的问题就到了。在这一刻,我得到一个 LogCat错误,如下所示:

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)

作为简历,我想根据我此时必须输入的数据修改ListView适配器。

有人知道怎么做吗?我不发布我的代码,因为它不起作用。我只需要一个小小的指南或链接来看一些例子。

谢谢!

编辑1

这是我必须将ListView转换为适配器的代码:

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

适配器代码:

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

唯一改变的是setText值和对象的拼写错误(而不是ObjectX是ObjectY)

1 个答案:

答案 0 :(得分:1)

你检查了这个答案吗?

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

我发布了答案,因为我还没有获得评论的许可。