2011-07-28 59 views
0

我想製作「聯繫人姓名」的.csv文件和相應的「聯繫電話」。但是當我運行下面的代碼給出錯誤發生pls。看到我的代碼下面,並建議我在哪裏我錯了。 意味着我有兩列之一是名字,第二是聯繫號碼和價值應該是showh下coloumn.so請參閱我的代碼。 我是Android和java新手關於製作.csv聯繫人列表的問題:android

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) 
+0

您的代碼不可讀,請正確格式化! –

+0

我爲他做了。另外@ram ji,你不必使用多個try ... catch塊。 –

+0

先生你可以給我想法如何使csv文件的聯繫信息。所以不矛盾不會發生意味着如果我保存名稱在一個數組和contactno在另一個數組中,並且如果任何聯繫人詳細信息沒有contactno但只有聯繫人名稱然後不一致應該..所以請幫助我先生... –

回答

0

使用JavaCSV庫,它可以讀取和寫入的CSV。其餘的邏輯進入你的代碼。

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

一注:如果光標爲空('getCount ()== 0'),fileWriter'fw'不會被關閉。 –