2

我正在尝试在 android.here 中获取已保存的联系人并将其写入 csv 文件。

 if (cur.getCount() > 0) {
      writer = new CSVWriter(new FileWriter(filename));
        List<String[]> data = new ArrayList<String[]>();
        data.add(new String[] {"Name", "Number","Email","Note","PoBox","street","City","State","Postal Code","Country","AddressType","Instant Messanger","Org Name","Org Title"});
     while (cur.moveToNext()) {
         String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
          name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
             if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {

                 // get the phone number
             Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                                    new String[]{id}, null);
             while (pCur.moveToNext()) {
                   String phonetemp = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                   if(pCur.getCount()>1)
                   {
                       phone=phone+phonetemp+"^";
                   }
                   else
                   {
                       phone=phone+phonetemp;
                   }
                  }
             pCur.close();


             // get email and type
            Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null);

             while (emailCur.moveToNext()) {
                 // This would allow you get several email addresses
                     // if the email addresses were stored in an array
                 if(emailCur.getCount()>0)
                 {
                     if(emailCur.getCount()>1)
                     {
                       String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                       String emailType = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
                       Emails=Emails + email + " $ " + emailType+"^";
                     }else
                     {
                         String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                         String emailType = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
                         Emails=  email + " $ " + emailType;
                     }
                 }
                 else
                 {
                     Emails=null;
                 }
            }
             emailCur.close();

             // Get note.......
             String noteWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
             String[] noteWhereParams = new String[]{id,ContactsContract.CommonDataKinds.Note.NOTE};
             Cursor noteCur = cr.query(ContactsContract.Data.CONTENT_URI, null, noteWhere, noteWhereParams, null);
             if (noteCur.moveToFirst()) {String note = noteCur.getString(noteCur.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
               Note= note;
             }
            noteCur.close();

             //Get Post Box Address....
            // String addrPoBoxWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
            // String[] addrPoBoxWhereParams = new String[]{id, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
             Cursor addrPoBoxCur = cr.query(ContactsContract.Data.CONTENT_URI,null,CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id},null);
             while(addrPoBoxCur.moveToNext()) {
                String poBoxtemp = addrPoBoxCur.getString(addrPoBoxCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
                if(poBoxtemp!=null)
                {
                    if(addrPoBoxCur.getCount()>1)
                    {
                        poBox=poBox+poBoxtemp+"^";  
                    }
                    else {
                        poBox=poBox+poBoxtemp;
                    }

                }

                 }
             addrPoBoxCur.close(); 

             //Get Street Address....
             //String addrStreetWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
             String[] addrStreetWhereParams = new String[]{ContactsContract.CommonDataKinds.StructuredPostal.STREET};
             Cursor addrStreetCur = cr.query(ContactsContract.Data.CONTENT_URI,addrStreetWhereParams,CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id},null);
             while(addrStreetCur.moveToNext()) {
                  String streettemp = addrStreetCur.getString(addrStreetCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
                  if(streettemp!=null)
                  {
                      if(addrStreetCur.getCount()>1)
                      {
                          street=street+streettemp+"^";   
                      }
                      else
                      {
                          street=street+streettemp;
                      }
                  }
             }
             addrStreetCur.close();

             //Get City Address....
            // String addrCityWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
            // String[] addrCityWhereParams = new String[]{id, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
             Cursor addrCityCur = cr.query(ContactsContract.Data.CONTENT_URI,null,CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id},null);
             while(addrCityCur.moveToNext()) {
                  String citytemp = addrCityCur.getString(addrCityCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
                  if(citytemp!=null)
                  city=city+citytemp+"^";
             }
             addrCityCur.close();

             //Get State Address....
           //  String addrStateWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
          //   String[] addrStateWhereParams = new String[]{id, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
             Cursor addrStateCur = cr.query(ContactsContract.Data.CONTENT_URI,null,CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id},null);
             while(addrStateCur.moveToNext()) {
                               String statetemp = addrStateCur.getString(addrStateCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
                  if(statetemp!=null)
                  {
                      if(addrStateCur.getCount()>1)
                      {
                          state=state+statetemp+"^";      
                      }
                      else
                      {
                          state=state+statetemp;
                      }
                  }

              }
             addrStateCur.close();

             //Get PostalCode Address....
            // String addrPostalCodeWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
             //String[] addrPostalCodeWhereParams = new String[]{id, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
             Cursor addrPostalCodeCur = cr.query(ContactsContract.Data.CONTENT_URI,null,CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id},null);
             while(addrPostalCodeCur.moveToNext()) {
                   String postalCodetemp = addrPostalCodeCur.getString(addrPostalCodeCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.PHONETIC_NAME));
                   if(postalCodetemp!=null)
                   {
                       if(addrPostalCodeCur.getCount()>1)
                       {
                           postalCode=postalCode+postalCodetemp+"^";       
                       }
                       else
                       {
                           postalCode=postalCode+postalCodetemp;
                       }
                   }

             }
             addrPostalCodeCur.close();

             //Get Country Address....
           //  String addrCountryWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
          //   String[] addrCountryWhereParams = new String[]{id, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
             Cursor addrCountryCur = cr.query(ContactsContract.Data.CONTENT_URI,null,CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id},null);
             while(addrCountryCur.moveToNext()) {
                String countrytemp = addrCountryCur.getString(addrCountryCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
                   if(countrytemp!=null)
                   {
                       if(addrCountryCur.getCount()>1)
                       {
                           country=country+countrytemp+"^";    
                       }
                       else
                       {
                           country=country+countrytemp;
                       }
                   }

               }
             addrCountryCur.close();

             //Get AddressType ....
           //  String addrAddressTypeWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
            // String[] addrAddressTypeWhereParams = new String[]{id, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
             Cursor addrAddressTypeCur = cr.query(ContactsContract.Data.CONTENT_URI,null,CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id},null);
             while(addrAddressTypeCur.moveToNext()) {
                   String AddressTypetemp = addrAddressTypeCur.getString(addrAddressTypeCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
                   if(AddressTypetemp !=null)
                   {
                       if(addrAddressTypeCur.getCount()>1)
                       {
                           AddressType=AddressType+AddressTypetemp +"^";       
                       }
                       else
                       {
                           AddressType=AddressType+AddressTypetemp;
                       }
                   }

                                     // Do something with these...
             }
             addrAddressTypeCur.close();

             // Get Instant Messenger.........
            String imWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";String[] imWhereParams = new String[]{id,ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE};
             Cursor imCur = cr.query(ContactsContract.Data.CONTENT_URI, null, imWhere, imWhereParams, null);
              while (imCur.moveToNext()) {
                 if(imCur.getCount()>0)
                 {
                     String imName = imCur.getString(
                              imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA));
                     String imType;
                     imType = imCur.getString(
                              imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE));
                     IM=IM + imName + " $ " + imType+"^";
                 }
                 else
                 {
                     String imName = imCur.getString(
                              imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA));
                     String imType;
                     imType = imCur.getString(
                              imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE));
                     IM=IM + imName + " $ " + imType;
                 }

             }
             imCur.close();

             // Get Organizations.........
             String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
             String[] orgWhereParams = new String[]{id,
                 ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE};
             Cursor orgCur = cr.query(ContactsContract.Data.CONTENT_URI,
                         null, orgWhere, orgWhereParams, null);
             if (orgCur.moveToFirst()) {
                  orgName = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DATA));

                  title = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE));
             }
             orgCur.close();
         }   data.add(new String[] {name, phone,Emails,Note,poBox,street,city,state,postalCode,country,AddressType,IM,orgName,title});
     }
     writer.writeAll(data);
     writer.close();

这里的问题是当面对联系的街道详细信息时,它正在获取街道详细信息 + 联系电话

4

0 回答 0