2

我試圖從CALL-LOGS這是在我的聯繫人不得到所有號碼的清單,通話記錄沒有得到CACHED_NAME

我面臨的issue當任何人誰是我的聯繫人打電話給我。

光標「Ç」返回了號碼,因爲「name‘(CACHED_NAME)是null’。

但是當我打開call-log應用程序,然後再次打開我的application,該號碼將不會返回現在「」 名 「(CACHED_NAME)」 具有價值。

我能刷新從我的應用程序呼叫日誌數據?

我可以建立一個function,它可以檢查電話聯繫中是否存在號碼。

但我該如何使用這個函數與光標適配器。我試圖在bindview中使用這個函數,但仍然爲該數字創建了空白元素。我想使用CusrorAdapter

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    pview = inflater.inflate(R.layout.fragment_call, container, false); 

    ListView lvCall = (ListView) pview.findViewById(R.id.lvCall); 
    Uri uri = Uri.parse("content://call_log/calls"); 
    ContentResolver cr = getActivity().getContentResolver(); 
    **Cursor c = cr.query(uri, null, "name is null", null, "date DESC");** 
    adapter = new CursorAdapter(getActivity().getBaseContext(), c) { 

     @Override 
     public View newView(Context context, Cursor cursor, ViewGroup parent) { 
      LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      return li.inflate(R.layout.call_list, parent, false); 
     } 

     @Override 
     public void bindView(View view, Context context, Cursor cursor) { 

      **if (contactExists(cursor.getString(cursor.getColumnIndex("NUMBER")))) { 
       return; 
      }** 
      txt_call_number = (TextView) view.findViewById(R.id.txt_call_number); 
      txt_call_id = (TextView) view.findViewById(R.id.txt_call_id); 

      txt_call_number.setText(cursor.getString(cursor.getColumnIndex("NUMBER"))); 
      txt_call_id.setText(cursor.getString(cursor.getColumnIndex("_ID")).trim()); 

     } 

    }; 

    lvCall.setAdapter(adapter); 

    return pview; 
} 

感謝

附:以上示例代碼中可能有一些技術errormissing code,因爲我剛剛從我的application中提取了所需的code

回答

1

您無法直接使用您的應用程序訪問更新的通話記錄「CACHED_NAME」。

public boolean contactExists(Context context, String number) { 
      /// number is the phone number 
      Uri lookupUri = Uri.withAppendedPath(
     PhoneLookup.CONTENT_FILTER_URI, 
     Uri.encode(number)); 
     String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME }; 
     Cursor cur = context.getContentResolver().query(lookupUri,mPhoneNumberProjection, null, null, null); 
     try { 
      if (cur.moveToFirst()) { 
       return true; 
      } 
     } finally { 
       if (cur != null) 
       cur.close(); 
     } 
    return false; 
} 

現在,您可以檢入bindView函數。

 @Override 
     public void bindView(View view, Context context, Cursor cursor) { 

      txt_call_number = (TextView) view.findViewById(R.id.txt_call_number); 
      txt_call_id = (TextView) view.findViewById(R.id.txt_call_id); 
      if (contactExists(cursor.getString(cursor.getColumnIndex("NUMBER")))) { 
       txt_call_number.setVisibility(View.GONE); 
       txt_call_id.setVisibility(View.GONE); 
       view.setVisibility(View.GONE); 
       return; 
      }else{ 
       txt_call_number.setText(cursor.getString(cursor.getColumnIndex("NUMBER"))); 
       txt_call_id.setText(cursor.getString(cursor.getColumnIndex("_ID")).trim()); 
      } 
     } 

這可以幫助很多。

快樂編碼... :-)

+1

是的。這將工作。但我想以其他方式做....不是通過設置可視性。 – Maulik