notifyDataSetChanged

时间:2016-02-04 11:52:07

标签: android notifydatasetchanged

我在notifydataset更改时收到错误,我添加了通知数据集已更改,即使我收到错误

public SelectUserAdapter(List<SelectUser> selectUsers, Context context) {
    _data = selectUsers;
    _c = context;
    this.arraylist = new ArrayList<>();
    this.arraylist.addAll(_data);
}

@Override
public int getCount() {
    return _data.size();
}

@Override
public Object getItem(int i) {
    return _data.get(i);
}

@Override
public long getItemId(int i) {
    return i;
}

@SuppressLint("InflateParams")
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
    View view = convertView;
    if (view == null) {
        LayoutInflater li = (LayoutInflater) _c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = li.inflate(com.jamol.contacts.R.layout.contact_info, null);
    } else {
        view = convertView;
    }

    ViewHolder v = new ViewHolder();

    v.title = (TextView) view.findViewById(com.jamol.contacts.R.id.name);
    v.check = (CheckBox) view.findViewById(com.jamol.contacts.R.id.check);
    v.setPhone((TextView) view.findViewById(com.jamol.contacts.R.id.no));
    v.imageView = (ImageView) view.findViewById(com.jamol.contacts.R.id.pic);

    final SelectUser data = _data.get(i);
    v.title.setText(data.getName());
    v.check.setChecked(data.getCheckedBox());
    v.getPhone().setText(data.getPhone());

    try {

        if (data.getThumb() != null) {
            v.imageView.setImageBitmap(data.getThumb());
        } else {
            v.imageView.setImageResource(com.jamol.contacts.R.drawable.ic_user);
        }
    } catch (OutOfMemoryError e) {
        v.imageView.setImageDrawable(this._c.getDrawable(com.jamol.contacts.R.drawable.ic_user));
        e.printStackTrace();
    }
    view.setTag(data);
    return view;
}


public void filter(String charText) {
    if (charText != null) {
        charText = charText.toLowerCase(Locale.getDefault());
        _data.clear();
        if (charText.length() == 0) {
            _data.addAll(arraylist);
        } else {
            for (SelectUser wp : arraylist)
                if (wp.getName().toLowerCase(Locale.getDefault())
                        .contains(charText)) {
                    _data.add(wp);
                }
        }
    }
    notifyDataSetChanged();
}
static class ViewHolder {
    ImageView imageView;
    TextView title;
    CheckBox check;


       private TextView phone;

        public TextView getPhone() {
            return phone;
        }

        public void setPhone(TextView phone) {
            this.phone = phone;
        }
    }
}

错误是

  

致命的例外:主要                                                                       过程:com.jamol.contacts,PID:20086                                                                       java.lang.IllegalStateException:适配器的内容有   已更改,但ListView未收到通知。确保   您的适配器的内容不会从后台线程修改,但是   仅来自UI线程。确保适配器调用   notifyDataSetChanged()在其内容发生变化时。 [在   带适配器的ListView(2131492978,类android.widget.ListView)(类   com.jamol.contacts.SelectUserAdapter)]                                                                           在android.widget.ListView.layoutChildren(ListView.java:1584)                                                                           在android.widget.AbsListView.onLayout(AbsListView.java:2645)                                                                           在android.view.View.layout(View.java:16939)                                                                           在android.view.ViewGroup.layout(ViewGroup.java:5409)                                                                           在android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077)                                                                           在android.view.View.layout(View.java:16939)                                                                           在android.view.ViewGroup.layout(ViewGroup.java:5409)                                                                           在android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)                                                                           在android.widget.FrameLayout.onLayout(FrameLayout.java:514)                                                                           在android.view.View.layout(View.java:16939)                                                                           在android.view.ViewGroup.layout(ViewGroup.java:5409)                                                                           在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702)                                                                           在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556)                                                                           在android.widget.LinearLayout.onLayout(LinearLayout.java:1465)                                                                           在android.view.View.layout(View.java:16939)                                                                           在android.view.ViewGroup.layout(ViewGroup.java:5409)                                                                           在android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)                                                                           在android.widget.FrameLayout.onLayout(FrameLayout.java:514)                                                                           在android.view.View.layout(View.java:16939)                                                                           在android.view.ViewGroup.layout(ViewGroup.java:5409)                                                                           在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2464)                                                                           在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2167)                                                                           在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1301)                                                                           在   android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:7016)                                                                           在   android.view.Choreographer $ CallbackRecord.run(Choreographer.java:777)                                                                           在android.view.Choreographer.doCallbacks(Choreographer.java:590)                                                                           在android.view.Choreographer.doFrame(Choreographer.java:560)                                                                           在   android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:763)                                                                           在android.os.Handler.handleCallback(Handler.java:739)                                                                           在android.os.Handler.dispatchMessage(Handler.java:95)                                                                           在android.os.Looper.loop(Looper.java:145)                                                                           在android.app.ActivityThread.main(ActivityThread.java:6946)                                                                           at java.lang.reflect.Method.invoke(Native Method)                                                                           在java.lang.reflect.Method.invoke(Method.java:372)                                                                           在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1404)                                                                           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)02-04   16:52:56.033 20086-20086 / com.jamol.contacts I / Process:发送信号。   PID:20086 SIG:9

MainActivity是

private class LoadContact extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Nullable
    @Override
    protected Void doInBackground(Void... voids) {
        if (phones != null) {
            Log.e("count", "" + phones.getCount());
            if (phones.getCount() == 0) {
                Log.d("No Contacts", "No Contacts");
            }

            while (phones.moveToNext()) {
                AtomicReference<Bitmap> bit_thumb = new AtomicReference<>(null);
                String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                String image_thumb = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
                try {
                    if (image_thumb != null) {
                        bit_thumb.set(MediaStore.Images.Media.getBitmap(resolver, Uri.parse(image_thumb)));
                    } else {
                        Log.d("dont show any images", "dont show any images");
                        //bit_thumb = MediaStore.Images.Media.getBitmap(resolver, Uri.parse(image_thumb));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

                SelectUser selectUser = new SelectUser();
                selectUser.setThumb(bit_thumb.get());
                selectUser.setName(name);
                selectUser.setPhone(phoneNumber);
                selectUser.setCheckedBox();
                if (selectUsers == null) throw new AssertionError();
                selectUsers.add(selectUser);


            }
        } else {
            Log.e("Cursor close 1", "----------------");
        }
        //phones.close();
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        adapter = new SelectUserAdapter(selectUsers, MainActivity.this);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
                                            @Override
                                            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                                                synchronized (MainActivity.this) {
                                                    if (firstClickTime == 0) {
                                                        firstClickTime = SystemClock.elapsedRealtime();
                                                        nonDoubleClick = true;
                                                    } else {
                                                        long deltaTime = SystemClock.elapsedRealtime() - firstClickTime;
                                                        firstClickTime = 0;
                                                        if (deltaTime < DOUBLE_CLICK_TIMEOUT) {
                                                            nonDoubleClick = false;
                                                            this.onItemDoubleClick(adapterView, view, position, l);
                                                            return;
                                                        }
                                                    }

                                                    view.postDelayed(new Runnable() {
                                                        @Override
                                                        public void run() {
                                                            if (nonDoubleClick) {
                                                                Log.d("Single click", "single click");
                                                            }
                                                        }

                                                    }, DOUBLE_CLICK_TIMEOUT);
                                                }
                                            }

                                            public void onItemDoubleClick(AdapterView<?> parent, View view, int position, long id) {
                                                String selected = ((TextView) view.findViewById(com.jamol.contacts.R.id.no)).getText().toString();
                                                try {
                                                    mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.call);
                                                    mediaPlayer.setVolume(1.0f, 1.0f);
                                                    mediaPlayer.setLooping(false);
                                                    mediaPlayer.start();
                                                    Vibrator v = (Vibrator) MainActivity.this.getSystemService(Context.VIBRATOR_SERVICE);
                                                    v.vibrate(500);
                                                    Intent in = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + selected));
                                                    startActivity(in);
                                                } catch (SecurityException e) {
                                                    Log.e("PERMISSION_EXCEPTION", "PERMISSION_NOT_GRANTED");
                                                }
                                            }
                                        }
        );
        listView.setFastScrollEnabled(true);
    }

}

3 个答案:

答案 0 :(得分:0)

您正在调用filter(String str)的后台线程中调用notifyDataSetChanged()。确保在主UI线程上调用notifyDataSetChanged()

答案 1 :(得分:0)

尝试在notifyDataSetChanged()方法

listView.setAdapter(adapter);之后添加onPostExecute()

答案 2 :(得分:0)

您正在呼叫&#34; notifyDataSetChanged();&#34;在SelectUserAdapter中,删除它。你可以使用&#34; notifyDataSetChanged()&#34;列表selectUsers更改/修改后。呼叫&#34; notifyDataSetChanged();&#34;像这样

 adapter.notifyDataSetChanged();

注意:你应该调用它&#34; adapter.notifyDataSetChanged();&#34;将适配器设置为listView(listView.setAdapter(adapter);)

之后