2

I have Spinner or ListView with flag icon and currency name (I save their R ids in sqlite, for example R.drawable.aed and R.string.aed from drawable folder and string.xml)

Then I insert new drawable, for example cxx.png

My flag icons shifts, some of them changes with other images, not flags icons. This happen if I update app on my phone. If I install it, or delete and then install all work fine.

I understood, that sqlite in my program save R Ids of drawables only once on install. When I add or delete drawables or strings, R changes ids numbers, but in my sqlite table saved old R ids.

I can try to write method, that will update all R ids in sqlite everytime when I will add or delete new strings and drawables, by increasing DB_VERSION each time. Is there simpler solution?

This is one of my Adapters:

public class SpinnerCursorAdapter extends CursorAdapter {

public SpinnerCursorAdapter(Context context, Cursor cursor) {
    super(context, cursor, 0);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    View view = LayoutInflater.from(context).inflate(R.layout.spinner_item, parent, false);
    ViewHolder viewHolder = new ViewHolder(view);
    view.setTag(viewHolder);
    return view;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    ViewHolder viewHolder = (ViewHolder) view.getTag();

    int flagIconId = Integer.valueOf(cursor.getString(5));
    viewHolder.flagIcon.setImageResource(flagIconId);

    String currencyName = cursor.getString(4);
    viewHolder.currencyName.setText(currencyName);

    viewHolder.currencyCharCode.setText(cursor.getString(1));
}

private static class ViewHolder {
    final ImageView flagIcon;
    final TextView currencyName;
    final TextView currencyCharCode;

    private ViewHolder(View view) {
        flagIcon = (ImageView) view.findViewById(R.id.spinner_flag_icon);
        currencyName = (TextView) view.findViewById(R.id.spinner_currency_name);
        currencyCharCode = (TextView) view.findViewById(R.id.spinner_currency_char_code);
    }
}

}

Polurival
  • 58
  • 1
  • 9

2 Answers2

0

For saving String from string.xml use :

context.getResources().getString(R.string.value);

and for saving Drawable id's use :

String.valueOf(R.drawable.aed)

So your query will be:

insertCurrency(db, CharCode.AED, 0, 1, 1, 0, 3.672950, 3.672950, context.getResources().getString(R.string.aed),String.valueOf(R.drawable.aed), 0, 1, 1);
Janki Gadhiya
  • 4,492
  • 2
  • 29
  • 59
  • I'll try this solution. What a difference between saving String - String.valueOf(R.drawable.aed) and just Integer - R.drawable.aed? I am afraid of that it doesn't solve problem - if I insert new image in drawable folder and update my app from google play, some images can shifts in ListView (ListView inflated by CursorAdapter from sqlite) – Polurival Jul 11 '16 at 19:28
  • Image shifting is a [Recycling Problem of ListView](http://stackoverflow.com/questions/6921462/listview-reusing-views-when-i-dont-want-it-to) it is nothing to do with database and drawables & the Difference : I have converted the Int to **String** because by seeing your query **i guess your are entering String to the database so my guess was the column type of that column may be text or varchar**. If it is **Int** you can leave it **R.drawable.aed** – Janki Gadhiya Jul 12 '16 at 03:41
  • I have ViewHolder in Adapters. – Polurival Jul 12 '16 at 06:47
  • still you need some ArrayList to maintain the exact position with TAGs..!! – Janki Gadhiya Jul 12 '16 at 06:50
  • My problem - for example I have 'apple', 'melon', 'pear' in ListView. Then I adding new String 'mmmm' in string.xml and update app on my phone - ViewList shifts items not all but since certain position, in than case shifts will be from 'm' char. But if I remove app and then install where is no problem. – Polurival Jul 12 '16 at 07:05
  • i m not getting your problem will you please edit the question with some screenshots (before and after).. It would be more better.>!! – Janki Gadhiya Jul 12 '16 at 07:07
  • Ok, I'll make screenshots when problem repeats. And I'll try to add ArrayList to maintain the exact position with TAGs – Polurival Jul 12 '16 at 07:10
  • I've updated the question with screenshots , please see – Polurival Jul 12 '16 at 17:27
  • Its is the recycling view of listView problem. can i see your adapter code ?? @Polurival – Janki Gadhiya Jul 13 '16 at 03:48
0

I find flexible solution for saving String and Drawable resources:
for example I have blabla.png and it's identifier R.drawable.blabla, then I save String blabla in sqlite.
In my CursorAdapter I take current identifier of blabla using this:
https://stackoverflow.com/a/4428288/318460
but instead of Drawable.class I use R.drawable.class.

Same with String resources.

Profit - it allows to change texts of strings in string.xml without updating sqlite table.

Community
  • 1
  • 1
Polurival
  • 58
  • 1
  • 9