activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.miao.news2.MainActivity"
android:orientation="vertical">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
news_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="80dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/content"
android:textSize="15sp"
android:textColor="#0000ff"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
MainActivity.java:
package com.miao.news;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/*
**再做一次修改....
*/
public class MainActivity extends AppCompatActivity {
private ListView lv ;
private SimpleAdapter sAdapter;
private List<Map<String,Object>> item;
private Map<String,Object> map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView)findViewById(R.id.lv);
sAdapter = new SimpleAdapter(this,getData(),R.layout.my_layout,
new String[]{"title","info","image"},
new int[]{R.id.title,R.id.info,R.id.image});
lv.setAdapter(sAdapter);
}
public List<Map<String,Object>> getData(){
item = new ArrayList<Map<String, Object>>();
map = new HashMap<String,Object>();
map.put("image",R.drawable.accident);
map.put("title","Accident");
map.put("info","今日早间新闻报道,四川一山区发生翻车事件。");
item.add(map);
map = new HashMap<String,Object>();
map.put("image",R.drawable.phone);
map.put("title","IPhone SE");
map.put("info","IPhoneSE 将于本月的21号发布,敬请期待。");
item.add(map);
map = new HashMap<String,Object>();
map.put("image",R.drawable.boy);
map.put("title","Boy & Dog");
map.put("info","男孩和两只狗狗的故事。");
item.add(map);
map = new HashMap<String,Object>();
map.put("image",R.drawable.cat);
map.put("title","Mom & Child");
map.put("info","这只猫咪的孩子好奇怪,是不是生着生着没墨了。");
item.add(map);
return item;
}
}