Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将Asynctask和listview与SQLite一起使用_Android_Sqlite_Android Listview_Android Asynctask_Nullpointerexception - Fatal编程技术网

Android 将Asynctask和listview与SQLite一起使用

Android 将Asynctask和listview与SQLite一起使用,android,sqlite,android-listview,android-asynctask,nullpointerexception,Android,Sqlite,Android Listview,Android Asynctask,Nullpointerexception,我使用AsyncTask查询SQLite数据库-联系人 当我在android emulator上运行该代码时,它运行良好(可能是因为我在那里添加的联系人较少),但当我在手机上运行该代码时(手机上有大约500个联系人),应用程序运行缓慢 任何帮助都将不胜感激。多谢各位 代码如下 private class loadMoreListView extends AsyncTask<Void, Void, Void> { ArrayList<Cont

我使用AsyncTask查询SQLite数据库-联系人

当我在android emulator上运行该代码时,它运行良好(可能是因为我在那里添加的联系人较少),但当我在手机上运行该代码时(手机上有大约500个联系人),应用程序运行缓慢

任何帮助都将不胜感激。多谢各位

代码如下

      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();
        }

    }
私有类loadMoreListView扩展了异步任务{
ArrayList contactsx=新的ArrayList();
@凌驾
受保护的void onPreExecute(){
pDialog=新建进度对话框(MainActivity.this);
pDialog.setMessage(“请稍候…”);
pDialog.setUndeterminate(真);
pDialog.setCancelable(假);
pDialog.show();
lv=(ListView)findViewById(R.id.blacklist);
}
@凌驾
受保护的void onPostExecute(未使用的void){
//关闭进度对话框
低压设置适配器(适配器);
pDialog.disclose();
}
@凌驾
受保护的Void doInBackground(Void…参数){
//TODO自动生成的方法存根
ArrayList contactItemsTemp=新的ArrayList();
适配器=新的效率适配器(MainActivity.this,contactItems);
DatabaseHandler db=新的DatabaseHandler(MainActivity.this);
字符串名称=”;
字符串id=“”;
字符串phoneNumber=“”;
//游标=空;
列出接触菌;
mycontactID=db.getAllIDs();
迭代器值=mycontactID.Iterator();
while(value.hasNext()){
int mg=(整型)值。next();
ContentResolver cr=getContentResolver();
Cursor cur=cr.query(Contacts contract.Contacts.CONTENT\u URI,null,
空,空,空);
//Log.d(“edw”,Integer.toString(mg));
如果(cur.getCount()>0){
while(cur.moveToNext()){
id=cur.getString(cur
.getColumnIndex(Contacts contract.Contacts._ID));
如果(id等于((mg)))
;
{
name=cur
.getString(cur)
.getColumnIndex(Contacts contract.Contacts.DISPLAY_NAME));
if(整数
.parseInt(cur.getString(cur
.getColumnIndex(Contacts contract.Contacts.HAS_PHONE_NUMBER))>0){
光标pCur=cr
.query(contacts contract.commondatatypes.Phone.CONTENT\u URI、,
无效的
Contacts contract.CommonDataTypes.Phone.CONTACT\u ID
+ " = ?",
新字符串[]{id},null);
while(pCur.moveToNext()){
Log.d(“编号”,
获取字符串(pCur
.getColumnIndex(ContactsContract.CommonDataTypes.Phone.NUMBER));
//用手机做点什么
电话号码=pCur
.getString(pCur)
.getColumnIndex(ContactsContract.CommonDataTypes.Phone.NUMBER));
}
contactItemsTemp.add(新联系人(整数
.parseInt(id)、姓名、电话号码);
pCur.close();
Log.d(“名称”,名称);
}
}
}
cur.close();
}
}
//不适用于临时联系人项目
列表消息sx;
messagesx=db.getAllIDs();
字符串namex=“”;
字符串phonex=“”;
for(整数mg:messagesx){
对于(int i=0;icontactx = new ArrayList<Contact>();
lv = (ListView) findViewById(R.id.blacklist);