我想從手機中獲取聯繫人姓名和相應的聯繫電話號碼,並且希望將其寫入.csv
文件中。每行將包含整個聯繫人列表中每個人的聯繫人姓名和聯繫人號碼。如何在.csv
文件中編寫聯繫人信息?如何在Android中的.csv文件中編寫contactname和contactno row-wise?
我寫的顯示contactname列的代碼,但它只顯示一個聯繫人意味着它與現有的重寫,所以更改新的聯繫人名稱的行應該是什麼竅門。
我的代碼
public class contactlist extends Activity {
static String name;
static int count;
static int countno;
static File file = null ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button filterbtn = (Button)findViewById(R.id.button1);
filterbtn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Uri u1 = Uri.fromFile(file);
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Person Details");
sendIntent.putExtra(Intent.EXTRA_STREAM, u1);
sendIntent.setType("text/html");
startActivity(sendIntent);
}
});
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
name = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String columnString = "\"PersonName\",\"phoneno\"";
String dataString = null;
我只用名字和 「PHONENO」 是固定的。但在.csv文件中只有一個聯繫人顯示
dataString = "\"" + name +"\",\"" + "phoneno" + "\"";
String combinedString = columnString + "\n" + dataString;
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()){
File dir = new File (root.getAbsolutePath() + "/PersonData");
dir.mkdirs();
file = new File(dir, "Data.csv");
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
out.write(combinedString.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
cur.close();
}
哪裏是代碼來獲取聯繫人姓名和電話號碼,並創建CSV? –
嗨pankaj我編輯了我的上面的代碼..實際上我沒有太多的想法如何做到這一點..我現在只提取聯繫人名稱進行測試..means顯示聯繫人名稱下面的「聯繫人名稱」列..i硬編碼「phoneno」字段,所以請你能告訴我如何做到這一點..如果你有任何其他的概念,您最歡迎感謝... –
我使用字符串combinedString = columnString +「\ n」+ dataString;用於在列字符串下方添加行,但它總是替換列名下面的現有行,以便如何添加第一行後其他..謝謝先生,如果你可以提供我任何代碼,我將非常感謝你... –