Sendind data from one activity to another

Hi i’m new here and firstly i want to apologise for my language .I wanted to create my first android app ,and im curently struggle with some issues. MainActivity ,which represents my main screen containing two buttons and spinner.In spinner user can choose one text file and after selection spinnerActivity should be triggered(screen with opened text file).I found problem because not every record from spinner trigger activity with text file that i want (one trigger file that i want and other trigger screen with empty TextView). Here is layout of 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>

Code of 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);
                }
}

and the code of 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

        }



}

Can someone look at this and help me with finding bugs?Thanks

Answer

Position starts form 0, I think problem is with the conditions, You can write like this-

        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);
    }

Leave a Reply

Your email address will not be published. Required fields are marked *