从数据库和异步任务获取空列表

时间:2016-12-16 07:29:39

标签: java android asynchronous arraylist android-sqlite

我有一个联系表帮助器,我有来自数据库的getAllContacts方法。 我在异步任务中调用此方法并返回联系人的数组列表。

但是当我调试时,我发现getAllContacts方法的conList返回0.

 public ArrayList<Contact> getAllContacts() {
    ArrayList<Contact> conList = new ArrayList<Contact>();

    String selectQuery = "SELECT * FROM " + CONTACT_TABLE;
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {

            Contact contact = new Contact();

            contact.setContactId(cursor.getString(0));
            contact.setmUserName(cursor.getString(1));
            contact.setmPass(cursor.getString(2));
            contact.setmMobileNo(cursor.getString(3));
            contact.setmEmailId(cursor.getString(4));
            contact.setmProfileImage(cursor.getString(5));
            contact.setmFullName(cursor.getString(6));
            contact.setmJobTitle(cursor.getString(7));
            contact.setmWorkAddress(cursor.getString(8));
            contact.setmWorkPhone(cursor.getString(9));
            contact.setmHomeAddress(cursor.getString(10));
            contact.setmCompany(cursor.getString(11));


            conList.add(contact);
        } while (cursor.moveToNext());
    }

    return conList;
}

异步任务:

    public class GetContacts extends AsyncTask<Void,Void,ArrayList<Contact>> {

        private Context mContext;
        private ContactTableHelper dbConnector;
        private GetContactsFromDbCallback getContactsFromDbCallback;

        public GetContacts(Context context,GetContactsFromDbCallback getContactsFromDbCallback)
        {

            this.mContext = context;
            this.getContactsFromDbCallback = getContactsFromDbCallback;
            dbConnector = new ContactTableHelper(context);

        }
    public interface GetContactsFromDbCallback{
        void doPostExecute(ArrayList<Contact> contacts,boolean b);
    }

        @Override
        public ArrayList<Contact> doInBackground(Void... params) {

            ArrayList<Contact> contacts = new ArrayList<>();

            contacts = dbConnector.getAllContacts();

            return contacts;
        }

        @Override
        public void onPostExecute(ArrayList<Contact> b) {
            if (b != null) {
                // set the adapter's Cursor

                getContactsFromDbCallback.doPostExecute(b,true);

                dbConnector.close();
            }
        }
}

回调:

公共类MainActivity扩展AppCompatActivity实现GetContacts.GetContactsFromDbCallback {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    contactList = new ArrayList<Contact>();

}


@Override
public void onResume()
{
    super.onResume();

    contactList.clear();

    if(!firstTimeLogin)
    {
        contactList.clear();

    //    contactDb = new ContactTableHelper(MainActivity.this);
    //    mDb = new UserTableHelper(MainActivity.this);

      // contactList = mDb.getAllContacts();

        new GetUser(MainActivity.this,MainActivity.this).execute(mUserId);

        new GetContacts(MainActivity.this,MainActivity.this).execute();

    }
    else {
        new GetUserAsyncTask(mUserId).execute(mUserId);
        new GetContactsAsyncTask(this, MainActivity.this, mUserId,MainActivity.this).execute();

        firstTimeLogin = false;

        SharedPreferences.Editor editor = getSharedPreferences("UserProfile",MODE_PRIVATE).edit();
        editor.putBoolean("login",firstTimeLogin);
        editor.commit();
    }

    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

    recyclerView.setHasFixedSize(true);
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(MainActivity.this);
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
    recyclerView.setItemAnimator(new DefaultItemAnimator());

    adapter = new ContactAdapter(MainActivity.this, contactList);
    recyclerView.setAdapter(adapter);

    recyclerView.addOnItemTouchListener(new RecyclerTouchListener(MainActivity.this, recyclerView, new ClickListener() {
        @Override
        public void onClick(View view, int position) {
            final Contact contact = contactList.get(position);

            // custom dialog

        }

        @Override
        public void onLongClick(View view, int position) {

        }
    }));

}

@Override
public void doPostExecute(ArrayList<Contact> contactArrayList,boolean y) {

   contactList = contactArrayList;

    adapter.notifyDataSetChanged();
}

}

这里出了什么问题?请帮忙......谢谢..

1 个答案:

答案 0 :(得分:3)

而不是这个

contactList = contactArrayList;

你需要这样做

contactList.clear();
contactList.addAll(contactArrayList);

您正在取消引用arraylist,因此适配器不再知道您尝试通知它的列表。