public class MainActivity extends AppCompatActivity {
ListView lvs;
Map<String,Object> map3;
List<Map<String,Object>> itmes = new ArrayList<>();
private List<News> newsList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvs = MainActivity.this.findViewById(R.id.lv); //獲取ListView控制元件
NewsAdapter adapter = new NewsAdapter(MainActivity.this,R.layout.lv2,newsList);
/* ListView listView = findViewById(R.id.lv);
listView.setAdapter(adapter);*/
btn();
}
//定義按鈕方法 點擊那個按鈕 獲得選中的url 賦值給download()方法
public static String url ;
public void btn( ){ //國內新聞(默認顯示)
itmes.clear();
url="http://www.people.com.cn/rss/politics.xml";
download();
};
public void btn1(View view){ //國內新聞
itmes.clear();
url="http://www.people.com.cn/rss/politics.xml";
download();
};
public void btn2(View view){ //體育新聞
itmes.clear();
url="http://www.people.com.cn/rss/sports.xml";
download();
//"http://www.people.com.cn/rss/haixia.xml"
};
public void btn3(View view){ //臺灣新聞
itmes.clear();
url="http://www.people.com.cn/rss/haixia.xml";
download();
};
Handler handler = new Handler(){//創建一個Android訊息處理機制 重寫handleMessage方法
@Override
public void handleMessage(Message msg) {//處理訊息的方法,使用handleMessage去處理訊息,里面的引數Message
//message物件可以接受任何型別的物件 并且可以通過反向強制轉換 轉換回來
String title = ((News)msg.obj).getTitle();
String pubDate = ((News)msg.obj).getPubDate();
String author = ((News)msg.obj).getAuthor();
String description = ((News)msg.obj).getDescription();
map3 = new HashMap<>();//創建map 物件進行復制傳遞
map3.put("title",title);
map3.put("pubDate",pubDate);
map3.put("author",author);
map3.put("description",description);
itmes.add(map3); //將map物件放進 list集合里面
lvs.setAdapter(new SimpleAdapter(MainActivity.this,itmes,R.layout.lv2,new String[]{"title","pubDate","author"}
,new int[]{R.id.txt_count,R.id.txt_Date,R.id.txt_author}));
lvs.setOnItemClickListener((parent, view, position, id) -> {
News news = newsList.get(position);
Bundle bundle = new Bundle();
bundle.putString("title", news.getTitle());
bundle.putString("time", pubDate);
bundle.putString("author", author);
bundle.putString("description",description);
Intent intent = new Intent(MainActivity.this,NewsDescription.class);
intent.putExtras(bundle);
startActivity(intent);
});
}
};
//創建一個下載檔案的方法
public void download(){
//創建一個作業執行緒,繼承 Thread,重新 run 方法,處理耗時操作
new Thread(){ //創建子執行緒 引文下載行程是一個緩慢的程式,主線執行緒不可以使用
//重寫run()方法。
@Override
public void run() {
try {
Context context = MainActivity.this.getApplication(); //獲取一個Context物件
String path =context.getFilesDir()+"/test.xml"; //目的為了獲得安卓的file目錄
String url1 = url;//創建下載連接
URL url = new URL(url1); //創建一個URL
HttpURLConnection con = (HttpURLConnection)url.openConnection();//獲得一個HttpURLConnection物件 con
con.setConnectTimeout(3*1000);
//防止屏蔽程式抓取而回傳403錯誤
con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
InputStream iso = con.getInputStream(); //通過con 獲得一個輸入流 iso 物件
File file = new File(path); //創建一個file物件 將路徑寫入
FileOutputStream fos = new FileOutputStream(file);
byte[] b = new byte[1024];
int count = 0;
while((count = iso.read(b,0,1024))!= -1){
fos.write(b,0,count);
System.out.print(b+"/kb");
}
fos.close();
iso.close();
List<News> pull = Pull(); //呼叫Pull()決議方法決議xml檔案 并回傳List<news> 物件
for(News n : pull){ //遍歷list集合
Message msg = new Message(); //創建一個 Message 物件 M
msg.obj=n; //message物件可以接受任何型別的物件 并且可以通過反向強制轉換 轉換回來
handler.sendMessage(msg);
}
}
catch (Exception e){
System.out.println(e.getMessage());
}
}
}.start();
}
public List<News> Pull(){
try {
Context context = MainActivity.this.getApplication();
String url = context.getFilesDir()+"/test.xml";
InputStream fis = new FileInputStream(url);
List<News> list=NewsPull.getNews(fis);
for (News n:list){
System.out.println(n);
}
return list;
} catch (Exception e) {
e.printStackTrace();
return null;
} catch (Throwable throwable) {
throwable.printStackTrace();
return null;
}
}
}其中lvs設定點擊事件如何獲取到相應的title,date,author和description傳遞到下一個界面呢
像目前News news = newsList.get(position);這樣寫不知道為什么會報錯java.lang.IndexOutOfBoundsException: Index: 3, Size: 0
求解
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/244445.html
標籤:Android
上一篇:關于qHash
下一篇:類加載、物件實體化知識點一網打盡
