Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7743465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:32:32+00:00 2026-06-01T09:32:32+00:00

I am having trouble passing data read from a text file to another class

  • 0

I am having trouble passing data read from a text file to another class in the app. I can read from the file just fine, but I think I need to use Bundle, but I’m not sure how to do it. I would like to have the second class handle the data from the text file, and then display it in the first class.

Edit: I’ve figured out how to pass the string from the file using an intent. I’m still working on getting some of the bugs out.

2nd Edit: I know there is a more efficient way of doing this. The only way I can get it to work is to have the first button in MainActivity use startActivity(intent) to allow secondActivity to bundle the string read from the file.

MainActivity.java:

public class MainActivity extends Activity {

    Button btn;
    Button bReadFile;
    TextView tvRead;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn = (Button) findViewById(R.id.btnNext);
        bReadFile = (Button) findViewById(R.id.btnRead);
        tvRead = (TextView) findViewById(R.id.tvMain);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //trying to find a way to remove this button
                Intent intent = new Intent(MainActivity.this, econdActivity.class);
                startActivity(intent);
            }
        });

        bReadFile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String value = getIntent().getExtras().getString("key");
                tvRead.setText(value);
            }
        });
    }
}

secondActivity.java:

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

        Intent mIntent = new Intent(secondActivity.this, MainActivity.class);
        mIntent.putExtra("key", readDataFile());
        startActivity(mIntent);
    }

    public String readDataFile() {
        String strData = null;
        InputStream is = null;
        try {
            is = getResources().openRawResource(R.raw.data_text);
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            strData = br.readLine();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return strData;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:32:33+00:00Added an answer on June 1, 2026 at 9:32 am

    Use the below edited code for your requirement
    MainActivity.java

    public class MainActivity extends Activity {
    
        Button btn;
        Button bReadFile;
        TextView tvRead;
        String value;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            btn = (Button) findViewById(R.id.btnNext);
            bReadFile = (Button) findViewById(R.id.btnRead);
            tvRead = (TextView) findViewById(R.id.tvMain);
    
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    //trying to find a way to remove this button
                    Intent intent = new Intent(MainActivity.this, secondActivity.class);
                    startActivityForResults(intent,0);
                }
            });
    
            bReadFile.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    tvRead.setText(value);
                }
            });
        }
        @Override
       protected void onActivityResult(int requestCode, int resultCode, Intent data){
           value = data.getStringExtra("key");
    }
    }
    

    the code for secondActivity.java

    public class secondActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Intent i = new Intent();
        i.putExtra("key", readDataFile());
        setResult(RESULT_OK, i);
        finish();
        }
    
        public String readDataFile() {
            String strData = null;
            InputStream is = null;
            try {
                is = getResources().openRawResource(R.raw.data_text);
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                strData = br.readLine();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return strData;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble passing data from one activity to another. I can easily
CakePHP Newbie :) I am having trouble accessing another controller and passing that data
I am having trouble passing data retrieved from a $.post() function to use in
I'm having trouble passing a variable defined in one class instance to another class
I am having trouble nailing down the best method for passing data from a
I'm having some trouble using XmlReader to read an XML file. I can open
I'm not sure why, but I'm having trouble passing a data object (NSManagedObject) to
i'm still transitioning from as2 to as3, i'm having trouble with parsing XML data
I'm having trouble loading data from sqlite database into a detailed view. The table
I'm working on a JSF 2.0 web app, and I'm having trouble passing in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.