2014-09-24 2 views
1

이 연락처 앱을 개발 중입니다. 지금까지 ListView 마녀에는 연락처 이름과 전화 번호가 있습니다. 연락처를 클릭하면 새로운 활동이 시작되고 연락처 이름과 전화 번호가 표시됩니다.안드로이드 데이터 in ListView

내가 원하는 것은 ListView에 연락처 이름 만 표시하고 목록에서 연락처를 클릭하면 활동이 시작되고 이름과 번호를 모두 볼 수 있다는 것입니다.

어쩌면 내가 ListView에서 정보의 일부를 숨길 수 있다고 생각했지만 그다지 좋은 점이 발견되지 않았습니다.

아무도 어떤 제안이 있습니까?

미리 감사드립니다. 당신은 당신이이 연락처로 커서를 가지고 후에는

<uses-permission android:name="android.permission.READ_CONTACTS" /> 
public Loader<Cursor> onCreateLoader(int loaderID, Bundle bundle){ 
     Uri uri    = ContactsContract.Contacts.CONTENT_URI; 
     String[] projection = new String[] { ContactsContract.Contacts._ID, 
            ContactsContract.Contacts.DISPLAY_NAME}; 
     String sortOrder  = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"; 

     // Returns a new CursorLoader 
     return new CursorLoader(
        getActivity(), // Parent activity context 
        uri,  // Table to query 
        projection,  // Projection to return 
        null,   // No selection clause 
        null,   // No selection arguments 
        sortOrder  // Sort by name 
    ); 

} 

을 선언해야 매니페스트에서

: 모든

+0

당신이 묻고있는 것이 확실하지 않습니다 - "연락처를 클릭하면 새로운 활동이 시작되고 연락처 이름과 전화 번호가 표시됩니다." "목록에서 연락처를 클릭하면 활동이 시작되고 이름과 전화 번호를 모두 볼 수 있습니다"와 매우 유사합니다. –

+0

오케이 죄송합니다. 조금 명확하지 않았습니다. 프로그램 시작시 맨 처음 무엇입니까? 연락처 목록을 보여주고 연락처 이름 만 표시하려고합니다. 하지만 지금은 이름과 전화 번호를 모두 보여줍니다. – user1032336

답변

3

우선, 단지 연락처 이름과 ID를 조회 그걸 패스 CursorAdapter

private final static String[] FROM_COLUMNS = { 
     Build.VERSION.SDK_INT 
       >= Build.VERSION_CODES.HONEYCOMB ? 
       Contacts.DISPLAY_NAME_PRIMARY : 
       Contacts.DISPLAY_NAME 
}; 
private final static int[] TO_IDS = { 
     android.R.id.text1 
}; 


public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    ... 
    // Gets the ListView from the View list of the parent activity 
    mContactsList = 
     (ListView) getActivity().findViewById(R.layout.contact_list_view); 
    // Gets a CursorAdapter 
    mCursorAdapter = new SimpleCursorAdapter(
      getActivity(), 
      R.layout.contact_list_item, 
      null, 
      FROM_COLUMNS, TO_IDS, 
      0); 
    // Sets the adapter for the ListView 
    mContactsList.setAdapter(mCursorAdapter); 

    // Prepare the loader. Either re-connect with an existing one, 
    // or start a new one. 
    getLoaderManager().initLoader(0, null, this); 

} 

public void onLoadFinished(Loader<Cursor> loader, Cursor data) { 
    // Swap the new cursor in. (The framework will take care of closing the 
    // old cursor once we return.) 
    mAdapter.swapCursor(data); 

    // The list should now be shown. 
    if (isResumed()) { 
     setListShown(true); 
    } else { 
     setListShownNoAnimation(true); 
    } 
}