list.notify()引发异常

时间:2018-12-08 18:55:55

标签: java android

我必须从BaseCardAdapter的列表中删除一个对象。此BaseCardAdapter用于我的SwipeCardView。我尝试在按下按钮后删除对象。之后,我需要通知适配器已删除一个对象。不幸的是,我收到此错误:

  

java.lang.IllegalMonitorStateException:对象未在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();

        }
    });



}



}

1 个答案:

答案 0 :(得分:1)

notify()与列表和适配器完全无关。

您需要的是notifyDataSetChanged(),它是在适配器本身而不是数据列表上调用的。

替换:

events.notify();

使用:

notifyDataSetChanged();

或者:

notifyItemRemoved(position);