如何使用TabChange上的TabHost将数据从listActivity传递到ListActivity

时间:2012-08-03 11:16:17

标签: java android android-activity android-tabhost listactivity

我有问题将数据传递给另一个活动...... 我在大班使用TabHost我有 3-Tabb 1(电台)2(电台)3(最喜欢的)。 我想将数据从stationActivity传递给RadioActivity On setOnItemClickListener。我使用的是Intnet,但Intent无效。请给我代码...... 或者如何将Data TabHost传递给TabHOst。

这是我主要的TabHost     package com.example.androidhive;

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class Main extends TabActivity {
    // TabSpec Names
    private static final String TAB_STATION = "station";
    private static final String TAB_FAVORITE = "favorite";
    private static final String TAB_RADIO = "radio";
    private static TabHost tabHost;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tabHost = getTabHost();

        // Station Tab
        TabSpec StationSpec = tabHost.newTabSpec(TAB_STATION);
        // Tab Icon
        StationSpec.setIndicator("Station", getResources().getDrawable(R.drawable.icon_radio));
        Intent StationIntent = new Intent(this, StationActivity.class);
        // Tab Content
        StationSpec.setContent(StationIntent);




        // Radio Tab
        TabSpec RadioSpec = tabHost.newTabSpec(TAB_RADIO);
        RadioSpec.setIndicator("Radio", getResources().getDrawable(R.drawable.icon_station));
        Intent RadioIntent = new Intent(this, RadioActivity.class);
        RadioSpec.setContent(RadioIntent);

        // Favorite Tab
        TabSpec FavoriteSpec = tabHost.newTabSpec(TAB_FAVORITE);
        FavoriteSpec.setIndicator("Favorite", getResources().getDrawable(R.drawable.icon_favorite));
        Intent FavoriteIntent = new Intent(this, FavoriteActivity.class);
        FavoriteSpec.setContent(FavoriteIntent);

        // Adding all TabSpec to TabHost
        tabHost.addTab(StationSpec); // Adding station tab
        tabHost.addTab(RadioSpec); // Adding Radio tab
        tabHost.addTab(FavoriteSpec); // Adding Favorite tab
    }


    public void SetTab(int i)
    {
        tabHost.setCurrentTab(i);
    }
}

电台活动

package com.example.androidhive;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.R.string;
import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class StationActivity extends ListActivity {
    // Progress Dialog
    private ProgressDialog pDialog;

    // Creating JSON Parser object
    JSONParser jsonParser = new JSONParser();

    ArrayList<HashMap<String, String>> inboxList;

    // products JSONArray
    JSONArray inbox = null;

    // Inbox JSON url
    private static final String INBOX_URL = "http://bolanhost.com/livestream/get_all_products.php";

    // ALL JSON node names
    private static final String TAG_PRODUCT = "products";
    private static final String TAG_Name = "name";
    private static final String TAG_URL = "url";
    private static final String TAG_WEBSITE = "web";
    private static final String TAG_GENER = "gener";
    private static final String TAG_COUNTRY = "country";
    private static final String TAG_LANGUAGE = "language";
    public String txtname;
    private String txtgener;
    private String txtwebsite;
    private String txtlanaguage;
    private String txtcountry;
    private String txtUrl;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.inbox_list);

        // Hashmap for ListView
        inboxList = new ArrayList<HashMap<String, String>>();

        // Loading INBOX in Background Thread
        new LoadInbox().execute();
    }

    /**
     * Background Async Task to Load all INBOX messages by making HTTP Request
     * */
    class LoadInbox extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(StationActivity.this);
            pDialog.setMessage("Loading Inbox ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        /**
         * getting Inbox JSON
         * */
        protected String doInBackground(String... args) {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();

            // getting JSON string from URL
            JSONObject json = jsonParser.makeHttpRequest(INBOX_URL, "GET",
                    params);

            // Check your log cat for JSON reponse
            Log.d("Inbox JSON: ", json.toString());

            try {
                inbox = json.getJSONArray(TAG_PRODUCT);
                // looping through All messages
                for (int i = 0; i < inbox.length(); i++) {
                    JSONObject c = inbox.getJSONObject(i);

                    // Storing each json item in variable
                    String name = c.getString(TAG_Name);
                    String language = c.getString(TAG_LANGUAGE);
                    String website = c.getString(TAG_WEBSITE);
                    String gener = c.getString(TAG_GENER);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_Name, name);
                    map.put(TAG_LANGUAGE, language);
                    map.put(TAG_WEBSITE, website);
                    map.put(TAG_GENER, gener);

                    // adding HashList to ArrayList
                    inboxList.add(map);
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    ListAdapter adapter = new SimpleAdapter(
                            StationActivity.this, inboxList,
                            R.layout.station, new String[] {TAG_Name,TAG_GENER,TAG_LANGUAGE,TAG_WEBSITE,TAG_URL,TAG_COUNTRY},
                            new int[] { R.id.name,R.id.gener , R.id.lang, R.id.web,R.id.url,R.id.country });
                    // updating listview
                    setListAdapter(adapter);
                }
            });


            final ListView lv = getListView();
            lv.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView<?> parent, View view,int position, long id){
                    txtname = ((TextView) view.findViewById(R.id.name)).getText().toString();
                    txtgener = ((TextView) view.findViewById(R.id.gener)).getText().toString();
                    txtwebsite = ((TextView) view.findViewById(R.id.web)).getText().toString();
                    txtlanaguage = ((TextView) view.findViewById(R.id.lang)).getText().toString();
                    txtcountry = ((TextView) view.findViewById(R.id.country)).getText().toString();
                    txtUrl = ((TextView) view.findViewById(R.id.url)).getText().toString();


                    Main m = new Main();
                    m.SetTab(1);
                }
            });



        }




    }


}

放射性

package com.example.androidhive;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class RadioActivity extends Activity {
    public String TextName;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.radio);       


        Context context = getApplicationContext();
        CharSequence text = value;
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();


        //if variables Null then Diplay POp Message...



        //TextView Instance 

        TextView SName = (TextView) findViewById(R.id.station_name);
        TextView SGener = (TextView) findViewById(R.id.station_gener);
        TextView SWeb = (TextView) findViewById(R.id.station_website);
        TextView SLang = (TextView) findViewById(R.id.station_lang);
        TextView SUrl = (TextView) findViewById(R.id.station_Url);
        TextView SCountry = (TextView) findViewById(R.id.station_country);

    }

}

0 个答案:

没有答案