我首先获取联系人列表,然后我尝试在自定义ListView中添加相同的内容,但它会减慢整个过程。 如何让它更快。
我首先遍历每个联系人,然后在ListView viw UI线程中附加相同的内容。 什么一定是空闲的方式。
public class PhoneBookListView extends ListActivity {
List<String> contactsList = new ArrayList<String>();
ArrayAdapter<String> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_phone_book_list_view);
// storing string resources into Array
String[] adobe_products = new String[]{"A1","BBB","Poly","KIOL","liooo"};
for(String str:adobe_products){
contactsList.add(str);
}
// arrayAdapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, getContacts);
// Binding resources Array to ListAdapter
arrayAdapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, contactsList);
this.setListAdapter(arrayAdapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
}
});
Thread thread = new Thread()
{
@Override
public void run() {
try {
getContacts();
} catch (Exception e) {
e.printStackTrace();
}
}
};
thread.start();
}
private void addItems(String strContact){
contactsList.add(strContact);
arrayAdapter.notifyDataSetChanged();
}
private String[] getContacts() {
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null,
null, null,
null);
//adds listview so I can get data from it
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
final String displayName = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
null, null, null);
/** read names **/
while (phones.moveToNext()) {
final String phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
addItems(displayName+" - "+phoneNumber);
}
});
}
phones.close();
}
return null;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.phone_book_list_view, menu);
return true;
}
}