简体   繁体   English

在SQLite中使用Asynctask和Listview

[英]Using Asynctask and listview with SQLite

I use AsyncTask to query an SQLite database - contacts. 我使用AsyncTask查询SQLite数据库-联系人。

When I run that code on the android emulator it works fine (probably because I have less contacts added there), but when I run it on my mobile (which it has approx 500 contacts) the app runs slow. 当我在android模拟器上运行该代码时,它工作正常(可能是因为我添加的联系人减少了),但是当我在移动设备上运行(它有大约500个联系人)时,应用运行缓慢。

Any help would be appreciated. 任何帮助,将不胜感激。 Thank you. 谢谢。

The Code is below. 代码如下。

      private class loadMoreListView extends AsyncTask<Void, Void, Void> {

            ArrayList<Contact> contactsx = new ArrayList<Contact>();
            @Override
            protected void onPreExecute() {
                pDialog = new ProgressDialog(MainActivity.this);
                pDialog.setMessage("Please wait..");
                pDialog.setIndeterminate(true);
                pDialog.setCancelable(false);
                pDialog.show();
                lv = (ListView) findViewById(R.id.blacklist);

            }

            @Override
            protected void onPostExecute(Void unused) {
                // closing progress dialog

                lv.setAdapter(adapter);

                pDialog.dismiss();
            }

            @Override
            protected Void doInBackground(Void... params) {
                // TODO Auto-generated method stub


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

                adapter = new EfficientAdapter(MainActivity.this, contactItems);

                DatabaseHandler db = new DatabaseHandler(MainActivity.this);
                String name = "";
                String id = "";
                String phoneNumber = "";
                // Cursor cursor = null;

                List<Integer> mycontactID;
                mycontactID = db.getAllIDs();

                Iterator value = mycontactID.iterator();

                while (value.hasNext()) {
                    int mg=(Integer)value.next();

                    ContentResolver cr = getContentResolver();
                    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                            null, null, null);
                //  Log.d("edw",Integer.toString(mg));
                    if (cur.getCount() > 0) {

                        while (cur.moveToNext()) {
                            id = cur.getString(cur
                                    .getColumnIndex(ContactsContract.Contacts._ID));
                            if (id.equals((mg)))
                                ;
                            {
                                name = cur
                                        .getString(cur
                                                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                                if (Integer
                                        .parseInt(cur.getString(cur
                                                .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {

                                    Cursor pCur = cr
                                            .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                                    null,
                                                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                                            + " = ?",
                                                    new String[] { id }, null);
                                    while (pCur.moveToNext()) {
                                        Log.d("number",
                                                pCur.getString(pCur
                                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
                                        // Do something with phones
                                        phoneNumber = pCur
                                                .getString(pCur
                                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                    }
                                    contactItemsTemp.add(new Contact(Integer
                                            .parseInt(id), name, phoneNumber));
                                    pCur.close();

                                    Log.d("Name", name);
                                }
                            }
                        }

                        cur.close();
                    }

                }
                // na kanw to temp contactItems

                List<Integer> messagesx;
                messagesx = db.getAllIDs();
                String namex = "";
                String phonex = "";
                for (Integer mg : messagesx) {
                    for (int i = 0; i < contactItemsTemp.size(); i++) {
                        if (contactItemsTemp.get(i).getID() == mg) {
                            namex = contactItemsTemp.get(i).getContactName();
                            phonex = contactItemsTemp.get(i).getPhoneNumber();
                        }
                    }

                    contactsx.add(new Contact(mg, namex, phonex));
                }

                adapter = new EfficientAdapter(MainActivity.this, contactsx);


                db.close();


                return null;
            }
        }



public static class EfficientAdapter extends ArrayAdapter<Contact>
            implements Filterable {
        private LayoutInflater mInflater;
        // private Bitmap mIcon1;
        private final Context context;
        private final ArrayList<Contact> values;

        public EfficientAdapter(Context context, ArrayList<Contact> values) {
            // Cache the LayoutInflate to avoid asking for a new one each time.
            super(context, R.layout.myobject, values);
            mInflater = LayoutInflater.from(context);
            this.context = context;
            this.values = values;
        }

        /**
         * Make a view to hold each row.
         * 
         * @see android.widget.ListAdapter#getView1(int, android.view.View,
         *      android.view.ViewGroup)
         */
        @Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {

            ViewHolder holder;

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.myobject, null);

                holder = new ViewHolder();
                holder.contact = (TextView) convertView
                        .findViewById(R.id.contact);
                holder.ph_num = (TextView) convertView
                        .findViewById(R.id.ph_num);

                convertView.setTag(holder);
            }

            else {
                // Get the ViewHolder back to get fast access to the TextView
                // and the ImageView.
                holder = (ViewHolder) convertView.getTag();
            }

            convertView.setOnLongClickListener(new View.OnLongClickListener() {
                private int pos = position;

                @Override
                public boolean onLongClick(View v) {
                    // TODO Auto-generated method stub

                    // prepare the alert box
                    AlertDialog.Builder alertbox = null;
                    alertbox = new AlertDialog.Builder(context);
                    // set the message to display
                    alertbox.setMessage("Are you sure you want to remove the contact from the blacklist?");
                    // add a neutral button to the alert box and assign a click
                    // listener
                    alertbox.setNeutralButton("OK",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    // TODO Auto-generated method stub
                                    Toast.makeText(context,
                                            "Ok button pressed " + pos,
                                            Toast.LENGTH_SHORT).show();

                                    DatabaseHandler db = new DatabaseHandler(
                                            context);
                                    db.deleteContact(values.get(position)
                                            .getID());
                                    db.close();
                                }
                            });

                    alertbox.setNegativeButton("Cancel",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    // TODO Auto-generated method stub
                                    Toast.makeText(context,
                                            "Cancel button pressed " + pos,
                                            Toast.LENGTH_SHORT).show();
                                }
                            });

                    alertbox.show();
                    return true;
                }
            });

            convertView.setOnClickListener(new OnClickListener() {
                private int pos = position;

                @Override
                public void onClick(View v) {
                    // Toast.makeText(context, "Click-" + pos,
                    // Toast.LENGTH_SHORT)
                    // .show();
                }
            });

            holder.contact.setText(getItem(position).getContactName());
            holder.ph_num.setText(getItem(position).getPhoneNumber());

            return convertView;

        }

        static class ViewHolder {
            TextView contact;
            TextView ph_num;

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return values.size();
        }

        @Override
        public Contact getItem(int position) {
            // TODO Auto-generated method stub
            return values.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public Filter getFilter() {
            // TODO Auto-generated method stub
            return null;
        }

        public void add(Contact con) {
            values.add(con);

            notifyDataSetChanged();
        }

        public void replace(int index, Contact con) {
            values.set(index, con);
            notifyDataSetChanged();
        }

        public void clear() {
            values.clear();
            notifyDataSetChanged();
        }

        public void remove(Contact con) {
            values.remove(con);
            notifyDataSetChanged();
        }

    }

Well, I am not sure about the exception, but it seems like you are trying to add contacts to your contactx arraylist before you initialize it. 好吧,我不确定该异常,但似乎您正在尝试在初始化它之前将联系人添加到contactx arraylist中。 You declare it as an instance variable, but do not initialize it until the very end of your doInBackground() method. 您将其声明为实例变量,但是直到doInBackground()方法结束时才对其进行初始化。 That would throw null pointer... 那会抛出空指针...

try putting the line.. 尝试把线..

contactx = new ArrayList<Contact>();

In your onPreExecute() method. 在您的onPreExecute()方法中。

lv = (ListView) findViewById(R.id.blacklist);

I would take a look at this line. 我来看看这条线。 You're trying to find your list view from a nested class. 您正在尝试从嵌套类中找到列表视图。 Although this will be run on the UI thread, AsyncTask is still not an activity. 尽管这将在UI线程上运行,但AsyncTask仍然不是活动。 I beleive your null point is becuase your listview reference is null as an instance of AsyncTask cannot retreive views. 我相信您的空点是因为您的listview引用为null,因为AsyncTask的实例无法获取视图。

  1. why is the adapter created twice (yet used only once) , and inside the runInBackground? 为什么在runInBackground内部创建两次适配器(至今仅使用一次)? should it be created only after the raw data was fetched , in the onPostExecute? 它应该仅在原始数据被获取后才在onPostExecute中创建吗?

  2. is it possible that you didn't implement the adapter nicely ? 您是否可能没有很好地实现适配器? have you watched the "the world of listView" video of google? 您是否看过Google的“ listView世界”视频? please show the adapter's code. 请显示适配器的代码。

  3. what is the exception of the crash? 崩溃有什么例外?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM