Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: Binding dates to listview alongside other data
    text
    copied!<p>This is my first time working with dates in listviews. From looking through the SO and google, there are lots of resources and examples on listviews and binding dates to them. Many of the examples are of the date field only, but alluding to how to add them in with other fields. I'm either not understanding how to implement these, or everyone has a hard time with it, or both. So in that way I'm thinking starting with what I'm doing conceptually and then figuring out where my logic is wrong or can improve will help others. </p> <p>I am working to display a listview with several textview fields, one of which is a formatted date. The data is stored in SQLite as yyyymmdd for sortability. (The listview is sorted by the date field using a good query in my dbadapter and that is working correctly)</p> <p>I researched a bit on binding dates to listviews, and found that my simplecursoradapter wont work by itself but I need an additional CursorAdapter OR ViewBinder. Based on <a href="http://andrewreyling.com/2011/10/23/using-dates-with-android-lists-and-sqlite/" rel="nofollow">this example</a>, I've started with a ViewBinder, but Not having my field populated with anything. To be clear, what I've done is kept my simplecursoradapter for the other fields, and they are functioning correctly. I then added an additional adapter with connection to the binder.</p> <p>Here is the code from the activity where the listview exists: </p> <pre><code>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); } </code></pre> <p>Here is the code from the DateViewBinder, essentially the same as from the example link above(package name is correct of course).</p> <pre><code>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; } </code></pre> <p>I am getting no errors in logcat, so usually that helps me get pointed in the right direction. Any help is appreciated, with my problem and with how I've stated it (first time poster)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload