0

すべての人の電話番号を検索し、特殊文字を削除し、先頭から 0 を削除し (存在する場合)、接頭辞と接尾辞 (最後の 8 桁) を分割し、それらの中間に 9 を追加するアプリを作成したいと考えています。リストに表示し、個人情報を更新します。すべて順調に進んでいますが、リストにあるすべての人を更新しているわけではありません。プログラムは、Aさん、Bさん、Cさんを更新したと表示されていますが、確認するとBだけが更新されています。

    public void btList(View view) {
        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
        list.clear();
        while (phones.moveToNext())
        {
            String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            //String id = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
            int id = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
            int type = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
            phoneNumber = phoneNumber.replace(" ", "");
            phoneNumber = phoneNumber.replace("-", "");
            phoneNumber = phoneNumber.replace("+", "");
            phoneNumber = phoneNumber.replace("*", "");
            phoneNumber = phoneNumber.replace("#", "");
            String first = phoneNumber.substring(0, 1);
            boolean comeco;
            String renum;
            if(first.equals("0")){
                //se começar com 0
                renum = phoneNumber.substring(1);
                comeco = true;
            } else {
                //se não começar
                renum = phoneNumber;
                comeco = false;
            }
            long num = Long.parseLong(renum.toString());
            long prenum = num / 100000000;
            long posnum = num % 100000000;
            boolean cidade;
            if(String.valueOf(prenum).startsWith("55"))
                prenum = Long.parseLong(String.valueOf(prenum).substring(2));
            if(prenum != 0){
                if(String.valueOf(prenum).contains("18") || String.valueOf(prenum).contains("9090")){
                    cidade = true;
                }else{
                    cidade = false;
                }
            }else{
                cidade = true;
            }
            if((posnum / 10000000 == 9) || (posnum / 10000000 == 8) && cidade){
                String fnum = null;
                    if(comeco){
                            fnum = "0" + prenum + " 9 " + (posnum / 10000) + "-" + (posnum % 10000);
                            String texto = "Nome: " + name + " / Num: " + fnum;     
                            list.add(texto);


            } else {                
                    if(prenum != 0) {
                        fnum = "" + prenum + " 9 " + (posnum / 10000) + "-" + (posnum % 10000); 
                    } else {
                        fnum = "9 " + (posnum / 10000) + "-" + (posnum % 10000);  
                    }
                    String texto = "Nome: " + name + " / Num: " + fnum;     
                    list.add(texto);
                }
            try {
                updateContact(String.valueOf(id), fnum, String.valueOf(type));
            } catch (RemoteException e) {
                Log.i("UPDATE", "ERRO: " + e);
                e.printStackTrace();
            } catch (OperationApplicationException e) {
                Log.i("UPDATE", "ERRO: " + e);
                e.printStackTrace();
            }
            }
        }
        ListView lista = (ListView) findViewById(R.id.listView1);
        final StableArrayAdapter adapter = new StableArrayAdapter(this, android.R.layout.simple_list_item_1, list);
        lista.setAdapter(adapter);
        phones.close();
    }

    public void updateContact (String contactId, String newNumber, String phoneType) 
            throws RemoteException, OperationApplicationException{

                //ASSERT: @contactId alreay has a work phone number 
                ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 
                String selectPhone = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='"  + 
                                Phone.CONTENT_ITEM_TYPE + "'" + " AND " + Phone.TYPE + "=?";
                String[] phoneArgs = new String[]{contactId, phoneType}; 
                ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
                        .withSelection(selectPhone, phoneArgs)
                        .withValue(Phone.NUMBER, newNumber)
                        .build()); 
                this.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
            }

}
4

0 に答える 0