Android开发之利用Intent实现数据传递的方法
程序员文章站
2024-02-26 17:19:55
本文实例讲述了android利用intent实现数据传递的方法。分享给大家供大家参考,具体如下:
在android开发过程中,很多人都熟悉intent,这是个用于在多个v...
本文实例讲述了android利用intent实现数据传递的方法。分享给大家供大家参考,具体如下:
在android开发过程中,很多人都熟悉intent,这是个用于在多个view之间共享数据的类。本节主要讲述通过点选listview中的文本,把文本中的url加载到一个新的页面上,并且打印出来。为了方便,我先把前面一篇《android开发之利用jsoup解析html页面的方法》的代码重新贴一下,因为在上一节后,代码做了少许修改:
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中的项目的时候,程序会将每个话题下面的url链接发送到新的页面显示:
下面是当点击listview项目的时候,利用intent传递数据的方法:
listview.setonitemclicklistener(new onitemclicklistener(){ @override public void onitemclick(adapterview<?> parent, view v, int position,long id) { //toast.maketext(getapplicationcontext(), (textview), duration) 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); } });
在子页面“topicdetails.java”中,我们可以通过如下的方式来接收传递过来的值:
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); } }
当然,一定不要忘记了在androidmanifest.xml文件中添加activity映射(黄色背景部分):
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.web" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="7" /> <!-- 加入访问网络的权限 --> <uses-permission android:name="android.permission.internet" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name="._getwebresoureactivity" > <intent-filter > <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".topicdetails"></activity> </application> </manifest>
这样当一切工作准备完毕后,运行程序,点击listview 的item,我们成功地跳转到了子页面:
以下是所有的源码:
主页面源码:
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<?> parent, view v, int position,long id) { //toast.maketext(getapplicationcontext(), (textview), duration) 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); } }); } /** * @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 ""; } } }
子页面源码:
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); } }
更多关于android相关内容感兴趣的读者可查看本站专题:《android调试技巧与常见问题解决方法汇总》、《android开发入门与进阶教程》、《android多媒体操作技巧汇总(音频,视频,录音等)》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。