我必须从 BaseCardAdapter 的列表中删除一个对象。这个 BaseCardAdapter 用于我的 SwipeCardView。我尝试在按下按钮后移除一个物体。之后我需要通知适配器一个对象被删除。不幸的是我收到这个错误:
java.lang.IllegalMonitorStateException: object not locked by thread before notify()
这是我的代码:
public class FeedAdapter extends BaseCardAdapter {
private ArrayList<EventObject> events;
private Activity activity;
private TextView property1, property2, property3, usernameage;
private ImageView userthumb;
public FeedAdapter(ArrayList<EventObject> events, Activity activity) {
this.events = events;
this.activity = activity;
}
@Override
public int getCount() {
return events.size();
}
@Override
public int getCardLayoutId() {
return R.layout.model_idea_feed;
}
@Override
public void onBindData(final int position, View cardview) {
if (events.size() == 0 || events == null){
return;
}
ImageView thumb = cardview.findViewById(R.id.feed_thumb_model);
userthumb = cardview.findViewById(R.id.feed_profile_thumb);
final TextView eventname = cardview.findViewById(R.id.feed_name_model);
usernameage = cardview.findViewById(R.id.feed_username_model);
TextView date = cardview.findViewById(R.id.feed_date_model);
TextView additional = cardview.findViewById(R.id.feed_additional_model);
TextView address = cardview.findViewById(R.id.feed_adress_model);
property1 = cardview.findViewById(R.id.feed_property1_model);
property2 = cardview.findViewById(R.id.feed_property2_model);
property3 = cardview.findViewById(R.id.feed_property3_model);
ImageButton save = cardview.findViewById(R.id.feed_savebtn_model);
ImageButton accept = cardview.findViewById(R.id.feed_accept_model);
switch(Integer.parseInt(events.get(position).getPosition())) {
case 0:
thumb.setBackgroundResource(R.drawable.berlin0);
break;
case 1:
thumb.setBackgroundResource(R.drawable.berlin1);
break;
case 2:
thumb.setBackgroundResource(R.drawable.berlin2);
break;
default:
thumb.setBackgroundResource(R.drawable.berlin2);
break;
}
eventname.setText(events.get(position).getName());
date.setText(events.get(position).getDate());
additional.setText(events.get(position).getInfos());
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(activity, Locale.getDefault());
try {
addresses = geocoder.getFromLocation(Double.parseDouble(events.get(position).getLat()),Double.parseDouble(events.get(position).getLng()), 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
String adderess = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
String knownName = addresses.get(0).getFeatureName();
address.setText(adderess);
} catch (IOException e) {
e.printStackTrace();
}
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
events.remove(events.get(position));
//Here Appears the error:
events.notify();
}
});
}
}
请您参考如下方法:
notify()
与列表和适配器完全无关。
您想要的是 notifyDataSetChanged()
,在适配器本身而不是数据列表上调用。
替换:
events.notify();
与:
notifyDataSetChanged();
或者:
notifyItemRemoved(position);