将列添加到ListView

时间:2015-06-22 16:02:28

标签: java android listview

我有ListView个用户的所有联系人,我想在每个联系人的姓名前面添加一个首字母的圈子,就像谷歌联系人应用中一样。我无法弄清楚如何将新列添加到ListView

这就是我在创建列表的活动中所拥有的:

    private CursorAdapter mAdapter;
    private String currentQuery;
    private static final String[] PROJECTION = {
            Contacts._ID,
            Contacts.DISPLAY_NAME_PRIMARY,
            Contacts.HAS_PHONE_NUMBER
    };
    private static final String[] FROM = { Contacts.DISPLAY_NAME_PRIMARY.substring(0,1), Contacts.DISPLAY_NAME_PRIMARY };
    private static final int[] TO = { R.id.first_initial, R.id.contact_text };
    private static final String SELECTION = "(" + Contacts.IN_VISIBLE_GROUP +
        " = 1) AND (" + Contacts.HAS_PHONE_NUMBER + " != 0 )";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAdapter = new SimpleCursorAdapter(getActivity(), R.layout.contacts_list_item, null, FROM, TO, 0);
    }

一切正常,然后我尝试添加第一个初始列,将Contacts.DISPLAY_NAME_PRIMARY.substring(0,1),添加到FROM并将R.id.first_initial,添加到TO。如何正确添加列?

1 个答案:

答案 0 :(得分:0)

您必须在适配器的getView上返回包含该组件的View。