gpt4 book ai didi

android notifydatasetchange()不工作

转载 作者:行者123 更新时间:2023-11-29 01:23:47 25 4
gpt4 key购买 nike

我在 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)

最佳答案

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

    @Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
public SelectUserAdapter(List<SelectUser> selectUsers, Context context) {
_data = selectUsers;
_c = context;
updateResults(selectUsers);
}

public void updateResults(List<SelectUser> results) {
userList = results;//List<SelectUser> userList
notifyDataSetChanged();
}

关于android notifydatasetchange()不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35207124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com