0

I am trying to pass a string with data that pulls from a database and I want to echo it into the body of a scrolling activity. I can get the sting to echo in the program I just do not know how pull it into different activities. This is the home screen of my App I am trying to build.This is my MainActivity.xml file and here is MainActivity.java code for that:

package com.example.it5.foothillers;

import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity implements        View.OnClickListener
{

Button button;
Button button2;
Button button3;

public TextView textViewData2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
        button = (Button)findViewById(R.id.button);
        button.setOnClickListener(this);



        textViewData2 = (TextView)findViewById(R.id.textViewData2);

        button2 = (Button)findViewById(R.id.button2);
        button2.setOnClickListener(this);

        button3 = (Button)findViewById(R.id.button3);
        button3.setOnClickListener(this);

}




private void buttonClick() {

  startActivity(new Intent("it5.foothillers.news"));

}
private void button2Click() {

    startActivity(new Intent("it5.foothillers.sports"));
}
private void button3Click() {
    startActivity(new Intent("it5.foothillers.events"));
}

@Override
public void onClick(View v) {

    switch (v.getId()){
        case R.id.button:
            new newsTask().execute("https://wordpress.org/plugins/about/readme.txt" +
                    "");
            buttonClick();
            break;
        case R.id.button2:
            button2Click();
            break;
        case R.id.button3:
            button3Click();
            break;

    }

}




public static void callBackDataFromAsyncTask(String result) {
}


private class newsTask extends AsyncTask<String, String, String>{


    @Override
    protected String doInBackground(String... params) {

        HttpURLConnection connection = null;
        BufferedReader reader = null;
        try {
            URL url = new URL(params[0]);

            connection = (HttpURLConnection) url.openConnection();
            connection.connect();

            InputStream stream = connection.getInputStream();

            reader = new BufferedReader(new InputStreamReader(stream));

            StringBuffer buffer = new StringBuffer();

            String line ="";

            while ((line = reader.readLine())!= null) {
                buffer.append(line);
            }
            String s = buffer.toString();
            System.out.println(s);
            return s;

            // return buffer.toString();

        }

        catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(connection != null)
                connection.disconnect();
            connection.disconnect();
            try {
                if(reader != null){
                    reader.close();
                }
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return null;
    }





}
// end of private class

}

It echos string 's' which is all the text from this link: http://www.komediagroup.com/clients.txt

The content of my news (content_news.xml) is this:

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/text_margin"
    android:text="@string/large_text" />

and my news.java file:

package com.example.it5.foothillers;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

public class news extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_news);
    ActionBar toolbar = getSupportActionBar();

}

}

It calls for the large text string in the strings.xml

When I click on 'News' it opens and my goal is for it to display text from a data base instead of "Test Test Test" pulled from my strings.xml which is this block of code:

<resources>
<string name="app_name">Foothillers</string>

<string name="title_activity_scrolling">ScrollingActivity</string>

<string name="large_text">

Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test 


</string>


<string name="action_settings">Settings</string>

<string name="title_activity_news">News</string>

<string name="title_activity_events">Events</string>

I wanted to replace "Test Test Test" with the data from the link from komediagroup

1 Answers1

0

See this page on Intents on Android Developers.

What you want to do is pass a String extra to the called Activity. You do this by calling the Intent#putExtra(String name, String value) method before calling startActivity.

Then, in the called activity, retrieve the intent that was used to call it with getIntent(), and retrieve the String extra with Intent#getStringExta(String name).

KevinOrr
  • 1,663
  • 3
  • 13
  • 24