Some of them help to understand columnIndex in ViewBInder.

Skip below if you just want to see the question without context

In the Android app I am creating, there is a simple table with three columns:

_id INTEGER PRIMARY KEY ..., name TEXT, color INT

This table is called categories. I load my categories from the database and pass them in SimpleCursorAdapterfor use with the Spinnerfollowing:

String[] from = new String[] {
        ListDbAdapter.KEY_CATEGORY_NAME,
        ListDbAdapter.KEY_CATEGORY_COLOR };
int[] to = new int[] { R.id.categorySpinnerItem };

mCategorySpinnerAdapter = new SimpleCursorAdapter(this,
    R.layout.category_spinner_item, categoryCursor, from, to);

mCategorySpinnerAdapter
    .setViewBinder(new CategorySpinnerViewBinder());
mCategorySpinner.setAdapter(mCategorySpinnerAdapter);

I set a custom ViewBinderone because I want the category name to be the text of the spinner element and the color to be the background color. My ViewBinderlooks like this:

private static final int NAME_COLUMN = 1;
private static final int COLOR_COLUMN = 2;

@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {

    TextView textView = (TextView) view;

    String name = cursor.getString(NAME_COLUMN);
    int color = cursor.getInt(COLOR_COLUMN);

    textView.setText(name);
    textView.setBackgroundColor(color);

    return true;
}

Here is my question (finally)

setViewValue , columnIndex? ", ", setViewValue, , columnIndex 1.

, setViewValue from, columnIndex 1, 2. , , .

, , - NAME_COLUMN COLOR_COLUMN. setViewValue - ViewBinders.

+3
2

SimpleCursorAdapter setViewValue bindView:

bound = binder.setViewValue(v, cursor, from[i]);

[i], , int [], . [i] int [] , , 1 - R.id.categorySpinnerItem

EDIT: String [] int [] , - int R.id... first view [0], - [1] .., 10 , 3 R.id-s, [2]:)

+1

, , - true, , - false ( ). , OnTouchEvent - . , true 1, 2, , , , 1 columnIndex.

- - OO, POJO , , db, "" , MVC. , - , KEY_CATEGORY_NAME "cat_name ## cat_description" (), Adapter. , getName() "cat_name", .

, CursorAdapters, columnIndex, , , , CursorAdapter :)

+1

Source: https://habr.com/ru/post/1775090/


All Articles