Slicekick
Slicekick

Reputation: 2159

Starting a new activity, and transferring a string to said activity

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!

Upvotes: 3

Views: 1032

Answers (3)

emmandroid
emmandroid

Reputation: 821

Well, you can always do this:

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 = "" ;

on Main.java

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

Upvotes: 0

Femi
Femi

Reputation: 64700

To save the URL:

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);

To retrieve it:

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

Upvotes: 4

aromero
aromero

Reputation: 25761

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

Upvotes: 0

Related Questions