在AndroID开发过程中,很多人都熟悉Intent,这是个用于在多个VIEw之间共享数据的类。本节主要是继承上节,通过点选ListVIEw中的文本,把文本中的URL加载到一个新的页面上,并且打印出来。为了方便,我先把上一节的代码重新贴一下,因为在上一节后,代码做了少许修改:
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开发探秘之四:利用Intent实现数据传递全部内容,希望文章能够帮你解决Android开发探秘之四:利用Intent实现数据传递所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)