2010-10-24 50 views
0

我对Java编程和Android开发都比较陌生,所以我的学习曲线目前非常陡峭。我似乎被困在一些我无法找到像过去那样的体面事例的东西上。Android getContentResolver()。查询错误

我写了一个函数,根据我在这里找到的示例和文档&那里得到我所有的手机的联系人。我似乎无法解决的问题是这样的。下面的代码工作得很好;

private void fillData() { 
    // This goes and gets all the contacts 
    // TODO: Find a way to filter this only on contacts that have mobile numbers 
    cursor = getContentResolver().query(Contacts.CONTENT_URI, null, null, null, null); 

    final ArrayList<String> contacts = new ArrayList<String>(); 

    // Let's set our local variable to a reference to our listview control 
    // in the view. 
    lvContacts = (ListView) findViewById(R.id.lvContacts); 

    while(cursor.moveToNext()) { 
     contacts.add(cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME))); 
    } 

    // Make the array adapter for the listview. 
    final ArrayAdapter<String> aa; 
    aa = new ArrayAdapter<String>(this, 
            android.R.layout.simple_list_item_multiple_choice, 
            contacts); 

    // Let's sort our resulting data alphabetically. 
    aa.sort(new Comparator<String>() { 
     public int compare(String object1, String object2) { 
      return object1.compareTo(object2); 
     }; 
    }); 

    // Give the list of contacts over to the list view now. 
    lvContacts.setAdapter(aa); 
} 

我想通过过滤掉所有没有手机号码条目的联系人来改变查询语句。我尝试过这样的事情;

 cursor = getContentResolver().query(Contacts.CONTENT_URI, 
     new String[] {Data._ID, Phone.TYPE, Phone.LABEL}, 
     null, null, null); 

但是,当我这样做时,它会引发NullPointer异常错误。这有什么问题? 我从android的网站上的例子得到了这一点,但他们有一个where子句,不适用于我的需求,所以我改变了什么地方为null。这是什么搞砸了吗?

谢谢。

+0

问题似乎是它不喜欢新的String [] {Data._ID,Phone.TYPE,Phone.LABEL}参数中的“data2”列引用。 Phone.TYPE转换为“data2”,错误消息说明SQLiteQueryBuilder.computeProjection对该列不太满意。不知道如何解决这个问题。 – Skittles 2010-10-24 19:29:42

回答

2

好吧,看来我已经自己解决了我的问题。 (将头发拉出头顶变得光滑)

看来Phone.TYPE的使用确实是问题所在。 Phone.TYPE是一个常量而不是数据列。

事实证明,完美工作的代码是这样的;

private void fillData() { 
    // This goes and gets all the contacts that have mobile numbers 

    final ArrayList<String> contacts = new ArrayList<String>(); 

    // Let's set our local variable to a reference to our listview control 
    // in the view. 
    lvContacts = (ListView) findViewById(R.id.lvContacts); 

    String[] proj_2 = new String[] {Data._ID, Phone.DISPLAY_NAME, CommonDataKinds.Phone.TYPE}; 
    phnCursor = managedQuery(Phone.CONTENT_URI, proj_2, null, null, null); 
    while(phnCursor.moveToNext()) { 
     if (phnCursor.getInt(2) == Phone.TYPE_MOBILE) { 
      String name = phnCursor.getString(1); 
      contacts.add(name); 
     } 
    } 

    // Make the array adapter for the listview. 
    final ArrayAdapter<String> aa; 
    aa = new ArrayAdapter<String>(this, 
            android.R.layout.simple_list_item_multiple_choice, 
            contacts); 

    // Let's sort our resulting data alphabetically. 
    aa.sort(new Comparator<String>() { 
     public int compare(String object1, String object2) { 
      return object1.compareTo(object2); 
     }; 
    }); 

    // Give the list of contacts over to the list view now. 
    lvContacts.setAdapter(aa); 
} 

我很感谢帮助,但不幸的是,必须说彻底的疯狂和研究最终回报。希望这可以帮助别人。