做这个的时候才发现自己java的一些基础知识掌握的不够,所以这个过程学到了很多东西。比如私有变量、成员变量,即java如何设置可以被整个项目引用的变量。除了这些还学会了如何使用Android studio的listView控件,和如何抓取网页数据。感觉自己以前学习的数组知识有重新温习了一编,以前学的是C,C#,和java还是有很多不同的。总之,这个小功能使我面对java更加的有自信。
一、定义可供整个项目调用的二维数组变量
public class Common {
private static String[][] testStr2;
public static String[][] getTestStr2()
{return testStr2;}
public void setTestStr2(String[][] b)
{ this.testStr2=b; }
}
二、Showview.class
import android.app.ListActivity;
import android.os.Bundle;
import android.os.StrictMode;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
public class Showview extends ListActivity {
String[] from = {"name", "id"}; //这里是ListView显示内容每一列的列名
int[] to = {R.id.user_name, R.id.user_id}; //这里是ListView显示每一列对应的list_item中控件的id
String[] userId = {"1001", "1002", "1003", "1004"}; //这里是人名对应的ID
static String[] testStr1;
ArrayList<HashMap<String, String>> list = null;
HashMap<String, String> map = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listmain); //为MainActivity设置主布局
//创建ArrayList对象;
list = new ArrayList<HashMap<String, String>>();
T1=(TextView)findViewById(R.id.textView) ;
//将数据存放进ArrayList对象中,数据安排的结构是,ListView的一行数据对应一个HashMap对象,
//HashMap对象,以列名作为键,以该列的值作为Value,将各列信息添加进map中,然后再把每一列对应
//的map对象添加到ArrayList中
Showmain.main(null);
testStr1=ToOne(Common.getTestStr2());
for (int i = 0; i < 4; i++) {
map = new HashMap<String, String>(); //为避免产生空指针异常,有几列就创建几个map对象
map.put("id", userId[i]);
map.put("name",testStr1[i]);
list.add(map);
}
//创建一个SimpleAdapter对象
SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.list_item, from, to);
//调用ListActivity的setListAdapter方法,为ListView设置适配器
setListAdapter(adapter);
}
public static String[] ToOne(String[][] a2) {
int a = a2.length;
String a1[];
a1 = new String[a];
for (int i = 0; i < a2.length; i++) {
a1[i] = a2[i][0];
}
return a1;
}
}
三、Showmain.class
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
public class Showmain {
public static String[][] Str(){
try {
Document doc = null;
doc = Jsoup.connect("http://********.jsp").get();
Elements trs = doc.select("table").select("tr");
Elements td11 = trs.get(0).select("td");
String[][] testStr22=new String[trs.size()][td11.size()];
for (int i = 0; i < trs.size(); i++) {
Elements tds = trs.get(i).select("td");
for (int j = 0; j < tds.size(); j++) {
String text = tds.get(j).text();
//二维数组
testStr22[i][j] = text;
}
}
return testStr22;
} catch (Exception e) {
String [][]testStr2={{"读取失败1","w"},{"读取失败2","222"},{"读取失败3","222"},{"读取失败4","222"}};
e.printStackTrace();
return testStr2;
}
}
public static void main(String[] args) {
String[][] testStr22;
testStr22= Str();
int a=testStr22.length;
int c=testStr22[0].length;
Common m=new Common();
m.setTestStr2(testStr22);
for(int i=0;i<a;i++)
for (int j = 0; j <c; j++) {
// Common.testStr2=new String[a][c];
//Common.testStr2[i][j]=testStr22[i][j];
System.out.print( Common.getTestStr2()[i][j]+"\n");
}
}
}
【注】:如果模拟器上 ,或logcat中查看到了Caused by: android.os.NetworkOnMainThreadException错误 可以在Showview.class的setContentView(R.layout.activity_listmain)后加入以下代码
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
详情:http://stackoverflow.com/questions/13136539/caused-by-android-os-networkonmainthreadexception