2

リストビューで日付を扱うのはこれが初めてです。SO と google を調べると、リストビューと日付のバインドに関する多くのリソースと例があります。例の多くは日付フィールドのみのものですが、他のフィールドにそれらを追加する方法をほのめかしています。これらの実装方法を理解していないか、誰もが苦労している、あるいはその両方です。そういう意味で、自分がやっていることを概念的に考え始めて、自分の論理が間違っているところや改善できるところを見つけ出すことが、他の人の助けになると考えています。

いくつかのテキストビュー フィールドを含むリストビューを表示しようとしていますが、そのうちの 1 つは書式設定された日付です。データは、並べ替えのために yyyymmdd として SQLite に格納されます。(リストビューは、私のdbadapterで適切なクエリを使用して日付フィールドでソートされ、正しく機能しています)

日付をリストビューにバインドすることについて少し調べたところ、単純なcursoradapterは単独では機能しないことがわかりましたが、追加のCursorAdapterまたはViewBinderが必要です。この例に基づいて、ViewBinder から始めましたが、フィールドに何も入力されていません。明確にするために、私が行ったことは、他のフィールドの単純なカーソルアダプターを保持することであり、それらは正しく機能しています。次に、バインダーに接続するアダプターを追加しました。

リストビューが存在するアクティビティのコードは次のとおりです。

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 でエラーが発生していないため、通常は正しい方向を示すのに役立ちます。私の問題と私がそれをどのように述べたかについて、どんな助けも大歓迎です(初めてのポスター)

4

1 に答える 1