I dati di Modifica SimpleCursorAdapter
-
23-10-2019 - |
Domanda
Sto lavorando su un'applicazione TV Guide che utilizza un ListActivity
che mostra i programmi TV per un canale / un giorno alla volta. Io sto usando un RelativeLayout
per le voci ListView
e voglio il ListView
di simile a questa:
07:00 The Breakfast Show
Latest news and topical reports
08:00 Tom and Jerry
More cat and mouse capers
ho i dati per le voci ListView
utilizzando il seguente codice:
Cursor cursor = db.rawQuery(SELECT blah,blah,blah);
String[] columnNames = new String[]{"start_time","title", "subtitle"};
int[] resIds = new int[]{R.id.start_time_short, R.id.title, R.id.subtitle};
adapter = new SimpleCursorAdapter(this, R.layout.guide_list_item, cursor, columnNames, resIds);
Il mio problema è che il campo start_time
è un datetime
con il seguente formato:
2011-01-23 07:00:00
così quello che ottengo è questo:
2011-01-23 07:00:00 The Breakfast Show
Latest news and topical reports
2011-01-23 08:00:00 Tom and Jerry
More cat and mouse capers
Quello che mi piacerebbe fare è il formato sopra utilizzando SimpleDateFormat
("HH:mm"
) quindi ho solo la parte hour:minute
del campo start_time
.
Ho trovato l'interfaccia SimpleCursor.ViewBinder
che suggerisce che potrebbe essere quello che voglio, ma non riesco a capire come usarlo. Se ho ragione su ViewBinder
, mi farebbe piacere alcune indicazioni per il codice di esempio su come usarlo. In caso contrario, in quale altro modo posso raggiungere cambiando il campo start_time
semplicemente mostrare formato HH:mm
?
Soluzione
Si può fare qualcosa di simile:
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int column) {
if( column == 0 ){ // let's suppose that the column 0 is the date
TextView tv = (TextView) view;
String dateStr = cursor.getString(cursor.getColumnIndex("name_of_the_date_column"));
// here you use SimpleDateFormat to bla blah blah
tv.setText(theFormatedDate);
return true;
}
return false;
}
});