gpt4 book ai didi

Android ProgressBar 不显示并且 native 联系人查找速度很慢

转载 作者:行者123 更新时间:2023-11-30 02:30:36 25 4
gpt4 key购买 nike

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

以下是我的主要 Activity -

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() 中的结果更新屏幕.

关于Android ProgressBar 不显示并且 native 联系人查找速度很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27387834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com