27

我想以 Date 为 SectionHeader 显示 ListView。

我所拥有的: 我正在使用自定义 SimpleCursorAdapter 从 sqlite 数据库中显示 ListView。

我的自定义 SimpleCursorAdapter 是:

public class DomainAdapter extends SimpleCursorAdapter{
private Cursor dataCursor;

private LayoutInflater mInflater;

public DomainAdapter(Context context, int layout, Cursor dataCursor, String[] from,
        int[] to) {
    super(context, layout, dataCursor, from, to);
        this.dataCursor = dataCursor;
        mInflater = LayoutInflater.from(context);
}


public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.todo_row, null);

        holder = new ViewHolder();
        holder.text1 = (TextView) convertView.findViewById(R.id.label);//Task Title
        holder.text2 = (TextView) convertView.findViewById(R.id.label2);//Task Date
        holder.img =   (ImageView) convertView.findViewById(R.id.task_icon);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    dataCursor.moveToPosition(position);
    int title = dataCursor.getColumnIndex("title"); 
    String task_title = dataCursor.getString(title);

    int title_date = dataCursor.getColumnIndex("day"); 
    String task_day = dataCursor.getString(title_date);

    int description_index = dataCursor.getColumnIndex("priority"); 
    int priority = dataCursor.getInt(description_index);

    holder.text1.setText(task_title);
    holder.text2.setText(task_day);

    if(priority==1) holder.img.setImageResource(R.drawable.redbutton);
    else if(priority==2) holder.img.setImageResource(R.drawable.bluebutton);
    else if(priority==3)holder.img.setImageResource(R.drawable.greenbutton);
    else holder.img.setImageResource(R.drawable.redbuttonchecked);

    return convertView;
}

static class ViewHolder {
    TextView text1;
    TextView text2;
    ImageView img;
}
}

到目前为止的谷歌搜索结果:

合并适配器

杰夫·夏基

惊人的列表视图

所以问题

问题:我想显示带有日期的列表视图作为节标题。当然日期值来自 sqlite 数据库。

谁能指导我如何完成这项任务。

或向我提供与之相关的示例代码或确切(类似)代码。

根据格雷厄姆博拉德的回答编辑(这工作正常。但它是一个快速修复。

public class DomainAdapter extends SimpleCursorAdapter{
    private Cursor dataCursor;
    private LayoutInflater mInflater;

    public DomainAdapter(Context context, int layout, Cursor dataCursor, String[] from,
            int[] to) {
        super(context, layout, dataCursor, from, to);
            this.dataCursor = dataCursor;
            mInflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        if (convertView == null) 
        {
            convertView = mInflater.inflate(R.layout.tasks_row, null);
            holder = new ViewHolder();
            holder.text1 = (TextView) convertView.findViewById(R.id.label);//Task Title
            holder.text2 = (TextView) convertView.findViewById(R.id.label2);//Task Date
            holder.img =   (ImageView) convertView.findViewById(R.id.taskImage);

            holder.sec_hr=(TextView) convertView.findViewById(R.id.sec_header);

            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        dataCursor.moveToPosition(position);
        int title = dataCursor.getColumnIndex("title"); 
        String task_title = dataCursor.getString(title);

        int title_date = dataCursor.getColumnIndex("due_date"); 
        String task_day = dataCursor.getString(title_date);

        int description_index = dataCursor.getColumnIndex("priority"); 
        int priority = dataCursor.getInt(description_index);

        String prevDate = null;

        if (dataCursor.getPosition() > 0 && dataCursor.moveToPrevious()) {
            prevDate = dataCursor.getString(title_date);
            dataCursor.moveToNext();
        }


        if(task_day.equals(prevDate))
        {
            holder.sec_hr.setVisibility(View.GONE);
        }
        else
        {
            holder.sec_hr.setText(task_day);
            holder.sec_hr.setVisibility(View.VISIBLE);
        }

        holder.text1.setText(task_title);
        holder.text2.setText(task_day);

        if(priority==1) holder.img.setImageResource(R.drawable.redbutton);
        else if(priority==2) holder.img.setImageResource(R.drawable.bluebutton);
        else if(priority==3)holder.img.setImageResource(R.drawable.greenbutton);
        else holder.img.setImageResource(R.drawable.redbuttonchecked);

        return convertView;
    }

    static class ViewHolder {
        TextView text1;
        TextView text2;
        TextView sec_hr;
        ImageView img;
    }
}

根据 CommonsWare 的回答编辑

public class DomainAdapter extends SimpleCursorAdapter{
        private Cursor dataCursor;
        private TodoDbAdapter adapter;

        private LayoutInflater mInflater;
        boolean header;
      String last_day;
      public DomainAdapter(Context context, int layout, Cursor dataCursor, String[] from,
        int[] to) {
        super(context, layout, dataCursor, from, to);
        this.dataCursor = dataCursor;
        mInflater = LayoutInflater.from(context);
        header=true;
        adapter=new TodoDbAdapter(context);
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder = null;
    TitleHolder title_holder = null;

    if(getItemViewType(position)==1)
    {
        //convertView= mInflater.inflate(R.layout.todo_row, parent, false);

        if (convertView == null) 
        {
            convertView = mInflater.inflate(R.layout.todo_row, null);

            holder = new ViewHolder();
            holder.text1 = (TextView) convertView.findViewById(R.id.label);//Task Title
            holder.text2 = (TextView) convertView.findViewById(R.id.label2);//Task Date
            holder.img =   (ImageView) convertView.findViewById(R.id.task_icon);

            convertView.setTag(holder);
        }
        else 
        {
            holder = (ViewHolder) convertView.getTag();
        }

        dataCursor.moveToPosition(position);
        int title = dataCursor.getColumnIndex("title"); 
        String task_title = dataCursor.getString(title);

        int title_date = dataCursor.getColumnIndex("day"); 
        String task_day = dataCursor.getString(title_date);

        int description_index = dataCursor.getColumnIndex("priority"); 
        int priority = dataCursor.getInt(description_index);

        holder.text1.setText(task_title);
        holder.text2.setText(task_day);

        if(priority==1) holder.img.setImageResource(R.drawable.redbutton);
        else if(priority==2) holder.img.setImageResource(R.drawable.bluebutton);
        else if(priority==3)holder.img.setImageResource(R.drawable.greenbutton);
        else holder.img.setImageResource(R.drawable.redbuttonchecked);
    }
    else
    {

        if (convertView == null) 
        {
            convertView = mInflater.inflate(R.layout.section_header, null);

            title_holder = new TitleHolder();
            title_holder.datee = (TextView) convertView.findViewById(R.id.sec_header);//Task Title

            convertView.setTag(title_holder);
        }
        else 
        {
            title_holder = (TitleHolder) convertView.getTag();
        }

        dataCursor.moveToPosition(position);

        int title_date = dataCursor.getColumnIndex("day"); 
        String task_day = dataCursor.getString(title_date);

        title_holder.datee.setText(task_day);
    }

    return convertView;
}

static class ViewHolder {
    TextView text1;
    TextView text2;
    ImageView img;
}

 static class TitleHolder{
    TextView datee;
}


@Override
public int getCount() {
    return dataCursor.getCount()+1; //just for testing i took no. of headers=1
}


@Override
public int getViewTypeCount() {
    return 2;
}

@Override
public int getItemViewType(int position) {

    dataCursor.moveToPosition(position);
    **Long id=dataCursor.getLong(position);**
    Cursor date=adapter.fetchTodo(id);
    int title_date = date.getColumnIndex("day"); 
        String task_day = date.getString(title_date);
        Log.i("tag",task_day);

    if(last_day.equals(task_day))
        return 1;//Display Actual Row
    else
    {
        last_day=task_day;//Displaying Header
        return 0;
    }

}

/*
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {

    final View view;

    if(getItemViewType(cursor.getPosition())==1)
        view= mInflater.inflate(R.layout.todo_row, parent, false);
    else
        view=mInflater.inflate(R.layout.section_header,parent, false);

    return view;

}

@Override
public void bindView(View convertView, Context context, Cursor cursor) {
    long id = cursor.getPosition();

}*/
}

我在行收到空指针异常Cursor date=adapter.fetchTodo(id);: 似乎光标没有获取任何数据。

4

3 回答 3

55

到目前为止,最简单的方法是在每个项目中嵌入日期标题视图。然后,您需要做的bindView就是将前一行的日期与这一行的日期进行比较,如果相同则隐藏日期。像这样的东西:

    String thisDate = cursor.getString(dateIndex);
    String prevDate = null;

    // get previous item's date, for comparison
    if (cursor.getPosition() > 0 && cursor.moveToPrevious()) {
        prevDate = cursor.getString(dateIndex);
        cursor.moveToNext();
    }

    // enable section heading if it's the first one, or 
    // different from the previous one
    if (prevDate == null || !prevDate.equals(thisDate)) {
        dateSectionHeaderView.setVisibility(View.VISIBLE);
    } else {
        dateSectionHeaderView.setVisibility(View.GONE);
    }
于 2011-06-08T09:29:02.470 回答
4

谁能指导我如何完成这项任务。

伴随着巨大的痛苦。

您将需要创建自己的CursorAdapter(或可能SimpleCursorAdapter)的子类。在该子类中,您将需要计算标题行数并进行调整getCount()以匹配。您需要getViewTypeCount()返回正确的号码。您将需要确定这些标题行的位置并调整getItemViewType(), newView(), 和bindView()处理您的标题行,调整position详细信息行的值以考虑该行前面的标题数量,以便您获得正确的Cursor位置跟...共事。可能还需要进行其他调整,但这些是相当确定的。

如果没有人超过我,我可能会在明年写其中一个,一旦我的项目需要它。

于 2011-06-07T11:38:46.103 回答
2

实现起来非常简单:

public class ChatAdapter extends NoteBaseAdapter {

private Cursor mCursor;

/**
 * List of notes to showToast
 */

public int getCount() {
    int count = 0;
    if (mCursor != null)
        count = mCursor.getCount();
    return count;
}

public Comment getItem(int position) {
    mCursor.moveToPosition(position);
    Comment comment = Comment.fromCursor(mCursor);
    return comment;
}

public long getItemId(int position) {
    return 0;
}

public View getView(final int position, View convertView, ViewGroup parent) {

    final Comment comment = getItem(position);

    CommentCell cellView = null;
    if (convertView == null) {
        cellView = new CommentCell(parent.getContext());
        cellView.setup(elementsColor, backgroundColor);
    } else
        cellView = (CommentCell) convertView;

    cellView.setComment(comment);

    boolean showHeader = false;
    String currentDate = null;

    if (position > 0 && position < getCount()) {

        int previousPosition = position - 1;
        Comment previousComment = getItem(previousPosition);

        currentDate = comment.headerDate();
        String previousDate = previousComment.headerDate();

        showHeader = !currentDate.equalsIgnoreCase(previousDate);
    } else {
        showHeader = true;
        currentDate = comment.headerDate();
    }

    cellView.showHeader(showHeader, currentDate);

    return cellView;
}

public void update(long itemUniqueId) {
    mCursor = Comment.fetchResultCursor(itemUniqueId);
    notifyDataSetChanged();
}

并在自定义 CommentCell 视图中显示/隐藏标题:

  public void showHeader(boolean show, String currentDate) {
    if (show) {
        headerTextview.setVisibility(VISIBLE);
        headerTextview.setText(currentDate);
    } else {
        headerTextview.setVisibility(GONE);
    }
}

创建字符串日期:

public String headerDate() {
    String createDateStr = null;
    if (createDate != Consts.NONE_LONG)
        createDateStr = TimeUtil.dateToString("dd MMMM", new Date(createDate));
    return createDateStr;
}

public static String dateToString(String format, Date date) {
    DateFormat dateFormat = new SimpleDateFormat(format, Locale.getDefault());
    String text = dateFormat.format(date);
    return text;
}
于 2016-02-27T20:28:49.270 回答