Ändern der Daten von SimplyReorAdapter
-
23-10-2019 - |
Frage
Ich arbeite an einer TV -Guide -App, die a verwendet ListActivity
Zeigen Sie die Fernsehsendungen für einen Kanal / einen Tag nach dem anderen. Ich benutze a RelativeLayout
für die ListView
Artikel und ich will das ListView
Etwas so aussehen:
07:00 The Breakfast Show
Latest news and topical reports
08:00 Tom and Jerry
More cat and mouse capers
Ich bekomme die Daten für die ListView
Elemente mit dem folgenden Code:
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);
Mein Problem ist, dass die start_time
Feld ist a datetime
mit dem folgenden Format:
2011-01-23 07:00:00
Also, was ich bekomme, ist Folgendes:
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
Was ich gerne tun würde, ist die oben genannte Verwendung zu formatieren SimpleDateFormat
("HH:mm"
) Also bekomme ich das nur das hour:minute
Teil von start_time
aufstellen.
Ich habe die gefunden SimpleCursor.ViewBinder
Schnittstelle, die darauf hindeutet, dass es das sein mag, was ich will, aber ich kann nicht herausfinden, wie ich es verwenden soll. Wenn ich Recht habe ViewBinder
, Ich würde mich über einige Hinweise freuen, um Code zu probieren, wie man ihn verwendet. Ansonsten wie kann ich sonst noch erreichen, um das zu ändern start_time
Feld, um einfach zu zeigen HH:mm
Format?
Lösung
Sie können so etwas tun:
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;
}
});