在android中自定义SimpleCursorAdaper

时间:2013-06-10 11:29:30

标签: android android-listview simplecursoradapter android-adapter

我必须在我的应用中将数据从光标加载到listview。这是我的自定义适配器类。它工作正常。但我需要为listview的第一行显示单独的布局。列表中的第一行将具有额外的imageview。我怎么能这样做?请帮我。提前致谢。

public class CustomSCAdapter extends SimpleCursorAdapter {
Cursor c;
Context context;
Activity activity;
int layout;

public CustomSCAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags ) {
    super(context, layout, c, from, to, flags);
    this.c = c;
    this.context=context;
    this.activity=(Activity) context;
    this.layout = layout;

}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    final View view = LayoutInflater.from(context).inflate(this.layout, parent, false);
    return view;
}  

@Override
public void bindView(View view, Context context, Cursor cursor) {
       //here finding all my views in row from view object and binding data from cursor as per my requirement.
     }

}

编辑:实际上我正在为适配器类的构造函数传递null作为我在片段中使用加载器机制。

    getLoaderManager().initLoader(0, null, this);
    adapter = new CustomSCAdapter(getActivity(), R.layout.row_layout, null, from, to, 0);

1 个答案:

答案 0 :(得分:0)

最简单的方法是将第一项视为标题in this answer列表的标题。

更简单(但更丑陋)的方法是将这些条款放在newViewbindView方法中:

if (cursor.isFirst()) {
 // inflate a different layout containing the imageview or populate the imageview
} else {
 // add logic for other rows here
}