Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to write contactname and contactno row-wise in .csv file in Android?
    text
    copied!<p>I want to take contactname and corresponding contact no from mobile and want to write it in <code>.csv</code> file. Each row will contains contact name and contact number of each person for the entire contact list. How to write contact information in <code>.csv</code> file?</p> <p>I write the code for showing contactname column but it display only one contact means its override with existing one so what should be the trick for changing the row for new contact name.</p> <p>My code</p> <pre><code> 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() &gt; 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; </code></pre> <hr> <p>I use only name and "phoneno" is hardcoded. But in .csv file only one contact is shown</p> <pre><code> 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(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload