C#獲得法式文件相干信息的辦法。本站提示廣大學習愛好者:(C#獲得法式文件相干信息的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#獲得法式文件相干信息的辦法正文
本文實例講述了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法式設計有所贊助。