1. 程式人生 > 實用技巧 >ListView資料列表(二)

ListView資料列表(二)

1、帶圖片資料列表

2、前端介面設計

3、後臺控制程式碼

 1 public class MainActivity extends AppCompatActivity {
 2     ListView listView;
 3     ArrayList<HashMap<String, Object>> list;
 4     int[] icon = {R.drawable.icon1, R.drawable.icon2, R.drawable.icon3};
 5     String[] title = {"企業會話", "辦公郵件", "財務資訊"};
6 String[] info = {"你好", "寫信", "發補貼"}; 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.activity_main); 12 13 listView = (ListView) findViewById(R.id.lv); 14 list = initDatas();
15 SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.item, new String[]{"img", "name", "txt"}, new int[]{R.id.img, R.id.title, R.id.info}); 16 listView.setAdapter(adapter); 17 } 18 19 private ArrayList<HashMap<String, Object>> initDatas() { 20 ArrayList<HashMap<String, Object>> tmplist = new
ArrayList<>(); 21 for (int i = 0; i < 3; i++) { 22 HashMap<String, Object> map = new HashMap<>(); 23 map.put("img", icon[i]); 24 map.put("name", title[i]); 25 map.put("txt", info[i]); 26 tmplist.add(map); 27 } 28 return tmplist; 29 }