0

特定の条件に基づいて、1つ(おそらくそれ以上)のListView行のみを変更しようとしています。同様の質問や他のたくさんのチュートリアルについて多くの回答を読みましたが、何もできません。

まさに私が達成したいのは、SQLiteの行が特定の値に設定されている場合に、行の背景(簡単なバージョン)または行の画像(難しいバージョンだと思います)を他の行とは異なる設定にすることです。

ListActivityを拡張するActivityがあり、ListViewアダプターを次のように設定しています。

private void refreshList() {
    mySQLiteAdapter = new MyDBAdapter(this);
    mySQLiteAdapter.open();
    String[] columns = { MyDBAdapter.KEY_TITLE, MyDBAdapter.KEY_GENRE,
            MyDBAdapter.KEY_PRICE, MyDBAdapter.KEY_ID };

    Cursor contentRead = mySQLiteAdapter.getAllEntries(false, columns,
            null, null, null, null, MyDBAdapter.KEY_TITLE, null);

    SimpleCursorAdapter adapterCursor = new SimpleCursorAdapter(this,
            R.layout.row, contentRead, columns, new int[] {
                    R.id.text1, R.id.detail });

    this.setListAdapter(adapterCursor);
    mySQLiteAdapter.close();
}

この関数は、onCreateメソッドとonResumeで呼び出されます。列の値MyDBAdapter.KEY_PRICEが5に等しい行の異なる色/画像を設定したい。R.layout.rowは行のデザインを含む私のxmlファイルです。

多分誰かがこれで私を助けることができますか?または、少なくともそれを説明するチュートリアルを表示しますか?

4

2 に答える 2

3

SimpleCursorAdapterを拡張し、bindView()をオーバーライドするだけです。

public class MyAdapter extends SimpleCursorAdapter {
    public MyAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        super.bindView(view, context, cursor);
        if(cursor.getLong(cursor.getColumnIndex(MyDBAdapter.KEY_PRICE)) == 5)
            view.setBackgroundColor(0xff00ff00);
        else // this will be the default background color: transparent
            view.setBackgroundColor(0x00000000);
    }
}
于 2012-07-16T22:22:57.653 に答える
0

SimpleCursorAdapter上で拡張するカスタムアダプターを作成してみてください。その中で、bindViewメソッドで、探している条件が満たされているかどうかを確認し、そのメソッドで必要なものを作成できます。

http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html#bindView(android.view.View、android.content.Context、android.database.Cursor)

助けてくれることを願っています:)

于 2012-07-16T22:19:05.250 に答える