为什么光标从0开始?

时间:2016-05-08 16:36:53

标签: android cursor

我有 MainFragment:

@Override
public void onStart() {
    super.onStart();
    MissedCallCounter listener = new MissedCallCounter(getActivity());
    TelephonyManager telephonyManager = (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE);
    telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}

@Override
public void onResume() {
    super.onResume();
    setCountCalls();
}

public void setCountCalls() {
    Cursor cursor = getActivity().getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);
    if (cursor != null) {
        int count = cursor.getCount();
        ...
        cursor.close();
    }
}

MainActivity:

public void setCountCalls() {
    mainFragment.setCountCalls();
}

MissedCallCounter:

public class MissedCallCounter extends PhoneStateListener {
 private Context mContext;

 public MissedCallCounter(Context context) {
     mContext = context;
 }

 @Override
 public void onCallStateChanged(int state, String incomingNumber) {
     super.onCallStateChanged(state, incomingNumber);
     MainActivity mainFragment = (MainActivity) mContext;
     mainFragment.setCountCalls();
 }
}

问题:为什么光标从0开始?如果我得到1个未接来电,cursor.getCount() = 0,如果我有2个未接来电,那cursor.getCount() = 1等。但是如果我重新打开MainFragment,那1个未接来电== cursor.getCount() = 1等,因为它应该。但它只有在我再次重新打开MainFragment时才有效。为什么?如何解决?

0 个答案:

没有答案