简体   繁体   中英

Android - Loading (updating) string resources from internet?

I want to be able to update strings from internet. Let's say I downloaded list of updated strings in a hash map an each string is mapped to ther ids (R.string.). I thought I could modify string.xml but they are written on rocks I guess.

How can replace view's strings and use the updated list while inflating things? Currently I tried two things.

First I created a custom getResources for my activity which returns a custom resource object with modified getString. However, probably activity inflator does not use getResources() so nothing changes.

After that I thought I can override setText methods of buttons etc however they are final for some reasons.

Any other suggestions? I want to automate this process, otherwise it will be very hard. (Can I even find which views uses which ids? Maybe I can parse resource xmls)

Thanks for all

Use database, or sharedPreferences to keep strings' values, and use as default R.string.bla_bla, cause there is no way to change resources but update the whole app. Try something like this to read string:

SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences( context );
String bla_bla = mSharedPreferences.getString( "R.string.bla_bla", context.getString( R.string.bla_bla ));

And to replace value:

Editor editor = PreferenceManager.getDefaultSharedPreferences( context ).edit();
editor.putString( "R.string.bla_bla", bla_bla );
editor.commit();

updated

Ok, i got it. Then you should create your own class extends Button , like this one.

public class MButton extends Button {
    String mText;
    public MButton( Context context, AttributeSet attrs ) {
        super( context, attrs );
        loadText( context, attrs );
    }
    public MButton( Context context, AttributeSet attrs, int defStyle ) {
        super( context, attrs, defStyle );
        loadText( context, attrs );
    }
    void loadText( Context context, AttributeSet attrs ) {
        String stringId = attrs.getAttributeValue( "http://schemas.android.com/apk/res/android", "text" );
        // stringId = @2130903040
        int intStringId = Integer.parseInt( stringId.substring( 1 ));
        // intStringId = 2130903040
        mText = PreferenceManager.getDefaultSharedPreferences( context ).getString( stringId, context.getString( intStringId ));
    }
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        setText( mText );
    }
}

And use it in your layouts:

<com.example.test.MButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:onClick="clicked" />

But make sure that you clean up all SharedPreferences keeps custom strings, when you update your app, cause your resource ids will be reordered. Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM