android notifydatasetchange()无效

时间:2016-02-04 17:03:46

标签: android listview custom-adapter notifydatasetchanged

我在notifydatasetchanged()上有错误。任何人都可以给我一个合适的解决方案吗?

我已经尝试了所有可能的情况,但我找不到解决方案。

我已将notifydatasetchanged()作为adapter.notifydatasetchanged()附加,但它根本不起作用,如果我这样做:oncreate(null)它在MainActivity中显示错误。

我的doInBackground方法是:

 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()) {
               Bitmap bit_thumb = 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 = MediaStore.Images.Media.getBitmap(resolver, Uri.parse(image_thumb));
                    } else {
                        bit_thumb = MediaStore.Images.Media.getBitmap(resolver, Uri.parse(image_thumb));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

                SelectUser selectUser = new SelectUser();
                selectUser.setThumb(bit_thumb);
                selectUser.setName(name);
                selectUser.setPhone(phoneNumber);
                selectUser.setCheckedBox();
                selectUsers.add(selectUser);
            }
        } else {
            Log.e("Cursor close 1", "----------------");
        }
        //phones.close();
        return null;
    }

protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);

    adapter = new SelectUserAdapter(selectUsers, MainActivity.this);
    listView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    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);
}

适配器是:

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

}

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

这是logcat:

FATAL EXCEPTION: main Process: com.jamol.contacts, PID: 18190 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(2131492978, class android.widget.ListView) with Adapter(class com.jamol.contacts.SelectUserAdapter)]
                                                                        at android.widget.ListView.layoutChildren(ListView.java:1584)
                                                                        at android.widget.AbsListView.onLayout(AbsListView.java:2645)
                                                                        at android.view.View.layout(View.java:16939)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5409)
                                                                        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077)
                                                                        at android.view.View.layout(View.java:16939)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5409)
                                                                        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                        at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                        at android.view.View.layout(View.java:16939)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5409)
                                                                        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702)

1 个答案:

答案 0 :(得分:1)

不要在活动中这样做。 在适配器中覆盖此方法创建一个获取更新值的方法。

npm install --save angular2@2.0.0-beta.3 es6-promise@^3.0.2 es6-shim@^0.33.3 reflect-metadata@0.1.2 rxjs@5.0.0-beta.0 zone.js@0.5.11