如何合并StickyListHeadersListView和Cursor

时间:2015-06-17 23:05:14

标签: android database android-listview adapter

您好我正在尝试创建一个ListView来实现StickyListHeadersListView库并从Cursor获取数据。我在我的StickyListHeaderAdapter中遇到了困难,我试图使用光标而不是string[]。我正在寻找一些指南代码,但我看到的所有例子都使用字符串[]。

我的问题是

  1. getCount()中我说我需要一个整数而不是android.database.Cursor还有其他方法来解决这个问题吗?
  2. 无法解决方法setText(android.database.Cursor),我知道我必须在这里使用String[]但是另外一种方法是什么?
  3. 这是我的代码

    public class StickyHeaderAdapter extends BaseAdapter implements StickyListHeadersAdapter {
    
    private Cursor information, header;
    private LayoutInflater inflater;
    DBHelper dbHelper;
    
    public StickyHeaderAdapter(Context context, Cursor countries, Cursor header) {
        inflater = LayoutInflater.from(context);
        countries = dbHelper.getInformationItems();
        header = dbHelper.getInformationHeader();
    }
    
    @Override
    public int getCount() {
        return information;
    }
    
    @Override
    public Object getItem(int position) {
        return information;
    }
    
    @Override
    public long getItemId(int position) {
        return position;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
    
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.text_list_item_layout, parent, false);
            holder.text = (TextView) convertView.findViewById(R.id.list_title);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
    
        holder.text.setText(information );
        return convertView;
    }
    
    @Override
    public View getHeaderView(int position, View convertView, ViewGroup parent) {
        HeaderViewHolder holder;
        if (convertView == null) {
            holder = new HeaderViewHolder();
            convertView = inflater.inflate(R.layout.header, parent, false);
            holder.text = (TextView) convertView.findViewById(R.id.header);
            convertView.setTag(holder);
        } else {
            holder = (HeaderViewHolder) convertView.getTag();
        }
    
    
        holder.text.setText(header);
        return convertView;
    }
    
    @Override
    public long getHeaderId(int position) {
        return position;
    }
    
    class HeaderViewHolder {
        TextView text;
    }
    
    class ViewHolder {
        TextView text;
     }
    
    }
    

    非常感谢您的帮助!

0 个答案:

没有答案