异步任务不加载数据
Async task not loading data
代码
public class ContactSettings extends Fragment {
private RecyclerView contactsList;
private List<ContactsHelper> contacts = new ArrayList<>();
private LinearLayoutManager linearLayoutManager;
private ContactsAdapter mAdapter;
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.settings_contact, container, false);
contactsList = (RecyclerView) rootView.findViewById(R.id.usersList);
//Add the data first
linearLayoutManager = new LinearLayoutManager(getActivity());
//and then create a object and pass the lis
mAdapter = new ContactsAdapter(contacts);
contactsList.setHasFixedSize(true);
contactsList.setLayoutManager(linearLayoutManager);
contactsList.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
COntacts runner = new COntacts();
return rootView;
}
@Override
public void onStart(){
super.onStart();
}
private class COntacts extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
ContentResolver contentResolver = getActivity().getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
if (cursor != null) {
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0) {
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Cursor phoneCursor = contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id},
null);
if (phoneCursor != null) {
if (phoneCursor.moveToNext()) {
String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contacts.add(new ContactsHelper(name, phoneNumber));
phoneCursor.close();
}
}
}
}
}
cursor.close();
}
return "Download failed";
}
@Override
protected void onPostExecute(String result) {
}
}
public void onClick(View view) {
}
}
它曾经在主线程中工作但是很慢所以我想使用 Asynctask
但我是新手而且在线教程不清楚所以有人可以告诉我这有什么问题和我怎样才能在后台获取联系人而不会变慢...................................... .........
正如我在代码中看到的,您没有在 doInBackground
中使用 urls
参数。并且您需要将 contacts
传递给 onPostExecute
,它在 UI Thread
中运行:
private class COntacts extends AsyncTask<Void, Void, List<ContactsHelper>> {
@Override
protected List<ContactsHelper> doInBackground(Void... urls) {
List<ContactsHelper> contacts = new ArrayList<>();
...
return contacts
}
@Override
protected void onPostExecute(List<ContactsHelper> result) {
contacts.addAll(result);
mAdapter.notifyDataSetChanged();
}
您需要在代码中执行 runner
对象:
runner.execute()
代码
public class ContactSettings extends Fragment {
private RecyclerView contactsList;
private List<ContactsHelper> contacts = new ArrayList<>();
private LinearLayoutManager linearLayoutManager;
private ContactsAdapter mAdapter;
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.settings_contact, container, false);
contactsList = (RecyclerView) rootView.findViewById(R.id.usersList);
//Add the data first
linearLayoutManager = new LinearLayoutManager(getActivity());
//and then create a object and pass the lis
mAdapter = new ContactsAdapter(contacts);
contactsList.setHasFixedSize(true);
contactsList.setLayoutManager(linearLayoutManager);
contactsList.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
COntacts runner = new COntacts();
return rootView;
}
@Override
public void onStart(){
super.onStart();
}
private class COntacts extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
ContentResolver contentResolver = getActivity().getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
if (cursor != null) {
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0) {
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Cursor phoneCursor = contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id},
null);
if (phoneCursor != null) {
if (phoneCursor.moveToNext()) {
String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contacts.add(new ContactsHelper(name, phoneNumber));
phoneCursor.close();
}
}
}
}
}
cursor.close();
}
return "Download failed";
}
@Override
protected void onPostExecute(String result) {
}
}
public void onClick(View view) {
}
}
它曾经在主线程中工作但是很慢所以我想使用 Asynctask
但我是新手而且在线教程不清楚所以有人可以告诉我这有什么问题和我怎样才能在后台获取联系人而不会变慢...................................... .........
正如我在代码中看到的,您没有在 doInBackground
中使用 urls
参数。并且您需要将 contacts
传递给 onPostExecute
,它在 UI Thread
中运行:
private class COntacts extends AsyncTask<Void, Void, List<ContactsHelper>> {
@Override
protected List<ContactsHelper> doInBackground(Void... urls) {
List<ContactsHelper> contacts = new ArrayList<>();
...
return contacts
}
@Override
protected void onPostExecute(List<ContactsHelper> result) {
contacts.addAll(result);
mAdapter.notifyDataSetChanged();
}
您需要在代码中执行 runner
对象:
runner.execute()