制作.csv联系人列表的问题:android

时间:2011-07-28 11:22:42

标签: android

我想制作“联系人姓名”的.csv文件和相应的“联系人号码”。但是,当我运行下面的代码给出错误发生时请。看下面我的代码,并建议我在哪里错了。 意思是我有两列,一个是名字,第二个是联系号,值应该在coloumn下面显示。请参阅我的代码。我是android和java的新手

public class contactlist extends Activity {
    static String name;
    static int count;
    static int countno;
    File file = null ;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            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));

                            //error on below line
                            String phoneno = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                            cur.close();

                            String columnString =   "\"PersonName\",\"phoneno\"";

                            String dataString = null;

                            dataString = "\"" + name +"\",\"" + phoneno + "\"";    
                            Log.i("saurabh",dataString);

                            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();
                                    }
                            }

                    }
            }
    }
}

错误是......

 07-28 16:09:56.758: ERROR/AndroidRuntime(27597): Caused by: java.lang.IllegalStateException: get field slot from row 0 col -1 failed
  07-28 16:09:56.758: ERROR/AndroidRuntime(27597):     at android.database.CursorWindow.getString_native(Native Method)
  07-28 16:09:56.758: ERROR/AndroidRuntime(27597):     at android.database.CursorWindow.getString(CursorWindow.java:329)
   07-28 16:09:56.758: ERROR/AndroidRuntime(27597):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)
        07-28 16:09:56.758: ERROR/AndroidRuntime(27597):     at android.database.CursorWrapper.getString(CursorWrapper.java:135)
         07-28 16:09:56.758: ERROR/AndroidRuntime(27597):     at contactlist.pkg.contactlist.onCreate(contactlist.java:83)
        07-28 16:09:56.758: ERROR/AndroidRuntime(27597):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
     07-28 16:09:56.758: ERROR/AndroidRuntime(27597):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

2 个答案:

答案 0 :(得分:0)

使用JavaCSV库,它可以读取和写入CSV。其余的逻辑进入你的代码。

答案 1 :(得分:0)

public class ContactlistActivity extends Activity {

    String name;
    File file = null;

    String[] names = { ContactsContract.CommonDataKinds.Phone._ID,
        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
        ContactsContract.CommonDataKinds.Phone.NUMBER };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {
            File folder = new File(Environment.getExternalStorageDirectory()+ "/Application");
            boolean var = false;
            if (!folder.exists())
                var = folder.mkdir();

            System.out.println("" + var);

            String filename = folder.toString() + "/Contact.csv";
            FileWriter fw = new FileWriter(filename);

            fw.append("Name");
            fw.append(',');
            fw.append("Number");
            fw.append(',');
            fw.append('\n');

            Cursor cur = getContentResolver().query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,names,null,null,
                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+ " ASC");
            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 phoneno = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    String dataString = null;
                    dataString = "\"" + name + "\",\"" + phoneno + "\"";

                    fw.append("" + name);
                    fw.append(',');
                    fw.append("" + phoneno);
                    fw.append(',');
                    fw.append('\n');        
                }
                cur.close();
                fw.close();
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
}