Quiero mostrar ListView con Date como SectionHeader.ListView personalizado con fecha como SectionHeader (Usado CustomCursorAdapter personalizado)
Lo que tengo: Estoy mostrando ListView de la base de datos sqlite usando SimpleCursorAdapter personalizado.
My Custom SimpleCursorAdapter es:
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;
}
}
Google Resultados hasta la fecha:
Problema: Quiero mostrar listview con la fecha como encabezados de sección. Los valores de la fecha del curso provienen de la base de datos sqlite.
¿Puede alguien por favor guía cómo puedo lograr esta tarea.
O proporcióneme un código de ejemplo o un código exacto (como) relacionado con el mismo.
Editado Según la respuesta de Graham Borald (esto funciona bien. Sin embargo, fue una solución rápida.)
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;
}
}
Editado Según la respuesta de 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();
}*/
}
estoy recibiendo Excepción del puntero nulo en la línea: Cursor date=adapter.fetchTodo(id);
Parece que el Cursor no está obteniendo un ny datos.
https://github.com/emilsjolander/StickyListHea Gracias 0rs. – Mertuarez