Note that there are some explanatory texts on larger screens.

plurals
  1. POStarting a new activity, and transferring a string to said activity
    text
    copied!<p>I'm a self-taught, high school android programmer so please bear with me.</p> <p>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.</p> <p>start.java - the first activity.</p> <pre><code>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 = "&amp;f=musifin2125&amp;k=mjjlnzkyzwuz&amp;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; } } } </code></pre> <p>Main.java - the second activity</p> <pre><code>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&lt;HashMap&lt;String, String&gt;&gt; mylist = new ArrayList&lt;HashMap&lt;String, String&gt;&gt; (); JSONObject json = JSONfunctions.getJSONfromURL("//Saved string goes here"); try{ JSONObject earthquakes = json.getJSONObject("Similar"); JSONArray info = earthquakes.getJSONArray("Results"); for (int i = 0; i &lt; info.length(); i++) { HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); 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&lt;results.length();i++){ HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); 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&lt;?&gt; parent, View view, int position, long id) { @SuppressWarnings("unchecked") HashMap&lt;String, String&gt; o = (HashMap&lt;String, String&gt;) lv.getItemAtPosition(position); Toast.makeText(Main.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show(); } }); } } </code></pre> <p>Lastly, my AndroidManifest.xml file.</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.amrit.musifind" android:versionCode="1" android:versionName="1.0"&gt; &lt;uses-sdk android:minSdkVersion="4" /&gt; &lt;application android:icon="@drawable/icon" android:label="@string/app_name"&gt; &lt;activity android:name=".start" android:label="@string/app_name"&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;activity android:name= ".Main" android:label="@string/app_name"&gt; &lt;intent-filter&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;/application&gt; &lt;uses-permission android:name="android.permission.INTERNET" /&gt; &lt;/manifest&gt; </code></pre> <p>Thanks in advance!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload