我有2个问题-
1.进度条(旋转器)根本不显示。好像从来没有过。
2.我是否有更好的方法来查找本地联系人列表并将其加载到arraylist中?我只需要填写联系人姓名和电话号码(如果一个联系人有多个电话号码,都应该出现)。对于大约300个联系人,需要3.5秒。不太慢。但是用户在等待时应看到进度条,以便知道进度条正在工作。

以下是我的主要活动-

public class myActivity extends Activity {

//declaring references for layout components - buttons, checkboxes, etc
ProgressBar pbLoadingSpinner;

ArrayList<Map<String, String>> nativeContactList = new ArrayList<Map<String, String>>();

@Override
    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //initializing references to layout components

    pbLoadingSpinner = (ProgressBar) findViewById(R.id.mySpinner);

    pbLoadingSpinner.setVisibility(View.VISIBLE);

    //
    //
    //

    populateContactList();
    pbLoadingSpinner.setVisibility(View.GONE);
}


private void populateContactList(){

    long tStart = System.currentTimeMillis();
    int contactCount = 0;

    Uri allContactsQueryUri = ContactsContract.Contacts.CONTENT_URI;
    String[] allContactsProjection = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.HAS_PHONE_NUMBER};
    String allContactsSelection = ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1";

    CursorLoader allContactsLoader = new CursorLoader(getApplicationContext(), allContactsQueryUri, allContactsProjection, allContactsSelection, null, null);
    Cursor allContacts = (Cursor) allContactsLoader.loadInBackground();

    nativeContactList.clear();

    while(allContacts.moveToNext()){

        String contactName = allContacts.getString(allContacts.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        String contactId = allContacts.getString(allContacts.getColumnIndex(ContactsContract.Contacts._ID));
        String hasPhoneNums = allContacts.getString(allContacts.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

        if(Integer.parseInt(hasPhoneNums)>0){

            Uri perContactUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
            String[] perContactProjection = new String[] {
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
                    ContactsContract.CommonDataKinds.Phone.NUMBER};
            String perContactSelection = ContactsContract.CommonDataKinds.Phone.CONTACT_ID+" = "+contactId;

            //Cursor allPhoneNums = getApplicationContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+" = "+contactId,null,null);

            CursorLoader perContactCursorLoader = new CursorLoader(getApplicationContext(),perContactUri,perContactProjection,perContactSelection,null,null);
            Cursor perContactCursor = (Cursor) perContactCursorLoader.loadInBackground();

            while(perContactCursor.moveToNext()){
                String contactPhoneNumber = perContactCursor.getString(perContactCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                Map<String,String> namePhone = new HashMap<String, String>();
                namePhone.put("Name", contactName);
                namePhone.put("Phone",contactPhoneNumber);
                nativeContactList.add(namePhone);
            }
            perContactCursor.close();
            contactCount++;
        }
    }

    Log.v("VERBOSE_PHONE_TAG", "Contact count =" + contactCount);

    allContacts.close();

    double elapsedSeconds = (System.currentTimeMillis() - tStart)/ 1000.0;

    Log.v("VERBOSE_PHONE_TAG", "Elapsed Time =" + elapsedSeconds);
}
}


我的xml布局看起来像-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/fragment_simple_call"
android:orientation="vertical">

<ProgressBar
    android:id="@+id/mySpinner"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true" />

</LinearLayout>

最佳答案

当您在populateContactList();中调用onCreate()时,您正在UI线程上进行整个数据库查询,从而阻止UI更新(包括ProgressBar旋转)。

您应该考虑使用AsyncTask在后台线程上运行populateContactList()(通过在AsyncTask的doInBackground()方法中调用它),然后隐藏进度条并使用onPostExecute()中的结果更新屏幕。

07-24 09:48
查看更多