how to load the related Bitmap according to input

by wine » Wed, 07 Oct 2009 02:42:55 GMT


Sponsored Links
 hi,

I have met an issue.
There are some pics in the res/drawable named as pic_1.png, pic_2.png,
pic_3.png, pic_4.png.... I want to load someone according to the
input.
For instance, if I input 2, I want to load pic_2.png. If input 3, will
load the pic_3.png....
How to achieve? Need your expert help here...

--~--~---------~--~----~------------~-------~--~----~



how to load the related Bitmap according to input

by I82Much » Wed, 07 Oct 2009 19:12:42 GMT


 I take it you're looking for something a little more scalable than the
naive solution:

switch (input) {
    case 1:
        return R.drawable.pic_1;
    . . .
    case n:
        return R.drawable.pic_n;
}

I see the problem; you need to append an arbitrary number to the end
of a string, and then turn that into the R.drawable. ___ reference.
I'm stumped



--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


how to load the related Bitmap according to input

by I82Much » Wed, 07 Oct 2009 19:15:33 GMT


 I suppose you could also do something like

int[] iconRefs = {
    R.drawable.pic_1,
    ...
    R.drawable.pic_n
}

if (input >= 0 && input < n) {
    // Account for 0 based indexing
    return iconRefs[input - 1];
}

This code gets the reference out (I might be wrong in how you
reference it, maybe R.res.drawable, don't have my eclipse handy);
you'd have to use the standard way of actually getting that drawable
image.



--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. WebView and Media

I am a noob so example are best. I have used the simple code for using
a webview but if i click on a link that points to a media file nothing
happens. I'd like to be able to stream audio or video from the webview
like it does in the browser but without opening the browser. How can I
get that functionality to work?


code:
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebViewdemo extends Activity {
        /** Called when the activity is first created. */
        WebView webview;
        @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        webview = (WebView) findViewById(R.id.webview);
        webview.setWebViewClient(new webviewClient());

        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setPluginsEnabled(true);
        webview.loadUrl("http://google.com");
        }
        private class webviewClient extends WebViewClient {
            @Override
            public boolean shouldOverrideUrlLoading(WebView
view,String url){
                view.loadUrl(url);
                return true;
            }
        }
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if ((keyCode == KeyEvent.KEYCODE_BACK) &&webview.canGoBack
()) {
                webview.goBack();
                return true;
            }
            return super.onKeyDown(keyCode, event);
        }
}

-- 

2. Screen Widget update time (android:updatePeriodMillis)

I am creating my widget, witch should check the free space on SDCard.
I set android:updatePeriodMillis="10000", but it doesn't work... The
widget doesn't update.
On anddev.org (http://www.anddev.org/viewtopic.php?p=27758) I found
information, that this option doesn't work in 1.6. Is this true?
Could you advice me, how to use update period for screen widget?

-- 

3. Problem Deleting From GroupMembership

4. Crash report app for beta testers

5. Contacts and Preferences framework

6. Address book postal address not visible ?

7. Debugging on Acer Liquid