Android development using intent to implement data transfer method _android

Source: Internet
Author: User

This example describes the way Android uses intent to implement data transfer. Share to everyone for your reference, specific as follows:

In the Android development process, many people are familiar with intent, a class that is used to share data among multiple view. This section focuses on selecting the text in ListView, loading the URL in the text into a new page, and printing it. For convenience, I'll start by pasting the code from the previous "Android development using Jsoup parsing HTML page", because the code made a few changes in the previous section:

try {doc = Jsoup.parse (new URL ("http://www.51yam.com"), 5000);
    catch (Malformedurlexception E1) {e1.printstacktrace ();
    catch (IOException E1) {e1.printstacktrace ();
    Final list<map<string, string>> List = new arraylist<map<string, string>> ();
    Elements es = Doc.getelementsbyclass ("Subnav");
      for (int i=0;i<es.size (); i++) {Element e = es.get (i);
      int count = E.getelementsbytag ("a"). Size ();
        for (int j=0;j<count;j++) {map<string, string> Map = new hashmap<string, string> ();
        Element ex = E.getelementsbytag ("a"). Get (j);
        Map.put ("title", Ex.text ());
        Map.put ("href", "http://www.51yam.com/" +ex.attr ("href"));
      List.add (map);
    } ListView ListView = (ListView) Findviewbyid (R.ID.LISTVIEW1); Listview.setadapter New Simpleadapter (this, list, Android.
 R.layout.simple_list_item_2, new string[] {"title", "href"}, new int[] {       Android. R.id.text1,android.

 R.ID.TEXT2}));

The effect of the implementation is as follows:

And then what we need to do is when you click on the item in ListView, the program sends the URL link below each topic to the new page display:

Here's how to use intent to pass data when you click on a ListView project:

Listview.setonitemclicklistener (New Onitemclicklistener () {
  @Override public
  void Onitemclick (Adapterview <?> Parent, View v, int position,long id) {
    //toast.maketext (Getapplicationcontext (), (TextView), duration) C4/>system.out.println ("Position:" +position);
    SYSTEM.OUT.PRINTLN ("ID:" +id);
    Toast.maketext (_getwebresoureactivity.this, list.get (position). Get ("href"), toast.length_short). Show ();
    Intent Intent = new Intent (V.getcontext (), topicdetails.class);
    Intent.putextra ("src", list.get (position). Get ("href"));
    Startactivityforresult (intent,0);
  }
);

In the sub page "Topicdetails.java", we can receive the value passed through in the following way:

Package com.android.web;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.widget.EditText;
Import Java.lang.Object;
public class Topicdetails extends activity {
  private edittext edittext;
  @Override
  protected void onCreate (Bundle savedinstancestate)
  {
    super.oncreate (savedinstancestate);
    Setcontentview (r.layout.topiccontent);
    EditText = (edittext) This.findviewbyid (r.id.edittext);
    String Srcurl = Getintent (). Getstringextra ("src");
    Edittext.settext (Srcurl);
  }


Sure, don't forget to add an activity map (yellow background) in the Androidmanifest.xml file:

 <?xml version= "1.0" encoding= "Utf-8"?> <manifest "xmlns:android=" Schemas.android.com/apk/res/android "package=" Com.android.web "android:versioncode=" 1 "android:versionname=" 1.0 "&
  Gt <USES-SDK android:minsdkversion= "7"/> <!--join access to the network--> <uses-permission android:name= "Android.permi Ssion. INTERNET "/> <application android:icon=" @drawable/ic_launcher "android:label=" @string/app_name "> & Lt;activity android:label= "@string/app_name" android:name= "_getwebresoureactivity" > <intent-filt er > <action android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.inte Nt.category.LAUNCHER "/> </intent-filter> </activity> <activity android:name=". Topicdetail S "></activity> </application> </manifest> 

So when all the work is ready, run the program, click ListView Item, we successfully jump to the sub page:

The following are all the source code:

Main Page Source:

Package com.android.web;
Import Java.io.BufferedInputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.net.MalformedURLException;
Import Java.net.URL;
Import java.net.URLConnection;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import Java.lang.Object;
Import Org.apache.http.util.ByteArrayBuffer;
Import Org.apache.http.util.EncodingUtils;
Import Org.jsoup.Jsoup;
Import org.jsoup.nodes.Document;
Import org.jsoup.nodes.Element;
Import org.jsoup.select.Elements;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.AdapterView;
Import Android.widget.AdapterView.OnItemClickListener;
Import Android.widget.ListView;
Import Android.widget.SimpleAdapter;
Import Android.widget.Toast;
  public class _getwebresoureactivity extends activity {Document doc; @Override public void OnCreate (Bundle sAvedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
        Findviewbyid (R.id.button1). Setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {
      Load ();
  }
    });
    protected void Load () {try {doc = Jsoup.parse (new URL ("http://www.51yam.com"), 5000);
    catch (Malformedurlexception E1) {e1.printstacktrace ();
    catch (IOException E1) {e1.printstacktrace ();
    Final list<map<string, string>> List = new arraylist<map<string, string>> ();
    Elements es = Doc.getelementsbyclass ("Subnav");
      for (int i=0;i<es.size (); i++) {Element e = es.get (i);
      int count = E.getelementsbytag ("a"). Size ();
        for (int j=0;j<count;j++) {map<string, string> Map = new hashmap<string, string> ();
        Element ex = E.getelementsbytag ("a"). Get (j);
        Map.put ("title", Ex.text ()); Map.put ("href","http://www.51yam.com/" +ex.attr ("href"));
      List.add (map);
    } ListView ListView = (ListView) Findviewbyid (R.ID.LISTVIEW1); Listview.setadapter New Simpleadapter (this, list, Android. R.layout.simple_list_item_2, new string[] {"title", "href"}, new int[] {Android. R.id.text1,android.
    R.ID.TEXT2})); Listview.setonitemclicklistener (New Onitemclicklistener () {@Override public void Onitemclick (adapterview<?& Gt Parent, View v, int position,long id) {//toast.maketext (Getapplicationcontext (), (TextView), duration) Sy
        Stem.out.println ("Position:" +position);
        SYSTEM.OUT.PRINTLN ("ID:" +id);
        Toast.maketext (_getwebresoureactivity.this, list.get (position). Get ("href"), toast.length_short). Show ();
        Intent Intent = new Intent (V.getcontext (), topicdetails.class);
        Intent.putextra ("src", list.get (position). Get ("href"));
      Startactivityforresult (intent,0);
  }
    }); }/** * @paraM urlstring * @return/public string gethtmlstring (string urlstring) {try {URL url = null;
      url = new URL (urlstring);
      URLConnection Ucon = null;
      Ucon = Url.openconnection ();
      InputStream instr = null;
      InStr = Ucon.getinputstream ();
      Bufferedinputstream bis = new Bufferedinputstream (instr);
      Bytearraybuffer BAF = new Bytearraybuffer (500);
      int current = 0;
      while (current = Bis.read ())!=-1) {Baf.append ((byte) current);
    Return encodingutils.getstring (Baf.tobytearray (), "GBK");
    catch (Exception e) {return "";

 }
  }
}

Sub-page source code:

Package com.android.web;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.widget.EditText;
Import Java.lang.Object;
public class Topicdetails extends activity {
  private edittext edittext;
  @Override
  protected void onCreate (Bundle savedinstancestate)
  {
    super.oncreate (savedinstancestate);
    Setcontentview (r.layout.topiccontent);
    EditText = (edittext) This.findviewbyid (r.id.edittext);
    String Srcurl = Getintent (). Getstringextra ("src");
    Edittext.settext (Srcurl);
  }


For more information on Android-related content readers can view the site topics: "Android Debugging techniques and common problems solution summary", "Android Development introduction and Advanced Course", "Android Multimedia operating skills Summary (audio, video, recording, etc.)", " Android Basic Components Usage Summary, Android View tips Summary, Android layout layout tips and Android Control usage summary

I hope this article will help you with the Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.