我正在尝试传递一个字符串,其中包含从数据库中提取的数据,我希望将其回显到滚动活动的主体中。我可以在程序中得到回应,我只是不知道如何将它拉入不同的活动中。 This is the home screen of my App I am trying to build.这是我的MainActivity.xml文件,这里是 MainActivity.java 代码:
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
}
回声字符串's'是此链接中的所有文字:http://www.komediagroup.com/clients.txt
我的新闻内容( content_news.xml )是这样的:
<?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" />
和我的 news.java 文件:
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();
}
}
它要求strings.xml中的大文本字符串
当我点击“新闻”时,它会打开,我的目标是显示来自数据库的文本,而不是从我的 strings.xml 中取出的“测试测试”,这是代码:
<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>
我想用来自komediagroup
的链接中的数据替换“Test Test Test”答案 0 :(得分:0)
请参阅Android开发者上的this page on Intents。
您要做的是将String extra传递给被调用的Activity。您可以在调用startActivity
之前调用Intent#putExtra(String name, String value)
方法执行此操作。
然后,在被调用的活动中,检索用getIntent()
调用它的意图,并使用Intent#getStringExta(String name)
检索字符串extra。