【问题标题】:android -listView -The content of the adapter has changed but ListView did not receive a notificationandroid -listView -adapter的内容发生了变化但是ListView没有收到通知
【发布时间】:2014-11-26 05:51:35
【问题描述】:

这是我的适配器,我通过 json 从服务器获取数据,然后使用 BaseAdapter 填充我的 ListView。制作了一个 30 行的列表视图,当用户到达列表视图的末尾时,GetContacts 再次调用,另外 30 个项目将添加到列表视图中。

这是代码:

ListAdapter ladap;

private class GetContacts AsyncTask<Void, Void,ArrayList<HashMap<String, String>>> {    
@Override
protected Void doInBackground(Void... arg0) {
    Spots_tab1_json sh = new Spots_tab1_json();
    String jsonStr = sh.makeServiceCall(url + page, Spots_tab1_json.GET);

    ArrayList<HashMap<String, String>> dataC = new ArrayList<HashMap<String, String>>();

    if (jsonStr != null) {
        try {
            JSONObject jsonObj = new JSONObject(jsonStr);
            contacts = jsonObj.getJSONArray(TAG_CONTACTS);

                for (int i = 0; i < contacts.length(); i++) {
                JSONObject c = contacts.getJSONObject(i);
                String id = new String(c.getString("id").getBytes("ISO-8859-1"), "UTF-8");
                String dates = new String(c.getString("dates").getBytes("ISO-8859-1"), "UTF-8");
                String price = new String(c.getString("gheymat").getBytes("ISO-8859-1"), "UTF-8");
                HashMap<String, String> contact = new HashMap<String, String>();
                contact.put("id", id);
                contact.put("dates", dates);
                contact.put("price", price);
                dataC.add(contact);
            }
            }
        } catch (JSONException e) {
            goterr = true;
        } catch (UnsupportedEncodingException e) {
            goterr = true;
        }
    } else {
        goterr = true;
    }
    return dataC;
}

@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
    super.onPostExecute(result);
    if (!isCancelled() && goterr == false) {
        if(ladap==null){
            ladap=new ListAdapter(MainActivity.this,result);
            lv.setAdapter(ladap);
        }else{
            ladap.addAll(result);
        }

}

}

public class ListAdapter extends BaseAdapter {
Activity activity;
public ArrayList<HashMap<String, String>> list;

public ListAdapter(Activity activity,ArrayList<HashMap<String, String>> list) {
    super();
    this.activity = (Activity) activity;
    this.list = list;
}

public void addAll(ArrayList<HashMap<String, String>> result) {
    Log.v("this",result.size()+" resultsize");
    this.list = result;
    notifyDataSetChanged();
}

public int getCount() {
    return contactList.size();
}

public void addAll(ArrayList<HashMap<String, String>> result) {
        if(list==null){
            list = new ArrayList<HashMap<String, String>>();
          }
          list.addAll(result);
          notifyDataSetChanged();
}

public Object getItem(int position) {
    return contactList.get(position);
}

public long getItemId(int arg0) {
    return 0;
}

private class ViewHolder {
    TextView title,price;
    ImageView img ; 
    //RelativeLayout rl; 
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    LayoutInflater inflater = activity.getLayoutInflater();
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.item, null);
        holder = new ViewHolder();
        holder.title = (TextView) convertView.findViewById(R.id.title);
        holder.price = (TextView) convertView.findViewById(R.id.price);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

        item = contactList.get(position);
        holder.price.setText(item.get("price"));
    return convertView;
}
}

我自己没有遇到此错误,请购买我的错误跟踪鞋,它在不同的设备和 android 版本上经常发生。

关于这个有 2 个错误 1号:

STACK_TRACE = 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. [in ListView(2131034188, class android.widget.ListView) with Adapter(class ir.klach.persiandesigners.MainActivity$ListAdapter)]
at android.widget.ListView.layoutChildren(ListView.java:1535)
at android.widget.AbsListView$CheckForTap.run(AbsListView.java:2215)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3770)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
at dalvik.system.NativeStart.main(Native Method)

PHONE_MODEL = GT-I9070
APP_VERSION_NAME = 2.7
ANDROID_VERSION = 2.3.6

2号:

STACK_TRACE = 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. [in ListView(2131034188, class android.widget.ListView) with Adapter(class ir.klach.persiandesigners.MainActivity$ListAdapter)]
at android.widget.ListView.layoutChildren(ListView.java:1551)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:4498)
at android.view.View.dispatchTouchEvent(View.java:7669)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2395)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2401)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2134)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2401)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2134)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2401)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2134)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2401)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2134)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2401)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2134)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2401)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2134)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2401)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2134)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2401)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2134)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2284)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1578)
at android.app.Activity.dispatchTouchEvent(Activity.java:2468)
at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.dispatchTouchEvent(ActionBarActivityDelegateICS.java:268)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2232)
at android.view.View.dispatchPointerEvent(View.java:7876)
at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3894)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3778)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5030)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5009)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5107)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:125)
at android.os.Looper.loop(Looper.java:124)
at android.app.ActivityThread.main(ActivityThread.java:5283)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)

PHONE_MODEL = GT-I9082
APP_VERSION_NAME = 2.7
ANDROID_VERSION = 4.2.2

你能帮我解决这个问题吗?

【问题讨论】:

  • 正如你的逻辑所说,“确保适配器的内容不是从后台线程修改的,而只是从 UI 线程修改”
  • @shayanpourvatan 我敢肯定,它运行在 postExceute 上,所以它运行在 UI 线程上

标签: android android-listview android-adapter


【解决方案1】:

尝试将新数据添加到列表适配器数据持有者,而不是将新数据分配给列表适配器数据持有者:

public void addAll(ArrayList<HashMap<String, String>> result) {
    Log.v("this",result.size()+" resultsize");
    if(this.list == null){
       this.list = result;
    }else{
       this.list.addAll(result);
    }
    notifyDataSetChanged();
}

【讨论】:

    【解决方案2】:

    虽然 opPostExecute 在 UI 线程本身上工作。但是你仍然面临这个问题,试试下面的。

    context.runOnUiThread(new Runnable()
       {
            @override
            public void run()
           {
               list.addAll(result);
               notifyDataSetChanged();
           }
      });
    

    希望它对你有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-01
      • 1970-01-01
      相关资源
      最近更新 更多