You can add an additional column to SimpleCursorAdapter

I have successfully used SimpleCursorAdapter to display the display name and value (both in my database) and display them in my list activity.

What I'm trying to do now is add a new view (ImageView) for each item in my list. In the end, it should look like this.

Image_1_NotInDB -- DisplayName1FromDB -- DisplayName1FromDB
Image_2_NotInDB -- DisplayName2FromDB -- DisplayName2FromDB.

Image will be different (based on DisplayName1FromDB). I do not think SimpleCursorAdapter is good for this purpose.

I tried to create a customSimpleCursorAdapter that extends SimpleCursorAdapter, and tried to use the 'newView' and 'bindView' methods to achieve. I pretty much followed this up: Custom CursorAdapters .

The problem is this: which image am I using based on the value from the database (which I was going to pass in the constructor or customSimpleCursorAdapter)

public View newView(Context pContext, Cursor pCursor, ViewGroup pParent)
{
    Cursor lCursor = getCursor();
    final LayoutInflater inflater = LayoutInflater.from(pContext);
    View lView = inflater.inflate(layout, pParent, false);
    int lImage = "dog".equals(variable) ? R.drawable.dog : R.drawable.cat;
    // "variable" is a member variable (set at the constructor)
    ImageView lImageView = (ImageView) lView.findViewById(R.id.appImage);
    if (lImageView != null)
    {
        lImageView.setImageResource(lImage);
    }
    return pParent;
}

public void bindView(View pView, Context pContext, Cursor pCursor)
{
    int lImage = "dog".equals(variable) ? R.drawable.dog : R.drawable.cat;
    // "variable" is a member variable (set at the constructor)
    ImageView lImageView = (ImageView) lView.findViewById(R.id.appImage);
    if (lImageView != null)
    {
        lImageView.setImageResource(lImage);
    }
}

This is how I tried using "customSimpleCursorAdapter"

    String[] lDisplay = new String[] {KEY_NAME, KEY_TIME};
    int[] lValues = new int[] {R.id.name, R.id.time};
    CustomRowCursorAdapter lCursorAdapter = new CustomRowCursorAdapter(this, R.layout.row, lSTime, lDisplay, lValues, "MY IDEA WAS TO PASS THE ANIMAL NAME HERE, BUT NOT LUCK as I am not sure How ");
    lCursorAdapter.newView(this, lSTime, getListView());
    lCursorAdapter.bindView(getListView(), this, lSTime);
    setListAdapter(lCursorAdapter);

Is the ArrayAdapter the answer? If so, could you share what parameters you could pass to him?

+3
source share
3 answers

I recommend switching to a custom adapter

+1
source

Is the ArrayAdapter the answer?

No, you cannot use ArrayAdapterhere. Since you need to display data from the database, you should use Custom here CursorAdapter.

0
source

.

Here is an example projection that I used to format the value based on the price of an item (stored in cents) and the item associated with it.

public static final String CONCATE_COST
    = "'$' || CASE WHEN SUBSTR(ROUND("+COLUMN_PRICE
        +"/100.0, 2), LENGTH(ROUND("+COLUMN_PRICE
        +"/100.0, 2))-1, 1)='.' THEN ROUND("+COLUMN_PRICE
        +"/100.0, 2) || '0' else ROUND("+COLUMN_PRICE
        +"/100.0, 2) end || "+COLUMN_UNIT;

Use it as a column, and you can associate it with a field in your view using normal mapping columnto R.id.*.

0
source

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


All Articles