2015-01-09 2 views
-3

안녕하세요 저는 여기 처음 왔어요. 제 언어에 대해 사과하고 싶습니다. 저는 첫 번째 안드로이드 앱을 만들고 싶었고, 일부 이슈로 치열한 경쟁을 벌였습니다. MainActivity는 두 개의 버튼과 회 전자를 포함하는 메인 화면을 나타냅니다. 회 전자 사용자는 하나의 텍스트 파일을 선택할 수 있고 선택 후 spinnerActivity가 트리거되어야합니다 (열린 텍스트 파일 화면). 텍스트가있는 회 전자 트리거 활동의 모든 레코드가 아니기 때문에 문제가 발생했습니다. 원하는 파일 (원하는 트리거 파일 하나와 비어있는 TextView가있는 다른 트리거 스크린). 여기에 spinnerActivity의 레이아웃입니다하나의 활동에서 다른 활동으로 데이터 보내기

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     > 
     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      > 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="199dp" 
     android:layout_height="89dp" 
     android:layout_below="@+id/editText1" 
     android:layout_centerHorizontal="true" 
     android:text="nie dziala" /> 

    <TextView 
     android:id="@+id/proximityTextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"    
     android:text="0.0" 
     android:textAppearance="?android:attr/textAppearanceLarge"/> 
     </LinearLayout> 
     </ScrollView> 
</LinearLayout> 

코드 MainACtivity

package com.example.myapp; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.ActionBarActivity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemSelectedListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.Spinner; 

public class MainActivity extends ActionBarActivity implements OnItemSelectedListener { 

     @Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     final Button click=(Button)findViewById(R.id.button2); 


     click.setOnClickListener(new View.OnClickListener(){ //nowy actionlistener 
     @Override 
     public void onClick(View v) { 
     Intent activitylauncher=new Intent(MainActivity.this,AboutActivity.class); 
     //tworzenie nowej intencji Create an intent for a specific component. 
     startActivity(activitylauncher); 
     }      
     }); 
    final Button click2=(Button)findViewById(R.id.button1); 
     click2.setOnClickListener(new View.OnClickListener() { //nowy actionlistener 
    @Override 
     public void onClick(View v) { 
     Intent activitylauncher=new Intent(MainActivity.this,RandomActivity.class); 
     //tworzenie nowej intencji Create an intent for a specific component. 
     startActivity(activitylauncher); 
       } 
       }); 
     Spinner spinner = (Spinner) findViewById(R.id.spinner1); 
       // Create an ArrayAdapter using the string array and a default spinner layout 
       ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.recipie_array, android.R.layout.simple_spinner_item); 
       // Specify the layut to use when the list of choices appears 
       adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
       // Apply the adapter to the spinner 
     spinner.setAdapter(adapter); 
     spinner.setOnItemSelectedListener(this); 

}  

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


     // String i=Integer.toString(pos); 
       if(pos==1) 
       { 

     Intent intent3 = new Intent(MainActivity.this, SpinnerActivity.class); 

         intent3.putExtra("txt",(pos)); 
         startActivity(intent3); 
       } 

       if(pos==2) 
       { 

     Intent intent3 = new Intent(MainActivity.this, SpinnerActivity.class); 

         startActivity(intent3); 
       } 
       if(pos==1) 
       { 

     Intent intent3 = new Intent(MainActivity.this, SpinnerActivity.class); 

         startActivity(intent3); 
       } 

     } 

     // An item was selected. You can retrieve the selected item using 
     // parent.getItemAtPosition(pos) 


    public void onNothingSelected(AdapterView<?> parent) { 
     // Another interface callback 
    } 





    @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
       } 



    @Override 

public boolean onOptionsItemSelected(MenuItem item){ 
       // Handle action bar item clicks here. The action bar will 
       // automatically handle clicks on the Home/Up button, so long 
       // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
      return true; 
                   } 
     return super.onOptionsItemSelected(item); 
       } 
} 

및 spinneractivity

package com.example.myapp; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 

import android.content.Intent; 
import android.hardware.Sensor; 
import android.hardware.SensorEvent; 
import android.hardware.SensorEventListener; 
import android.hardware.SensorManager; 
import android.os.Bundle; 
import android.os.Environment; 
import android.support.v7.app.ActionBarActivity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.TextView; 

public class SpinnerActivity extends ActionBarActivity implements SensorEventListener { 
TextView proxText; 
SensorManager sm; 
Sensor proxSensor; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.activity_spinner); 
       //proximitymeter 
       sm=(SensorManager)getSystemService(SENSOR_SERVICE); 
       proxSensor=sm.getDefaultSensor(Sensor.TYPE_PROXIMITY); 
       proxText=(TextView)findViewById(R.id.proximityTextView); 
       sm.registerListener(this,proxSensor,SensorManager.SENSOR_DELAY_NORMAL); 



       Intent intent3 = getIntent(); 
       int value = intent3.getIntExtra("txt", 0); 
       //finish(); 
       if (value==1) 
       { 
         open("przepis1.txt"); 
       } 
       if (value==2) 
       { 
       open("przepis2.txt"); 
       } 
       if (value==3) 
       { 
       open("przepis3.txt"); 
       } 


     } 
public void open(String name){ 
      File sdcard = Environment.getExternalStorageDirectory(); 

      //Get the text file 

      File file = new File(sdcard,name); 

      //Read text from file 
      StringBuilder text = new StringBuilder(); 

      try { 
       BufferedReader bufor = new BufferedReader(new FileReader(file)); 
       String line; 

       while ((line = bufor.readLine()) != null) { 
        text.append(line); 
        text.append('\n'); 
       } 
       bufor.close(); 
      } 
      catch (IOException e) { 
       //You'll need to add proper error handling here 
      } 

      //Find the view by its id 
      TextView tv = (TextView)findViewById(R.id.textView2); 

      //Set the text 
      tv.setText(text); 
     } 
     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
       // Inflate the menu; this adds items to the action bar if it is present. 
       getMenuInflater().inflate(R.menu.spinner, menu); 
       return true; 
     } 
     @Override 
     public boolean onOptionsItemSelected(MenuItem item) { 
       // Handle action bar item clicks here. The action bar will 
       // automatically handle clicks on the Home/Up button, so long 
       // as you specify a parent activity in AndroidManifest.xml. 
       int id = item.getItemId(); 
       if (id == R.id.action_settings) { 
         return true; 
       } 
       return super.onOptionsItemSelected(item); 
     } 
     @Override 
     public void onSensorChanged(SensorEvent event) { 
       // TODO Auto-generated method stub 

       //proxText.setText(String.valueOf(event.values[0])); 

     } 
     @Override 
     public void onAccuracyChanged(Sensor sensor, int accuracy) { 
       // TODO Auto-generated method stub 

     } 



} 

사람이보고 버그를 찾는 나를 도와? 감사 할 수

+0

정확히 어떤 문제가 있습니까? 예외가 있습니까? –

+0

가능한 [Android 안의 한 활동에서 다른 활동으로 데이터 전달] (http://stackoverflow.com/questions/5497502/passing-data-from-one-activity-to-another-in-android) –

+0

해야합니다. 각각의 의도에 intent.putExtra ("txt", (pos))를 덧붙여라. – kelvincer

답변

0
의 코드

위치 시작 양식 0, 문제가있는 것 같습니다. 다음과 같이 작성할 수 있습니다.

 public void onItemSelected(AdapterView<?> parent, View view, 
           int pos, long id) { 
     Intent intent = new Intent(MainActivity.this, SpinnerActivity.class); 
     intent.putExtra("txt", pos + 1); 
     startActivity(intent); 
    } 
+0

그 일은 나를 위해 일했지만 다른 문제가있다. 내 스피너에서 첫 번째 레코드를 선택하지 못한다. – guerillamac

+0

첫 번째 레코드는 회 전자를 채울 때 자동으로 선택되며 onItemSelected() 콜백 메서드는 새 위치가 이전 위치와 다른 경우에만 호출됩니다. [docs] (http://developer.android.com/reference/android/widget/AdapterView.OnItemSelectedListener.html#onNothingSelected%28android.widget.AdapterView%3C?%3E%29)에서 확인하십시오. –

관련 문제