Ter*_*how 17 java android listview simplecursoradapter
我很抱歉,如果这看起来像一百万次相同的问题...但谷歌搜索这个没有提供任何结果,只是一堆过时的教程使用managedQuery和其他已弃用的解决方案......
我通过Android开发人员培训检索联系人列表,但教程不完整,甚至下载示例代码也无济于事,因为示例代码用于更高级的联系人列表操作(搜索等)
在任何情况下,没有理由不应该有一个简单的解决方案,所以我希望有人可以在这里回答,因为我确信这已经做了一百万次,我相信其他几十个开始android开发人员我会很感激的.
没有任何联系人出现,我已经按照我的知识进行了教程.我认为最重要的是它TO_IDS是一个指向的整数数组android.R.id.text1.我很困惑如何以某种方式拉出一系列联系人姓名.
另外,我很困惑为什么当最终目标是显示列表视图时需要textview ...在教程中,我们有mContactsList这是一个列表视图...但是我们使用指向R.layout.contact_list_item哪个的适配器填充列表视图只是由TO_IDS填充的文本视图,这是一个整数数组.
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);
Run Code Online (Sandbox Code Playgroud)
我做错了什么,如何在列表视图中显示联系人列表?
编辑:添加我的代码:
在我的片段类中:
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) {
}
}
Run Code Online (Sandbox Code Playgroud)
在我的activity_main.xml中:
<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>
Run Code Online (Sandbox Code Playgroud)
在我的contact_list_view xml中:
<?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"/>
Run Code Online (Sandbox Code Playgroud)
在我的contact_list_item xml中
<?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"/>
Run Code Online (Sandbox Code Playgroud)
最后为contact_list_layout xml:
我该放什么contact_list_layout.xml?这只是空的<LinearLayout>吗?在教程中不清楚如何处理这个xml.它说这个XML是片段,但如果它是片段,我们为什么要定义一个listview已经存在的片段contact_list_view.xml?
zap*_*apl 20
用于在a中显示联系人姓名的小型剥离示例ListView.以下Fragment扩展ListFragment具有默认布局.您无需指定自己的.列表项的布局也取自Android的默认布局(android.R.layout.simple_list_item_1),这是每个项目的简单单行文本.
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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22593 次 |
| 最近记录: |