0

I'm trying to get all the contacts from my SQLite database.

Everything is working fine, I just want to make it asynchronous and not run in the main thread, to not influence the UI.

public List<contacts> getAllcontacts() {
    List<contacts> contactsl = new LinkedList<contacts>();

String query = "SELECT  * FROM contacts WHERE show is not 'NOTSIGNEDUP'"
        +" ORDER BY name COLLATE NOCASE;";

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);

contacts contact = null;
if (cursor.moveToFirst()) {
    do {
        contact = new contacts();

        contact.setName(cursor.getString(1));
        contact.setNumero(cursor.getString(3));
        contact.setProfil(cursor.getString(2));
        contact.setShow(cursor.getString(5));
        contact.setBlocked(cursor.getString(4));
        contact.setObjectid(cursor.getString(6));
        contactsl.add(contact);
    } while (cursor.moveToNext());
}

return contactsl;
}

I'm calling this function from my activity :

  final sql s = sql.getInstance(getContext());


   if (ContactsList != null) {
       ContactsList.clear();
       ContactsList.addAll(list);
       ContactsList.addAll(s.getAllcontacts_());
       cAdapter.notifyDataSetChanged();
   }

Is there any way to make s.getAllcontacts() runs asyn

I made my Fragment like this :

public class ContactsFragment extends Fragment implements LoaderManager.LoaderCallbacks<List<contacts>> {
    private RecyclerView mRecyclerView;
    private LinearLayoutManager mLayoutManager;
    private ContactsAdapter cAdapter;
    private List<contacts> ContactsList;



    public ContactsFragment() {
        // Required empty public constructor
    }

    public void set(List<contacts> list) {
        final sql s = sql.getInstance(getContext());


       if (ContactsList != null) {
           ContactsList.clear();
           ContactsList.addAll(list);
           ContactsList.addAll(s.getAllcontacts_());
           cAdapter.notifyDataSetChanged();
       }



    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_blank, container, false);
        return view;

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);



        View view = getView();
        if(view != null) {
            mRecyclerView = (RecyclerView) view.findViewById(R.id.contacts_recycler);
            mRecyclerView.setHasFixedSize(true);
            mLayoutManager = new LinearLayoutManager(view.getContext());
            mRecyclerView.setLayoutManager(mLayoutManager);
            final sql s = sql.getInstance(view.getContext());
            ContactsList = new ArrayList<contacts>();
            cAdapter = new ContactsAdapter(ContactsList, mRecyclerView);
            mRecyclerView.setAdapter(cAdapter);
            getLoaderManager().initLoader(0, null, this);

        }
    }


    @Override
    public android.support.v4.content.Loader<List<contacts>> onCreateLoader(int id, Bundle args) {
        return new AppListLoader(this.getContext());
    }

    @Override
    public void onLoadFinished(android.support.v4.content.Loader<List<contacts>> loader, List<contacts> data) {
        ContactsList.addAll(data);
        cAdapter.notifyDataSetChanged();
    }

    @Override
    public void onLoaderReset(android.support.v4.content.Loader<List<contacts>> loader) {

    }


    public static class AppListLoader extends AsyncTaskLoader<List<contacts>> {
        final sql s = sql.getInstance(getContext());
        public AppListLoader(Context context) {
            super(context);
        }

        @Override
        public List<contacts> loadInBackground() {
            return s.getAllcontacts();

        }

    }


}
1
  • 2
    Wrap it in an AsyncTask or a Thread, the same basic ways that you make anything else asynchronous. Commented Dec 20, 2015 at 20:18

1 Answer 1

1

in addition to what @CommonsWare suggests, you could also use give to the AsyncTaskLoader a try. You could define

public static class AppListLoader extends AsyncTaskLoader<List<Contact>> {

and move your querying logic in loadInBackground().

Your Activity/Fragment will make then use of the LoaderManager. It will implement LoaderManager.LoaderCallbacks<List<Contact>> and onCreateLoader will return a new instance of your AsyncTaskLoader. The List<Contact> will be delivered as part of onLoadFinished

Sign up to request clarification or add additional context in comments.

5 Comments

OnCreatLoader is returnin null. It has to return new AppListLoader(getActivity());
And how to call this function ??
Override onActivityCreated and call getLoaderManager().initLoader(0, null, this); . the method will be callee automatically by the framework
Nothing happens when calling, I updated my post wih the final Fragment code
I think that the only thing missing is the implementation of deliverResult in the AsyncTaskLoader

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.