2013-08-13 49 views
17

Tôi xin lỗi nếu điều này có vẻ giống như câu hỏi một triệu lần ... nhưng tìm kiếm trên google cho điều này không cung cấp kết quả, chỉ là một loạt các hướng dẫn lỗi thời bằng cách sử dụng managedQuery và các giải pháp không được dùng nữa ...cách hiển thị danh bạ trong danh sách xem trong Android cho Android api 11+

Tôi đã xem qua android developer training for retrieving a contact list, nhưng hướng dẫn không đầy đủ và thậm chí tải xuống mã mẫu không giúp được gì vì mã mẫu dành cho thao tác danh sách liên hệ nâng cao hơn (tìm kiếm, v.v. Trong tất cả các trường hợp, không có lý do gì không phải là một giải pháp đơn giản cho điều này vì vậy tôi hy vọng ai đó có thể trả lời ở đây vì tôi chắc chắn điều này đã được thực hiện hàng triệu lần và tôi ure hàng chục nhà phát triển Android bắt đầu khác sẽ đánh giá cao điều này.

Tôi đã làm theo hướng dẫn theo sự hiểu biết tốt nhất của mình khi không có liên hệ nào hiển thị. Tôi nghĩ điều quan trọng nhất là TO_IDS là một mảng số nguyên trỏ tới android.R.id.text1. Tôi đang bối rối như thế nào đó là nghĩa vụ phải bằng cách nào đó kéo một loạt các tên liên lạc.

Ngoài ra, tôi đang bối rối tại sao một textview là cần thiết khi mục tiêu cuối cùng là để hiển thị một listview ... Trong hướng dẫn, chúng tôi có mContactsList đó là một danh sách xem ... Nhưng chúng tôi cư danh sách xem với một bộ chuyển đổi trỏ đến R.layout.contact_list_item chỉ là các bản xem văn bản được điền bởi TO_IDS, một mảng các số nguyên.

mContactsList = (ListView) getActivity().findViewById(R.layout.contact_list_view); 
mCursorAdapter = new SimpleCursorAdapter(
      getActivity(), 
      R.layout.contact_list_item, 
      null, 
      FROM_COLUMNS, TO_IDS, 
      0); 
mContactList.setAdapter(mCursorAdapter); 

Tôi đang làm gì sai và làm cách nào để hiển thị danh sách liên hệ trong chế độ xem danh sách?

EDIT: thêm vào trong mã của tôi:

trong lớp mảnh của tôi:

public class MyFragment extends Fragment implements 
    LoaderManager.LoaderCallbacks<Cursor>{ 

private static final String[] FROM_COLUMNS = {ContactsContract.Contacts.DISPLAY_NAME_PRIMARY }; 
private static final int[] TO_IDS = {android.R.id.text1}; 
ListView mContactList; 
private SimpleCursorAdapter mCursorAdapter; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 
    return inflater.inflate(R.layout.contact_list_view,container,false); 
} 

@Override 
public void onActivityCreated(Bundle savedInstanceState){ 
    super.onActivityCreated(savedInstanceState); 
    mContactsList = (ListView) getActivity().findViewById(R.layout.contact_list_view); 
    mCursorAdapter = new SimpleCursorAdapter(
      getActivity(), 
      R.layout.contact_list_item, 
      null, 
      FROM_COLUMNS, TO_IDS, 
      0); 
    mContactList.setAdapter(mCursorAdapter); 

} 

@Override 
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) { 
    return null; 
} 

@Override 
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) { 

} 

@Override 
public void onLoaderReset(Loader<Cursor> cursorLoader) { 

} 
} 

trong activity_main.xml tôi:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <fragment 
      android:id ="@+id/contactListFragment" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:name="preamble.myapp.MyFragment"/> 
</LinearLayout> 

trong xml contact_list_view tôi:

<?xml version="1.0" encoding="utf-8"?> 
<ListView xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@android:id/list" 
      android:layout_height="match_parent" 
      android:layout_width="match_parent"/> 

Trong liên hệ của tôi_ xml list_item

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@android:id/text1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"/> 

cuối cùng cho contact_list_layout xml:

nào để tôi đặt gì trong contact_list_layout.xml? Đây có phải chỉ là số trống <LinearLayout> không? Nó không rõ ràng trong hướng dẫn cách xử lý xml này. Nó nói rằng XML này là mảnh, nhưng nếu đó là đoạn, tại sao chúng ta định nghĩa một listview đã có trong contact_list_view.xml?

+0

'FROM_COLUMNS, TO_IDS, 'cột cơ sở dữ liệu bản đồ vào văn bản xem id (trong mỗi mục trong danh sách), xem [' SimpleCursorAdapter'] (http://developer.android.com/reference/android/widget/SimpleCursorAdapter .html # SimpleCursorAdapter% 28android.content.Context,% 20int,% 20android.database.Cursor,% 20java.lang.String [],% 20int [],% 20int% 29).Cái gì đó cần tải dữ liệu con trỏ để sử dụng dữ liệu đó và không có trong đoạn mã của bạn – zapl

+0

@zapl Tôi đã thêm một số mã của tôi để hiển thị cho bạn 'FROM_COLUMNS' và' TO_IDS' của mình ... Tôi nhận thấy rằng tôi không có 'contact_list_layout.xml' và tôi có' activity_main.xml' ... Bạn có thể cho tôi biết cách 'contact_list_layout.xml' được cấu trúc không? Bạn có thể xem lại các tệp xml của tôi không? Tôi có một cảm giác có một cái gì đó tôi chỉ mất tích và một khi tôi nhận ra rằng một phần ra nó sẽ làm việc. Các hướng dẫn không hiển thị như thế nào contact_list_layout.xml được viết nên có lẽ đó là nơi tôi đang phạm sai lầm? –

+0

@zapl Tôi nhận được một ngoại lệ con trỏ null tại 'mContactList.setAdapter (mCursorAdapter); ' –

Trả lời

20

Ví dụ rút gọn nhỏ để hiển thị tên liên hệ trong một ListView. Bên dưới Fragment mở rộng ListFragment có bố cục mặc định. Bạn không cần phải chỉ định của riêng bạn. Cách bố trí cho các mục danh sách cũng được lấy từ bố cục mặc định của Android (android.R.layout.simple_list_item_1) là một dòng văn bản đơn giản trên mỗi mục.

import android.content.Context; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.ContactsContract.Contacts; 
import android.support.v4.app.ListFragment; 
import android.support.v4.app.LoaderManager.LoaderCallbacks; 
import android.support.v4.content.CursorLoader; 
import android.support.v4.content.Loader; 
import android.support.v4.widget.CursorAdapter; 
import android.support.v4.widget.SimpleCursorAdapter; 

public class ContactListFragment extends ListFragment implements LoaderCallbacks<Cursor> { 

    private CursorAdapter mAdapter; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // create adapter once 
     Context context = getActivity(); 
     int layout = android.R.layout.simple_list_item_1; 
     Cursor c = null; // there is no cursor yet 
     int flags = 0; // no auto-requery! Loader requeries. 
     mAdapter = new SimpleCursorAdapter(context, layout, c, FROM, TO, flags); 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

     // each time we are started use our listadapter 
     setListAdapter(mAdapter); 
     // and tell loader manager to start loading 
     getLoaderManager().initLoader(0, null, this); 
    } 

    // columns requested from the database 
    private static final String[] PROJECTION = { 
     Contacts._ID, // _ID is always required 
     Contacts.DISPLAY_NAME_PRIMARY // that's what we want to display 
    }; 

    // and name should be displayed in the text1 textview in item layout 
    private static final String[] FROM = { Contacts.DISPLAY_NAME_PRIMARY }; 
    private static final int[] TO = { android.R.id.text1 }; 

    @Override 
    public Loader<Cursor> onCreateLoader(int id, Bundle args) { 

     // load from the "Contacts table" 
     Uri contentUri = Contacts.CONTENT_URI; 

     // no sub-selection, no sort order, simply every row 
     // projection says we want just the _id and the name column 
     return new CursorLoader(getActivity(), 
       contentUri, 
       PROJECTION, 
       null, 
       null, 
       null); 
    } 

    @Override 
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) { 
     // Once cursor is loaded, give it to adapter 
     mAdapter.swapCursor(data); 
    } 

    @Override 
    public void onLoaderReset(Loader<Cursor> loader) { 
     // on reset take any old cursor away 
     mAdapter.swapCursor(null); 
    } 
} 
+0

cảm ơn đã giúp rất nhiều, bạn có thể cho tôi biết làm thế nào chúng ta có thể hiển thị email và số cùng với tên của địa chỉ liên lạc trong cùng một cách trên. –

+0

@JagdeepSingh Không chắc chắn làm thế nào để có được tất cả các dữ liệu nhưng email là như xa như tôi có thể nhìn thấy có trong [ContactsContract.Data] (http://developer.android.com/reference/android/provider/ContactsContract.Data.html) bàn. Có thể đủ để thay đổi URI thành bảng đó và thêm cột email + số điện thoại vào phép chiếu. Ngoài ra, bạn sẽ cần bố trí mục danh sách tùy chỉnh với 3 trường văn bản cho bản đồ 'FROM'>' TO' hoặc mở rộng toàn bộ 'CursorAdapter' vì đơn giản không cho phép hiển thị 3 cột trong 1 trường văn bản. – zapl