標籤:prot except html轉換 重寫 失敗 undle develop 圖片 init
Google官方文檔的介紹:https://developer.android.com/reference/android/widget/ListView.html
顯示可垂直滾動的視圖集合,其中每個視圖都立即位於列表中的上一個視圖的下方。 為了更現代化,更靈活和更有效地顯示列表,請使用RecyclerView。。。
現在活動布局中加入ListView:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent"/></LinearLayout>
這裡做一個定製的列表,就是自己定製列表中的布局。建立一個hero_item.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <ImageView android:id="@+id/hero_img" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/hero_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:textSize="20sp"/></LinearLayout>
再然後是這次想要使用的資料,http://ow.blizzard.cn/heroes/,資料來自暴雪的守望先鋒官網的英雄介紹頁面
旁邊的html代碼中<img src="..." class="portrait">是英雄頭像的url,</span><span class="portrait-title">末日鐵拳</span></span>是英雄的名字。
所以建立一個H
public class Hero { private String imageUrl; private String name; public Hero(String imageUrl, String name) { this.imageUrl = imageUrl; this.name = name; } public String getUrl() { return imageUrl; } public void setUrl(String url) { this.imageUrl = url; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
然後就是ListView的Adapter,建立一個HeroAdapter.class,這個Adapter繼承自ArrayAdapter,並將泛型指定為Hero
public class HeroAdapter extends ArrayAdapter<Hero> { private int resourceId; public HeroAdapter(Context context, int textViewResourceId, List<Hero> objects){ super(context,textViewResourceId,objects); resourceId = textViewResourceId; } @NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { Hero hero = getItem(position); View view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false); ImageView heroImg = (ImageView) view.findViewById(R.id.hero_img); TextView heroName = (TextView) view.findViewById(R.id.hero_name); Glide.with(getContext()).load(hero.getUrl()).into(heroImg); heroName.setText(hero.getName()); return view; }}
HeroAdapter重寫了父類的建構函式,可以獲得上下文,ListView的子布局的id和資料,然後重寫getView()方法,這個方法會在每個子項被滾動到螢幕時調用,在getItem()中根據position獲得當前的Hero執行個體,然後使用LayoutInflater來為這個子項載入布局,再然後設定布局中的ImageView 和TextView,最後返回布局。
最後活動中的代碼
public class MainActivity extends AppCompatActivity { private List<Hero> heroList = new ArrayList<>(); private Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ListView listView = (ListView) findViewById(R.id.list_view); initHeroes(); handler = new Handler(){ @Override public void handleMessage(Message msg) { if(msg.what == 1){ HeroAdapter adapter = new HeroAdapter(MainActivity.this, R.layout.hero_item, heroList); listView.setAdapter(adapter); } } }; } //讀取資料 private void initHeroes(){ String weatherUrl = "http://ow.blizzard.cn/heroes/"; HttpUtil.sendOkHttpRequest(weatherUrl, new Callback() { @Override public void onResponse(Call call, Response response) throws IOException { final String responseText = response.body().string(); new Thread(new Runnable() { @Override public void run() { Document doc = Jsoup.parse(responseText);//將String類型的html轉換為Document Elements elements1 = doc.select(".portrait"); //讀取圖片url Elements elements2 = doc.select(".portrait-title");//讀取英雄名字 for (int j = 0; j < elements1.size(); j++) { Hero hero = new Hero(elements1.get(j).attr("src"), elements2.get(j).text()); heroList.add(hero); } Message msg = new Message(); msg.what = 1; handler.sendMessage(msg); } }).start(); } @Override public void onFailure(Call call, IOException e) { e.printStackTrace(); runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "資料擷取失敗", Toast.LENGTH_SHORT).show(); } }); } }); }}
建立一個HeroAdapter,傳入上下文,子布局和資料,再將這個適配器傳遞給ListView就好了
android ——ListView