启动一个新活动,并将字符串传输到该活动
我是一名自学成才的高中 Android 程序员,所以请耐心等待。
我正在开发一个应用程序,我希望用户在文本框中输入文本,一旦用户点击“开始”按钮,文本就会保存到字符串中。之后,应用程序应该启动下一个活动并将字符串传输到该活动。我一直在搞乱意图,但我一无所获。这是我的代码。
start.java - 第一个活动。
package com.amrit.musifind;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import com.amrit.musifind.R;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class start extends Activity implements OnClickListener {
Button buttonGo, buttonReset;
EditText EditTextinput;
String input = "";
String ur = "http://www.tastekid.com/ask/ws?q=";
String l = "&f=musifin2125&k=mjjlnzkyzwuz&format=JSON";
String url = "" ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonGo = (Button) findViewById(R.id.buttonGo);
buttonReset = (Button) findViewById(R.id.buttonReset);
EditTextinput = (EditText) findViewById(R.id.EditTextinput);
//Button listener
buttonReset.setOnClickListener(this);
buttonGo.setOnClickListener(this);
}
public void onClick (View src){
switch(src.getId()){
case R.id.buttonGo:
input = EditTextinput.getText().toString();
url = ur + input + l ;
Intent myIntent = new Intent(start.this,
Main.class);
start.this.startActivity(myIntent);
startActivity(myIntent);
break;
case R.id.buttonReset:
EditTextinput.setText("");
break;
}
}
}
Main.java - 第二个活动
package com.amrit.musifind;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import com.amrit.musifind.JSONfunctions;
import com.amrit.musifind.R;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Main extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>
();
JSONObject json = JSONfunctions.getJSONfromURL("//Saved string goes here");
try{
JSONObject earthquakes = json.getJSONObject("Similar");
JSONArray info = earthquakes.getJSONArray("Results");
for (int i = 0; i < info.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = info.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("name", "Name:" + e.getString("Name"));
map.put("type", "Type: " + e.getString("Type"));
mylist.add(map);
}
JSONArray results = json.getJSONArray("Results");
for(int i=0;i<results.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = results.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("name", "Name:" + e.getString("name"));
map.put("type", "Type: " + e.getString("type"));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main,
new String[] { "name", "type" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long
id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>)
lv.getItemAtPosition(position);
Toast.makeText(Main.this, "ID '" + o.get("id") + "' was clicked.",
Toast.LENGTH_SHORT).show();
}
});
}
}
最后是我的 AndroidManifest.xml 文件。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.amrit.musifind"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".start" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name= ".Main" android:label="@string/app_name">
<intent-filter>
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
提前致谢!
I'm a self-taught, high school android programmer so please bear with me.
I am developing an app in which I want to have the user input text into a text box, and once the user hits the "go" button, the text is saved to a string. Right after that, the app should start the next activity and transfer the string over to that activity. I've been messing with intents, but I'm not getting anywhere. Here's my code.
start.java - the first activity.
package com.amrit.musifind;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import com.amrit.musifind.R;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class start extends Activity implements OnClickListener {
Button buttonGo, buttonReset;
EditText EditTextinput;
String input = "";
String ur = "http://www.tastekid.com/ask/ws?q=";
String l = "&f=musifin2125&k=mjjlnzkyzwuz&format=JSON";
String url = "" ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonGo = (Button) findViewById(R.id.buttonGo);
buttonReset = (Button) findViewById(R.id.buttonReset);
EditTextinput = (EditText) findViewById(R.id.EditTextinput);
//Button listener
buttonReset.setOnClickListener(this);
buttonGo.setOnClickListener(this);
}
public void onClick (View src){
switch(src.getId()){
case R.id.buttonGo:
input = EditTextinput.getText().toString();
url = ur + input + l ;
Intent myIntent = new Intent(start.this,
Main.class);
start.this.startActivity(myIntent);
startActivity(myIntent);
break;
case R.id.buttonReset:
EditTextinput.setText("");
break;
}
}
}
Main.java - the second activity
package com.amrit.musifind;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import com.amrit.musifind.JSONfunctions;
import com.amrit.musifind.R;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Main extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>
();
JSONObject json = JSONfunctions.getJSONfromURL("//Saved string goes here");
try{
JSONObject earthquakes = json.getJSONObject("Similar");
JSONArray info = earthquakes.getJSONArray("Results");
for (int i = 0; i < info.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = info.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("name", "Name:" + e.getString("Name"));
map.put("type", "Type: " + e.getString("Type"));
mylist.add(map);
}
JSONArray results = json.getJSONArray("Results");
for(int i=0;i<results.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = results.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("name", "Name:" + e.getString("name"));
map.put("type", "Type: " + e.getString("type"));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main,
new String[] { "name", "type" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long
id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>)
lv.getItemAtPosition(position);
Toast.makeText(Main.this, "ID '" + o.get("id") + "' was clicked.",
Toast.LENGTH_SHORT).show();
}
});
}
}
Lastly, my AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.amrit.musifind"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".start" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name= ".Main" android:label="@string/app_name">
<intent-filter>
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
保存 URL:
检索 URL:
To save the URL:
To retrieve it:
您必须在触发活动的意图中添加额外内容。
请参阅此问题以获取代码示例使用参数启动活动
You have to put an extra in the intent that triggers the activity.
Refer to this SO question for a code example Start an Activity with a parameter
好吧,你总是可以这样做:
在 Start.java 上
在 Main.java 上
Well, you can always do this:
on Start.java
on Main.java