开发者

Comparing string array with string and posting my result to main.xlm

I am having trouble with understanding how to compare strings in Java for Android. I have written code to do this in JavaScript and Palm but am new to Java and am a little confused. Case in point, I am trying to modify the example on the Android Developers site for SpinnerActivity (http://developer.android.com/resources/samples/Spinner/src/com/android/example/spinner/SpinnerActivity.html). In my application I am looking at pipe sizes in the spinner not planets. When the user picks a pipe size I want to reference an array of pipe sizes and be able to pick other parameters associated with that pipe size like the outside diameter (OD) of the pipe. I have modified the above sample code and added and array for the pipe sizes and the OD sizes. I then try to compare what the user picked in the pipe sizes spinner with my pipe sizes array and use the number of the array that matches to pick the associated OD. There is something wrong with the way I am trying to make this comparision. I set both of these values as stings but they never seem to find one another.

HelloSpinner1.java section I have changed is:

       public void onItemSelected(AdapterView<?> parent, View v, int pos, long row) {

            HelloSpinner1.this.mPos = pos;
            HelloSpinner1.this.mSelection = parent.getItemAtPosition(pos).toString();
            /*
             * Set the value of开发者_如何学编程 the text field in the UI
             */
            TextView resultText = (TextView)findViewById(R.id.SpinnerResult);
            resultText.setText(HelloSpinner1.this.mSelection);

            String[] OD;              // array of pipe ODs
            OD = new String[30];      // allocates memory for 30 floating point numbers
            OD[0] = "0.405";
            OD[1] = "0.540";
            OD[2] = "0.675";
            OD[3] = "0.840";
            OD[4] = "1.050";
            OD[5] = "1.315";
            OD[6] = "1.660";
            OD[7] = "1.9";
            OD[8] = "2.375";
            OD[9] = "2.875";
            OD[10] = "3.5";
            OD[11] = "4";
            OD[12] = "4.5";
            OD[13] = "5.563";
            OD[14] = "6.625"; 
            OD[15] = "8.625";
            OD[16] = "10.750";
            OD[17] = "12.75";
            OD[18] = "14";
            OD[19] = "16";
            OD[20] = "18";
            OD[21] = "20";
            OD[22] = "22";
            OD[23] = "24";
            OD[24] = "26";
            OD[25] = "28";
            OD[26] = "30";
            OD[27] = "32";
            OD[28] = "34";
            OD[29] = "36";


String [] Size;
Size = new String [30];
            Size[0] = "1/8";
            Size[1] = "1/4";
            Size[2] = "3/8";
            Size[3] = "1/2";
            Size[4] = "3/4";
            Size[5] = "1";
            Size[6] = "1-1/4"; 
            Size[7] = "1-1/2";
            Size[8] = "2";
            Size[9] = "2-1/2";
            Size[10] = "3";
            Size[11] = "3-1/2";
            Size[12] = "4";
            Size[13] = "5";
            Size[14] = "6";
            Size[15] = "8";
            Size[16] = "10";
            Size[17] = "12";
            Size[18] = "14";
            Size[19] = "16";
            Size[20] = "18";
            Size[21] = "20";
            Size[22] = "22"; 
            Size[23] = "24";
            Size[24] = "26";
            Size[25] = "28";
            Size[26] = "30";
            Size[27] = "32";
            Size[28] = "34";
            Size[29] = "36"; 



            String ODSize;

            for (int i = 0; i  <= 29; i++){
                    if (Size.equals("HelloSpinner1.this.mSelection")) {
                    ODSize = OD[i];
                    break;
                     }
                }  

        }

The associated strings.xml rorm the android site with slight modifications is:

Pipe and Tube

1/8 1/4 3/8 3/4 1 1-1/4 1-1/2 2 2-1/2 3 3-1/2 4 5 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 Select a Pipe Size


Just a quick note, but I see two things here:

1) You have "HelloSpinner1.this.mSelection" in quotes. Inside .equals, that is comparing your variable Size directly to that string ... not the value stored in that object. For example, you're asking: "1/8" ?= "HelloSpinner1.this.mSelection" ... not, "1/8" ?= "1/4" in this case. That might be most of your problem here.

2) You could just use the position of the spinner inside your Listener method. That gives you the position of the selection on the spinner. If you aren't modifying those values, you would already know the index into your array. If you were modifying them, /then/ you could do a string comparison (or concurrently modify your array to keep them up to date).

You might also want to check what you're comparing against once you eliminate the quotes problem. A very simple way to do that would be to declare a String for each value, then run it in the debugger or log the output.

Lastly, as personal preference, I don't like to store long arrays which aren't going to change in the xml file. Just code that up as an array which doesn't have to be interpreted later. That'll give you the array access directly, and speed up execution some.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜