启动新活动,并将字符串传输到所述活动

时间:2011-08-02 03:31:41

标签: android string android-activity android-intent

我是一名自学成才的高中程序员,所以请耐心等待。

我正在开发一个应用程序,我希望将用户输入文本放入文本框中,一旦用户点击“go”按钮,文本就会保存为字符串。在此之后,应用程序应该启动下一个活动并将字符串转移到该活动。我一直在搞乱意图,但我没有到达任何地方。这是我的代码。

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>

提前致谢!

3 个答案:

答案 0 :(得分:4)

要保存网址:

input = EditTextinput.getText().toString();
                            url = ur + input + l  ;
                            Intent myIntent = new Intent(start.this,  Main.class);
myIntent.putExtra("URL", url);
                            start.this.startActivity(myIntent);

                            startActivity(myIntent);

要检索它:

JSONObject json = JSONfunctions.getJSONfromURL(getIntent().getStringExtra("URL"));

答案 1 :(得分:0)

您必须在触发活动的意图中添加额外内容。

请参阅此SO问题以获取代码示例Start an Activity with a parameter

答案 2 :(得分:0)

嗯,你可以随时这样做:

on Start.java

static public String ur = "http://www.tastekid.com/ask/ws?q=";
static public String l = "&f=musifin2125&k=mjjlnzkyzwuz&format=JSON";
static public String url = "" ;
Main.java上的

String url = Start.url;
JSONObject json = JSONfunctions.getJSONfromURL(url);