0

組み込みの電話連絡先から連絡先を読み取るコードがあり、アプリのリスト ビューにすべての連絡先が表示されます。ユーザーは複数の連絡先を選択して別のアクティビティに表示できます。このコードは Android API レベル 18 以降では正常に動作しますが、API 18 未満のバージョンではエラーが発生します。

連絡先ピッカー アクティビティのコードとそのアダプターを添付します。

エラーログキャット

private void getSelectedContacts() {
        // TODO Auto-generated method stub

        StringBuffer sb = new StringBuffer();
        dataBase=mHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        for (ContactObject bean : ContactsListClass.phoneList) {
            if (bean.isSelected()) {
                sb.append(bean.getName());
                sb.append(bean.getNumber());
                sb.append("1");

                values.put(DbHelper.KEY_FNAME,bean.getName());
                values.put(DbHelper.KEY_LNAME,bean.getNumber() );
                values.put(DbHelper.KEY_INVITE,"1" );
                dataBase.insert(DbHelper.TABLE_NAME, null, values);
            }
        }

        dataBase.close();

        String s = sb.toString().trim();

        if (TextUtils.isEmpty(s)) {
            Toast.makeText(context, "Select atleast one Contact",
                    Toast.LENGTH_SHORT).show();
        } else {

            s = s.substring(0, s.length() - 1);

            /**

            Toast.makeText(context, "Selected Contacts : " + s,
                    Toast.LENGTH_SHORT).show();
            **/
            Intent i = new Intent(Contacts_main.this, MainActivity.class);
            i.putExtra("NAME", name);
            i.putExtra("EVT_Name", event_name);
            i.putExtra("EVT_Date", event_date);
            startActivity(i);


        }



    }

    private void addContactsInList() {
        // TODO Auto-generated method stub

        Thread thread = new Thread() {
            @Override
            public void run() {

                showPB();

                try {

                    Cursor phones = getContentResolver().query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null, null, null, null);

                    try {
                        ContactsListClass.phoneList.clear();
                    } catch (Exception e) {

                    }

                    while (phones.moveToNext()) {
                        String phoneName = phones
                                .getString(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        String phoneNumber = phones
                                .getString(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        String phoneImage = phones
                                .getString(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));

                        //String pImage =  

                        ContactObject cp = new ContactObject();



                        cp.setName(phoneName);
                        cp.setNumber(phoneNumber);
                        cp.setImage(phoneImage);
                        //cp.setImage(getResources(R.drawable.prof_active));

                        ContactsListClass.phoneList.add(cp);

                    }
                    phones.close();

                    lv = new ListView(context);
                    lv.setDividerHeight(0);
                    lv.setDivider(null);

                    lv.setLayoutParams(new LayoutParams(
                            LayoutParams.MATCH_PARENT,
                            LayoutParams.MATCH_PARENT));

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            llContainer.addView(lv);
                        }
                    });

                    Collections.sort(ContactsListClass.phoneList,
                            new Comparator<ContactObject>() {
                                @Override
                                public int compare(ContactObject lhs,
                                        ContactObject rhs) {
                                    return lhs.getName().compareTo(
                                            rhs.getName());
                                }
                            });

                    objAdapter = new ContactsAdapter(Contacts_main.this,
                            ContactsListClass.phoneList);
                    lv.setAdapter(objAdapter); //ERROR SHOWING HERE
                    lv.setOnItemClickListener(new OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> parent,
                                View view, int position, long id) {

                            CheckBox chk = (CheckBox) view
                                    .findViewById(R.id.contactcheck);

                            ContactObject bean = ContactsListClass.phoneList
                                    .get(position);
                            if (bean.isSelected()) {
                                bean.setSelected(false);
                                chk.setChecked(false);
                            } else {
                                bean.setSelected(true);
                                chk.setChecked(true);
                            }

                        }
                    });

                } catch (Exception e) {

                 //   e.printStackTrace();
                    //Toast.makeText(context, "Crash",Toast.LENGTH_SHORT).show();


                }

                hidePB();

            }
        };
        thread.start();

    }

アダプタ クラス:

Context mContext;
    LayoutInflater inflater;
    private List<ContactObject> mainDataList = null;
    private List<ContactObject> mainInviteesList = null;
    private ArrayList<ContactObject> arraylist;
    private DbHelper mHelper;
    private SQLiteDatabase dataBase;


    public ContactsAdapter(Context context, List<ContactObject> mainDataList) {

        mContext = context;

        this.mainDataList = mainDataList;
        inflater = LayoutInflater.from(mContext);
        this.arraylist = new ArrayList<ContactObject>();
        this.arraylist.addAll(mainDataList);
        mHelper=new DbHelper(context);



    }

    static class ViewHolder {
        protected TextView name;
        protected TextView number;
        protected CheckBox check;
        protected ImageView image;
        protected EditText invitees;
    }

    @Override
    public int getCount() {
        return mainDataList.size();
    }

    @Override
    public ContactObject getItem(int position) {
        return mainDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.list_row, null);

            holder.name = (TextView) view.findViewById(R.id.contactname);
            holder.number = (TextView) view.findViewById(R.id.contactno);

            holder.check = (CheckBox) view.findViewById(R.id.contactcheck);

            holder.image = (ImageView) view.findViewById(R.id.contactimage);

            holder.invitees = (EditText) view.findViewById(R.id.editInvites);

            view.setTag(holder);
            view.setTag(R.id.contactname, holder.name);
            view.setTag(R.id.contactno, holder.number);
            view.setTag(R.id.contactcheck, holder.check);
            view.setTag(R.id.editInvites, holder.invitees);

            holder.check
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton vw,
                                boolean isChecked) {

                            int getPosition = (Integer) vw.getTag();
                            mainDataList.get(getPosition).setSelected(
                                    vw.isChecked());
                    /**        

                            dataBase=mHelper.getWritableDatabase();
                            ContentValues values=new ContentValues();
                            values.put(DbHelper.KEY_FNAME,mainDataList.get(getPosition).getName());
                            values.put(DbHelper.KEY_LNAME,mainDataList.get(getPosition).getNumber());
                            values.put(DbHelper.KEY_INVITE,"1" );
                            dataBase.insert(DbHelper.TABLE_NAME, null, values);


                    dataBase.close();

                    **/

                        }
                    });

           // holder.invitees.addTextChangedListener(watcher);


        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.check.setTag(position);
        //holder.invitees.setTag(position);

        holder.name.setText(mainDataList.get(position).getName());
        holder.number.setText(mainDataList.get(position).getNumber());




        if(getByteContactPhoto(mainDataList.get(position).getImage())==null){
            holder.image.setImageResource(R.drawable.prof_active);
        }else{
            holder.image.setImageBitmap(getByteContactPhoto(mainDataList.get(position).getImage()));
        }



        holder.check.setChecked(mainDataList.get(position).isSelected());

        return view;
    }

    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        mainDataList.clear();
        if (charText.length() == 0) {
            mainDataList.addAll(arraylist);
        } else {
            for (ContactObject wp : arraylist) {
                if (wp.getName().toLowerCase(Locale.getDefault())
                        .contains(charText)) {
                    mainDataList.add(wp);
                }
            }
        }
        notifyDataSetChanged();
    }

    public Bitmap getByteContactPhoto(String contactId) {
        Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, Long.parseLong(contactId));
        Uri photoUri = Uri.withAppendedPath(contactUri, Contacts.Photo.CONTENT_DIRECTORY);
        Cursor cursor = mContext.getContentResolver().query(photoUri,
                        new String[] {Contacts.Photo.DATA15}, null, null, null);
        if (cursor == null) {
            return null;
        }
        try {
            if (cursor.moveToFirst()) {
                byte[] data = cursor.getBlob(0);
                if (data != null) {
                    return BitmapFactory.decodeStream( new ByteArrayInputStream(data));
                }
            }
        } finally {
            cursor.close();
        }

        return null;
        }

}
4

0 に答える 0