0

SQLiteDBの情報から生成されたリストビューを作成しようとしています。現在、各行を適切に表示し、3つのテキストビューにデータを入力するようにコードを設定しています。ある種のIfステートメントを使用して、値の1つが「Room1」であるかどうかを確認し、その行の背景を特定の色に設定できるようにしたいと思います。

このように、リストの各行は、データの「部屋」に基づいて異なる背景を持ちます。

SimpleCursorAdapterを拡張しようとしましたが、必要なものを取得する方法に少し迷っています。

SimpleCursorAdapterについて現在持っているものは次のとおりです。

Cursor notesCursor = myDbHelper.fetchDayPanelNotes(type, day);
startManagingCursor(notesCursor);
String[] from = new String[]{DataBaseHelper.KEY_NAME, DataBaseHelper.KEY_ROOM, DataBaseHelper.KEY_TIME};

int[] to = new int[]{R.id.text1, R.id.text2, R.id.text3};

SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.main_row, notesCursor, from, to);
setListAdapter(notes);
4

4 に答える 4

9

getViewアダプタのメソッドをオーバーライドする必要があります。

SimpleCursorAdapter notes = new SimpleCursorAdapter(
        this, R.layout.main_row, notesCursor, from, to)
{
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        final View row = super.getView(position, convertView, parent);
        if (position % 2 == 0)
            row.setBackgroundResource(android.R.color.darker_gray);
        else
            row.setBackgroundResource(android.R.color.background_light);
        return row;
    }
};

位置に基づいて行の背景を設定します(奇数か偶数か)。

于 2011-05-02T19:15:36.680 に答える
2

これでうまくいくはずです。この投稿のおかげで、まったく同じ問題が発生しましたが、これを実行した後、以下に示すように問題が解決しました。

    SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.notes_row, mNotesCursor, from, to)
    {
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            cursor.moveToPosition(position);
            String s = cursor.getString(mursor.getColumnIndex("roomnumber"));
            final View row = super.getView(position, convertView, parent);

            if (s.equalsIgnoreCase("room1"))
                row.setBackgroundColor(Color.parseColor("#480000"));
            else if (s.equalsIgnoreCase("room2"))
                row.setBackgroundColor(Color.parseColor("#000040"));
            else 
                row.setBackgroundColor(Color.parseColor("#004000"));
            return row;
        }
    };       


    setListAdapter(notes);
于 2012-06-13T19:41:22.483 に答える
1

SimpleCursorAdapter.ViewBinderをアクティビティに実装してから、setViewValueをオーバーライドします。

public class MyActivity extends Activity
    implements SimpleCursorAdapter.ViewBinder {

    SimpleCursorAdapter myAdapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState)

        // Initialize myAdapter
        myAdapter.setViewBinder(this);
    }

    @Override
    public boolean setViewValue(View view, Cursor cursor, int column) {
        // Put code to process row data here
    }
}
于 2011-05-02T19:33:20.827 に答える
0

拡張BaseAdapterし、リストデータを手動で入力し、そのデータのローカルコピーをアダプタクラスに保持することをお勧めします。次に、メソッドを実装するときに、生成しgetView(...)ている位置またはIDを使用してView、対応するデータをフェッチして「Room1」とView照合し、その比較に基づいて返す値を変更できます。

rekaszeruの答えは正確に正しい考えですが、位置とIDよりも多くの情報が必要になるようです。そのため、レベルを下げてBaseAdapter直接実装することをお勧めします。

于 2011-05-02T19:18:22.417 に答える