2012-06-05 3 views
0

String.xml곱한 2 개 회 값 함께

<string-array name="fruits"> 
     <item>Apple</item> 
     <item>Banana</item> 
     <item>Orange</item> 
     <item>Pear</item> 
     <item>Watermelon</item> 
     <item>Mango</item> 
     <item>Pineapple</item> 
     <item>Strawberry</item> 
</string-array> 

<string-array name="total"> 
    <item>1</item> 
    <item>2</item> 
    <item>3</item> 
    <item>4</item> 
    <item>5</item> 
    <item>6</item> 
    <item>7</item> 
    <item>8</item> 
</string-array> 

<string-array name="calorie"> 
    <item>80</item> 
    <item>101</item> 
    <item>71</item> 
    <item>100</item> 
    <item>45</item> 
    <item>135</item> 
    <item>80</item> 
    <item>53</item> 
</string-array>  

자바 파일 : 그래서 사용자가 클릭 회 3 개 바나나, 나는이 작업을 수행하는 방법을 알고 싶은 경우

so when user click 3 bananas in spinner, i want to know how to do the code for multiplication of calories. once done with the calculation, it should show 303 calories in the "total calorie" textview

public class Fruit extends Activity implements OnClickListener, OnItemSelectedListener { 
private TextView tvFruit, tvNo; 
Context context=this; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.fruit); 

    tvFruit = (TextView) findViewById(R.id.tvfruit); 
    tvNo = (TextView) findViewById(R.id.tvno); 


    final Spinner fruits = (Spinner)findViewById(R.id.spin_fruit); 
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
      this,R.array.fruits,android.R.layout.simple_spinner_item); 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    fruits.setAdapter(adapter); 
    fruits.setOnItemSelectedListener(this); 

    fruits.setOnItemSelectedListener(new OnItemSelectedListener(){ 
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, 
    long arg3) { 
      // TODO Auto-generated method stub 
      String selectedItem = fruits.getSelectedItem().toString(); 
      tvFruit.setText(selectedItem); 
     } 

    public void onNothingSelected(AdapterView<?> arg0) { 
     // TODO Auto-generated method stub 

    } 

    }); 

    final Spinner num = (Spinner)findViewById(R.id.spin_no); 
    ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
      this,R.array.total,android.R.layout.simple_spinner_item); 
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    num.setAdapter(adapter1); 
    num.setOnItemSelectedListener(this); 

    num.setOnItemSelectedListener(new OnItemSelectedListener(){ 
     public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, 
     long arg3) { 
       // TODO Auto-generated method stub 
       String selectedItem = num.getSelectedItem().toString(); 
       tvNo.setText(selectedItem); 
      } 

     public void onNothingSelected(AdapterView<?> arg0) { 
      // TODO Auto-generated method stub 

     } 

     }); 

    Button save = (Button) findViewById(R.id.bsave); 
    save.setTextColor(Color.BLUE); 
    save.setOnClickListener(new View.OnClickListener() { 


      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       // Get the subject details and show it in an alertdialog 
       AlertDialog.Builder builder = new AlertDialog.Builder(context); 
       builder.setMessage("Success"); 
       builder.setPositiveButton("OK", null); 
       builder.setNegativeButton("View Log", new DialogInterface.OnClickListener(){ 

        public void onClick(DialogInterface dialog, int which) { // this part is done for the negative button 
                      // if we want it to link to new intent 
         launchIntent(); 
        } 

       }); 
       builder.create().show(); 
      } 

      //making the "View Log" button in dialog box to go to new intent FruitLog.class 
      private void launchIntent(){ 
       Intent i = new Intent(Fruit.this, FruitLog.class); 
       i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(i); 
      } 


     }); 

    } 
칼로리 곱셈 코드. 한 번 계산을 완료하면 "총 칼로리"텍스트 뷰에 303 칼로리가 표시되어야합니다

누군가가 계산을 코딩하는 방법을 안내해 줄 수 있습니까? 예를 들어 2 가지 사과 칼로리를 도와 주면 나머지는 알아낼 것입니다. 정말 고맙습니다. 이 커뮤니티는 매우 도움이되고 있습니다. 기존의 코드를 사용

답변

1

, 나는이의 라인을 따라 뭔가를 시도 할 것입니다 :

먼저, integer-arraycalorie 배열을 변경합니다. 다음에 다음을 추가하십시오 :

public int calculateCalories() { 
    int[] calorie = getResources().getIntArray(R.array.calorie); 
    return Integer.parseInt((String) num.getSelectedItem()) * calorie[fruits.getSelectedItemPosition()]; 
} 

이 함수는 선택된 과일 열의 칼로리 값을 반환해야합니다. 명시 적으로 그렇게 같은 텍스트 뷰에 직접 설정 :

totalTextView.setText(String.valueOf(calculateCalories())); 

당신은 그들에게 tvFruit 선언 같은 방식으로 선언함으로써, 전체 클래스에 numfruits 볼 수 있도록해야합니다.

+0

그리고 사용자가 코드로 'Spinner'수량의 첫 번째 위치를 선택하면 어떻게됩니까? – Luksprog

+0

@Luksprog 때로는 컴파일러가 내장되어 있기를 바랍니다. – Sam

+0

'Spinner', ** num **에서 선택한 위치를 구문 분석하고 있습니다 (코드가 올바르다면). 이것이 선택된 첫 번째 위치에 해당하면 '0'을 파싱 할 것이므로 양의 첫 번째 위치에 대해 총 금액 값은 항상 '0'이됩니다. – Luksprog