聯繫人列表使用ListView和SimpleCusrorAdapter與聯繫方式照片和濾波器的一端/搜索
我一直在尋找一個更簡單的解決方案和我的最終解決方案與丹尼爾在這裏提到的那個非常接近,所以我認爲我應該在這裏分享。我正在使用Fragment將設備聯繫人顯示爲其圖片名稱列表。結果與丹尼爾的非常相似,但只顯示名字。一旦你理解了代碼,更多的信息可以很容易地顯示出來。
在我的情況下,我使用PHOTO_URI從ContactsContract中獲取姓名和圖片,因此我不必按照Daniel的要求延長SimpleCursorAdapter
。
我的例子還包括過濾聯繫人作爲用戶類型的列表中SearchView
找到一個接觸
我有一個名爲FragmentContacts
和兩個佈局文件片段,第一主佈局frag_contacts.xml
和第二爲每個聯繫人行list_row_contact
。
frag_contacts.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@android:color/holo_blue_light"
android:padding="8dip">
<android.support.v7.widget.SearchView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:id="@+id/searchView"/>
</FrameLayout>
<LinearLayout
android:id="@+id/ll_contactList"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="9"
android:orientation="vertical" >
<ListView
android:id="@+id/lv_ContactList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#aaaaaa"
android:dividerHeight="1dp" >
</ListView>
</LinearLayout>
</LinearLayout>
list_row_contact.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants">
<FrameLayout
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center">
<ImageView
android:id="@+id/imgContact"
android:layout_width="35dip"
android:layout_height="35dip"
android:layout_gravity="center"
android:layout_margin="5dip" />
</FrameLayout>
<TextView
android:id="@+id/contact_name"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="6"
android:gravity="center_vertical"
android:textSize="18sp"
android:paddingLeft="10dip">
</TextView>
</LinearLayout>
FragmentContacts.java
public class FragmentContacts extends Fragment
implements LoaderManager.LoaderCallbacks<Cursor>{
private ListView lv_ContactList;
private SearchView searchView;
private SimpleCursorAdapter mCursorAdapter;
private static final String DISPLAY_NAME = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? Contacts.DISPLAY_NAME_PRIMARY : Contacts.DISPLAY_NAME;
private static final String[] contactsColumns = { Contacts._ID, Contacts.LOOKUP_KEY, DISPLAY_NAME, Contacts.PHOTO_URI };
private final String contactsFilter = "(" +Contacts.HAS_PHONE_NUMBER+ "='1') AND (" + Contacts.IN_VISIBLE_GROUP + "='1')";
private final String contactsSortOrder = DISPLAY_NAME + " COLLATE LOCALIZED ASC";
private final static String[] listDisplayColumns = { DISPLAY_NAME, Contacts.PHOTO_URI };
private final static int[] listDataViewIDs = { R.id.contact_name, R.id.imgContact };
String[] mSelectionArgs;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_contacts, null);
lv_ContactList = (ListView)view.findViewById(R.id.lv_ContactList);
searchView = (SearchView)view.findViewById(R.id.searchView);
return view;
}
@Override
public void onResume(){
super.onResume();
mCursorAdapter= new SimpleCursorAdapter(getActivity(), R.layout.list_row_contact, null, listDisplayColumns, listDataViewIDs, 0);
lv_ContactList.setAdapter(mCursorAdapter);
getLoaderManager().initLoader(0, null, this);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {return false;}
@Override
public boolean onQueryTextChange(String newText) {
if(newText.isEmpty()) mSelectionArgs = null;
else mSelectionArgs = new String[]{ "%"+newText.trim()+"%"};
getLoaderManager().restartLoader(0, null, FragmentContacts.this);
return false;
}
});
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
if(mSelectionArgs == null)
return new CursorLoader(getActivity(), Contacts.CONTENT_URI, contactsColumns, contactsFilter, null, contactsSortOrder);
else
return new CursorLoader(getActivity(), Contacts.CONTENT_URI, contactsColumns, contactsFilter + " AND (" + DISPLAY_NAME+" LIKE ?)", mSelectionArgs, contactsSortOrder);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mCursorAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mCursorAdapter.swapCursor(null);
}
}
什麼是使用SimpleCursorAdapter和列告訴它,如果我的點我將在getView中手動構建視圖???? – Radu
@Radu就像你指出的那樣,它確實勝過了目的。 – anthonymonori