这是我第一次使用listviews中的日期。通过查看SO和谷歌,列表视图和绑定日期有很多资源和示例。许多示例仅属于日期字段,但暗示如何将其添加到其他字段中。我要么不理解如何实现这些,要么每个人都很难,或两者兼而有之。因此,我正在考虑从概念上开始做什么,然后找出我的逻辑错误或可以改进的地方将帮助其他人。
我正在显示一个包含多个textview字段的列表视图,其中一个是格式化日期。数据作为yyyymmdd存储在SQLite中以实现可排序性。 (listview按日期字段排序,在我的dbadapter中使用一个好的查询,并且工作正常)
我对listviews的绑定日期进行了一些研究,发现我的simplecursoradapter本身不能工作,但我还需要一个额外的CursorAdapter或ViewBinder。基于this example,我开始使用ViewBinder,但没有填充任何内容。要清楚,我所做的就是保留我的simplecursoradapter用于其他字段,并且它们正常运行。然后我添加了一个附加到绑定器的附加适配器。
以下是listview存在的活动的代码:
private void fillTasks() {
Cursor tasksCursor = mDbHelper.fetchAllTasksforBatch(mRowId);
startManagingCursor(tasksCursor);
// Create an array to specify the fields we want to display in the list
String[] from = new String[]{
VDbAdapter.COLUMN_taskid,
VDbAdapter.COLUMN_tasktype,
VDbAdapter.COLUMN_taskSpecGrav,
VDbAdapter.COLUMN_taskacid
//VDbAdapter.COLUMN_taskdate
};
// and an array of the fields we want to bind those fields to
int[] to = new int[]{ R.id.TASKID,
R.id.TASKTYPE,
R.id.TASKGRAVITY,
R.id.TASKACID
//R.id.DATE
};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter tasks = new SimpleCursorAdapter(this, R.layout.tasklist_layout, tasksCursor, from, to);
setListAdapter(tasks);
String[] fromdate = new String[]{
VDbAdapter.COLUMN_taskdate
};
// and an array of the fields we want to bind those fields to
int[] todate = new int[]{
R.id.DATE
};
// SimpleCursorAdapter tasksdates = new SimpleCursorAdapter(this, R.layout.tasklist_layout, tasksCursor, fromdate, todate);
final DateViewBinder binder = new DateViewBinder(formatforddisplay, R.id.DATE);
tasks.setViewBinder(binder);
}
以下是DateViewBinder中的代码,与上面的示例链接基本相同(包名称当然是正确的)。
import java.text.SimpleDateFormat;
import java.util.Date;
import android.database.Cursor;
import android.view.View;
import android.widget.SimpleCursorAdapter.ViewBinder;
import android.widget.TextView;
public class DateViewBinder implements ViewBinder {
private SimpleDateFormat mFormat;
private int mTargetViewId;
public DateViewBinder(SimpleDateFormat formatforddisplay, int targetViewId) {
mFormat = formatforddisplay;
mTargetViewId = targetViewId;
}
public void setBinding(int from) {
mTargetViewId = from;
}
public void setFormat(SimpleDateFormat format) {
mFormat = format;
}
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
final int id = view.getId();
if (id == mTargetViewId) {
final String value = getLongFieldAsString(cursor, columnIndex);
if (view instanceof TextView)
setViewText((TextView)view, value);
else
throw new IllegalStateException(view.getClass().getName() + " is not a view that can be bound by this view binder (" +
DateViewBinder.class.getSimpleName() + ")");
return true;
}
return false;
}
我在logcat中没有错误,所以通常这有助于我指向正确的方向。任何帮助,我的问题以及我如何陈述(第一次海报)
答案 0 :(得分:0)
这是通过将光标作为参数传递给DateViewBinder来解决的。以下是带有更改的代码:
填写任务更改
SimpleCursorAdapter tasks =
new SimpleCursorAdapter(this, R.layout.tasklist_layout, tasksCursor, from, to);
final DateViewBinder binder = new DateViewBinder(formatforddisplay, R.id.DATE, tasksCursor);
tasks.setViewBinder(binder);
setListAdapter(tasks);
DateViewBinder更改
public DateViewBinder(SimpleDateFormat formatfromBatchOverview, int targetViewId, Cursor taskscursor) {
mFormat = formatfromBatchOverview;
mTargetViewId = targetViewId;
cursor = taskscursor;
}