Android開發(fā)之利用Intent實(shí)現(xiàn)數(shù)據(jù)傳遞的方法
本文實(shí)例講述了Android利用Intent實(shí)現(xiàn)數(shù)據(jù)傳遞的方法。分享給大家供大家參考,具體如下:
在Android開發(fā)過程中,很多人都熟悉Intent,這是個(gè)用于在多個(gè)View之間共享數(shù)據(jù)的類。本節(jié)主要講述通過點(diǎn)選ListView中的文本,把文本中的URL加載到一個(gè)新的頁面上,并且打印出來。為了方便,我先把前面一篇《Android開發(fā)之利用jsoup解析HTML頁面的方法》的代碼重新貼一下,因?yàn)樵谏弦还?jié)后,代碼做了少許修改:
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
}));
實(shí)現(xiàn)的效果如下:

然后我們需要做的就是當(dāng)點(diǎn)擊ListView中的項(xiàng)目的時(shí)候,程序會(huì)將每個(gè)話題下面的URL鏈接發(fā)送到新的頁面顯示:
下面是當(dāng)點(diǎn)擊ListView項(xiàng)目的時(shí)候,利用Intent傳遞數(shù)據(jù)的方法:
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);
}
}
當(dāng)然,一定不要忘記了在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" />
<!-- 加入訪問網(wǎng)絡(luò)的權(quán)限 -->
<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>
這樣當(dāng)一切工作準(zhǔn)備完畢后,運(yùn)行程序,點(diǎn)擊ListView 的Item,我們成功地跳轉(zhuǎn)到了子頁面:

以下是所有的源碼:
主頁面源碼:
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);
}
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android調(diào)試技巧與常見問題解決方法匯總》、《Android開發(fā)入門與進(jìn)階教程》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android入門之利用OKHttp實(shí)現(xiàn)斷點(diǎn)續(xù)傳功能
這篇文章主要為大家詳細(xì)介紹了Android如何使用OKHttp多線程制作像迅雷一樣的斷點(diǎn)續(xù)傳功能,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2023-01-01
Flutter 自定義Drawer 滑出位置的大小實(shí)例代碼詳解
這篇文章主要介紹了Flutter 自定義Drawer 滑出位置的大小,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
PullToRefreshListView實(shí)現(xiàn)多條目加載上拉刷新和下拉加載
這篇文章主要為大家詳細(xì)介紹了PullToRefreshListView實(shí)現(xiàn)多條目加載上拉刷新和下拉加載,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
Android webview 遇到android.os.FileUriExposedException錯(cuò)誤解決辦法
這篇文章主要介紹了Android webview 遇到android.os.FileUriExposedException錯(cuò)誤解決辦法的相關(guān)資料,希望通過本文能幫助到大家,讓大家遇到這樣的問題解決,需要的朋友可以參考下2017-10-10
Flutter進(jìn)階之實(shí)現(xiàn)動(dòng)畫效果(七)
這篇文章主要為大家詳細(xì)介紹了Flutter進(jìn)階之實(shí)現(xiàn)動(dòng)畫效果的第七篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Android使用LinearLayout設(shè)置邊框
這篇文章主要介紹了Android如何使用LinearLayout設(shè)置邊框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
Android實(shí)現(xiàn)自定義滑動(dòng)刻度尺方法示例
這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)自定義滑動(dòng)刻度尺的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04

