为什么函数没有按顺序执行?

时间:2016-11-25 19:24:15

标签: java android performance firebase firebase-realtime-database

这是问题......我有三个不同的功能,即syncFirebaseContacts(); getAllContacts(); and compareContactUpdateList();。这些函数的执行顺序非常重要,因为第一个函数的输出变为另一个函数的输入。正如我所说的那样,我按照所描述的顺序在我的onCreate()方法中放置了这三个函数。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        datasource = new AdoreDataSource(this);
        datasource.open();
        syncFirebaseContacts(); 
        getAllContacts();
        compareContactUpdateList();
       .......

但是在调试时我发现虽然指针首先指向syncFirebaseContacts() ,但先执行getAllContact()的代码,然后执行compareContactUpdateList(),然后执行syncFirebaseContacts()代码已执行。

private void getAllContacts() {
    String contactNumber;
    ContentResolver cr = this.getContentResolver(); //Activity/Application android.content.Context
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

    if (cursor.moveToFirst()) {
        do {
            String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

            if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
                while (pCur.moveToNext()) {
                    contactNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    //All Mapping Stuff to be done here..
                    contactNumber = contactNumber.replaceAll("\\s+","");
                    contactNumber = contactNumber.trim();
                    lst2.add(contactNumber);
                    break;
                }
                pCur.close();
            }
        } while (cursor.moveToNext());
    }
}


private void syncFirebaseContacts() {
    Firebase userLocationRef = new Firebase(Constants.FIREBASE_URL_USERS);
    userLocationRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot dsp : dataSnapshot.getChildren()){
                datasource.createContact(String.valueOf(dsp.getKey()));
                lst1.add(String.valueOf(dsp.getKey()));
                Log.e("Hello", String.valueOf(dsp.getKey()));
            }
        }
        @Override
        public void onCancelled(FirebaseError firebaseError) {
            Log.e("Could not process", "Sorry");
        }
    });

}


private void compareContactsUpdateList() {
    ArrayList<String> commonOfAll = new ArrayList<String>(lst1);
    commonOfAll.retainAll(lst2);
    ContentResolver cr = this.getContentResolver(); //Activity/Application android.content.Context
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    final ArrayList<ContactsList> contacts = new ArrayList<ContactsList>();
    //List<SyncContacts> syncContacts = datasource.getAllContacts();
    if (cursor.moveToFirst()) {

        do {
            String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
                while (pCur.moveToNext()) {
                    String nameOfContact = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    String contactNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    contactNumber = contactNumber.replaceAll("\\s+","");
                    contactNumber = contactNumber.trim();
                    Log.d("Name ", nameOfContact + contactNumber);
                    for(int j =0; j < commonOfAll.size(); j++){
                        if(contactNumber.equals(commonOfAll)){
                            Log.d("Name ", nameOfContact);
                            contacts.add(new ContactsList(contactNumber, nameOfContact, R.mipmap.ic_launcher));
                        }
                    }
                    break;
                }
                pCur.close();
            }

        } while (cursor.moveToNext());

    }

    final ContactsAdapterTwo contactsAdapterTwo = new ContactsAdapterTwo(this, contacts);
    final GridView gridView = (GridView) findViewById(R.id.grid);

    gridView.post(new Runnable() {
        public void run() {
            gridView.setAdapter(contactsAdapterTwo);
        }
    });

}

我的问题是我要比较compareContactsUpdateList() ArrayList<String> commonOfAll = new ArrayList<String>(lst1); commonOfAll.retainAll(lst2);中的两个列表,因为我发现lst1总是空的,因为syncFirebaseContacts() 's代码最后被执行

我的applogies,如果我的问题长度太长或我的问题是微不足道的。但这对我来说是一个很大的问题因为我是新手编码:-)请帮助我......无论如何都要提前完成...

0 个答案:

没有答案