2011-05-15 80 views
20

我正在嘗試使用Android腳本和Python開發一個簡單的應用程序。我如何獲得聯繫人姓名和他/她的號碼

現在,我有一個電話號碼,我想找出哪個聯繫人有該號碼。我可以做一個contactsGet()並搜索數字,但是很多程序都使用該功能,我認爲這有一個更簡單的方法。

還有一個問題存在同樣的問題,但是Java有沒有Python的等價物? Search contact by phone number

有沒有簡單的方法來實現這一目標?

任何示例代碼表示讚賞。

編輯,幾天後沒有回答,我決定改變一點問題:什麼是最好的方式來搜索一個數字的列表,我與contactsGet()?

回答

1

這是抽象層,一個常見的問題。該圖層不會抽象出您想要使用的特定功能,工具或案例。然而,在這種情況下,似乎並非所有的希望都失去了。看來,Android腳本API是一個開源項目。爲什麼不貢獻一個能夠爲項目提供這種能力的補丁?

我可能會在未來某個時候提供這樣的補丁,但是如果它對你很重要,那麼你可以在做之前做同樣的事情,並且在路上!

+0

這是唯一的答案,其中包括我的問題可能的解決方案:)。似乎沒有其他辦法。謝謝 :)。 – utdemir 2011-05-24 16:15:07

1
package com.slk.example.CursorActivity; 
import android.app.ListActivity; 
import android.content.Context; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.provider.Contacts.Phones; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.CursorAdapter; 
import android.widget.TextView; 

public class CursorActivity extends ListActivity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Cursor contactsCursor = this.managedQuery(Phones.CONTENT_URI, null, null, null, null); 
     this.setListAdapter(new MyContactsAdapter(this,contactsCursor)); 
    } 

    private class MyContactsAdapter extends CursorAdapter{ 
     private Cursor mCursor; 
     private Context mContext; 
     private final LayoutInflater mInflater; 

     public MyContactsAdapter(Context context, Cursor cursor) { 
      super(context, cursor, true); 
      mInflater = LayoutInflater.from(context); 
      mContext = context; 
     } 

     @Override 
     public void bindView(View view, Context context, Cursor cursor) { 
      TextView t = (TextView) view.findViewById(R.id.txtName); 
      t.setText(cursor.getString(cursor.getColumnIndex(Phones.NAME))); 

      t = (TextView) view.findViewById(R.id.txtDisplayName); 
      t.setText(cursor.getString(cursor.getColumnIndex(Phones.DISPLAY_NAME))); 

      t = (TextView) view.findViewById(R.id.txtPhone); 
      t.setText(cursor.getString(cursor.getColumnIndex(Phones.NUMBER))); 
     } 

     @Override 
     public View newView(Context context, Cursor cursor, ViewGroup parent) { 
      final View view = mInflater.inflate(R.layout.main, parent, false); 
      return view; 
     } 
    } 
} 
+1

問題是它的Python等價物。 – utdemir 2011-05-23 14:27:08

0

你可能想看看ContactsContract data table。像這樣的東西進行查詢:

Cursor c = getContentResolver().query(Data.CONTENT_URI, 
     new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL}, 
         Data.RAW_CONTACT_ID + "=?" + " AND " 
         + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'", 
         new String[] {String.valueOf(rawContactId) 
     }, null) 
+1

android-scripting API不提供像這樣的東西。這是它提供的:http://code.google.com/p/android-scripting/wiki/ApiReference – utdemir 2011-05-23 19:04:47

相關問題