0

我有一个列表片段,我用一个简单的光标适配器填充。但是,只有第一列绑定到布局。这是代码。

public class ContactList extends ListFragment

{
private ActivityDbAdapter mDbHelper;
private Long mRowId=Long.valueOf(1);
Activity mContext;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
{
    mContext=getActivity();
    mDbHelper=new ActivityDbAdapter(mContext);
    mDbHelper.open();
    Cursor activity = mDbHelper.fetchContacts(mRowId);
    if (activity.moveToFirst())
    {


    String[] from = new String[]{ActivityDbAdapter.COLUMN_NAME_CONTACT1 ,
            ActivityDbAdapter.COLUMN_NAME_CONTACT2,ActivityDbAdapter.COLUMN_NAME_CONTACT3,
            ActivityDbAdapter.COLUMN_NAME_CONTACT4, ActivityDbAdapter.COLUMN_NAME_CONTACT5};



    int[] to = new int[]{R.id.contacts};

    SimpleCursorAdapter contacts = 
        new SimpleCursorAdapter(mContext, R.layout.activity_contact_row, activity, from, to);

    setListAdapter(contacts);
    }

    return inflater.inflate(R.layout.activity_contact_list, container, false);

}

}

...所以这里只有 Contact1 出现在布局中。或者我首先拥有的任何专栏。据我了解,我不必使用 for 循环来正确绑定所有列,但您似乎这样做了?除非我在这里遗漏了什么。任何帮助,将不胜感激。

4

2 回答 2

0

将此方法用于您的ListFragment而不是旧方法。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
{
    mContext=getActivity();
    mDbHelper=new ActivityDbAdapter(mContext);

    mDbHelper.open();
    Cursor activity = mDbHelper.fetchContacts(mRowId);
    mDbHelper.close();

    String[] from = new String[]{ActivityDbAdapter.COLUMN_NAME_CONTACT1 ,
            ActivityDbAdapter.COLUMN_NAME_CONTACT2,ActivityDbAdapter.COLUMN_NAME_CONTACT3,
            ActivityDbAdapter.COLUMN_NAME_CONTACT4, ActivityDbAdapter.COLUMN_NAME_CONTACT5};


    int[] to = new int[]{R.id.contacts};

    SimpleCursorAdapter contacts = 
        new SimpleCursorAdapter(mContext, R.layout.activity_contact_row, activity, from, to);

    setListAdapter(contacts);

    return inflater.inflate(R.layout.activity_contact_list, container, false);

}
于 2013-01-14T23:12:54.177 回答
0

R.id.contacts您的数组中只有一项 ( ) to;因此,只有一个视图用于/显示数据。您还需要为其他视图添加 ID。

于 2013-01-15T00:50:27.637 回答