●畫面預覽

HashMap與SimpleAadapter

●補充說明

(一)Collection<E>=>一群東西的集合
 
(二)Set<E> interface=>有排序(ex:TreeSet Class)、沒有排序(ex:HashSet Class)
=>不可重複,用來計算不一樣資料的總筆數
HashSet<String> myset = new HashSet<>();
        myset.add("aa");
        myset.add("aa");
        myset.add("bb");
        myset.add("aa");
        myset.add("aa");
        for (String s : myset) {
            System.out.println(s);
        }
=>結果會只有aa和bb
 
(三)Map就是key對value的關係
=>Interface Map<k,v>,常用HashMap Class
=>Map就像一張表格
 
Key
Value
username
John
tel
123
HashMap<String, String> mymap = new HashMap();
mymap.put("username", "john");
mymap.put("tel", "0911111111");
System.out.println(mymap.get("tel"));
**說明:
1.結果是0911111111
2.new HashMap();在java7以後可以省略<>
3.mymap.put("username", "john");=>把key和value放入表格中
4.android中很多資料存取是用map關係
 
*Map的另一種寫法
 
Key
Value
TPE
TC
KH
02
04
07
=>把上面的做法改為下面三個,並且放入arraylist中
 
Key
Value
city
code
台北
02
 
Key
Value
city
code
台中
04
 
Key
Value
city
code
高雄
07
 
        ArrayList<Map<String, String>> mylist = new ArrayList();
        HashMap<String, String> m1 = new HashMap();
        m1.put("city", "台北");
        m1.put("code", "02");
        mylist.add(m1);
        
        HashMap<String, String> m2 = new HashMap();
        m2.put("city", "台中");
        m2.put("code", "04");
        mylist.add(m2);
        
        HashMap<String, String> m3 = new HashMap();
        m3.put("city", "高雄");
        m3.put("code", "07");
        mylist.add(m3);
        
        for(Map<String,String> m:mylist){
            System.out.println(m.get("city")+","+m.get("code"));
        }
**說明:
1.outputs為
HashMap與SimpleAadapter
2.這邊的get method為HashMap CLass下的get method而非arraylist下的get method
 
3.get():public V get(Object key):Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
 
4.android可用這種結構=>simple adapter

 


●程式與說明--java code

public class MainActivity extends AppCompatActivity {
private ListView lv;
private SimpleAdapter sa;
ArrayList<Map<String,String>> mylist= new ArrayList<>();
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView)findViewById(R.id.lv);
HashMap<String, String> m1 = new HashMap<>();
m1.put("city", "台北");
m1.put("code", "02");
mylist.add(m1);
HashMap<String, String> m2 = new HashMap<>();
m2.put("city", "台中");
m2.put("code", "04");
mylist.add(m2);
HashMap<String, String> m3 = new HashMap<>();
m3.put("city", "高雄");
m3.put("code", "07");
mylist.add(m3);
 
sa=new SimpleAdapter(MainActivity.this,
mylist,
android.R.layout.simple_list_item_2,
new String[]{"city","code"},
new int[]{android.R.id.text1,android.R.id.text2});
 
lv.setAdapter(sa);
 
 
}
}
**說明:
1.建立arraylist
2.把資料放進arraylist中
3.建立 SimpleAdapter()物件,放五個參數=>context(MainActivity.this),Map資料,listview的版型,Map的key值陣列,key要對應到的資源(這邊因為是使用內建的layout,所以也有內建的資源id)
4.使用simpleadapter的話,版型和資源id都可使用自訂的
 
*如果要加圖片並且用自己的layout
 
public class MainActivity extends AppCompatActivity {
private ListView lv;
private SimpleAdapter sa;
ArrayList<Map<String,Object>> mylist= new ArrayList<>();
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView)findViewById(R.id.lv);
HashMap<String,Object> m1 = new HashMap<>();
m1.put("city", "台北");
m1.put("code", "02");
m1.put("img",R.drawable.taipei);
mylist.add(m1);
HashMap<String, Object> m2 = new HashMap<>();
m2.put("city", "台中");
m2.put("code", "04");
m2.put("img",R.drawable.taichun);
mylist.add(m2);
HashMap<String, Object> m3 = new HashMap<>();
m3.put("city", "高雄");
m3.put("code", "07");
m3.put("img",R.drawable.kaishon);
mylist.add(m3);
 
sa=new SimpleAdapter(MainActivity.this,
mylist,
R.layout.myitem, //如果是用自己的版型,R前面不用放android.
new String[]{"city","code","img"},
new int[]{R.id.t1,R.id.t2,R.id.iv});
 
lv.setAdapter(sa);
 
 
}
}
**說明:
1.先把MAP改為 Map<String,Object>,下面相對應的HashMap也要跟著改
2.把圖片放到m1/m2/m3中,key為img,value為id
3.最後的字串陣列要加入img key,資源陣列要放入img的id

 

●程式參考(GitHub):檔案存取(三)HashMap與SimpleAadapter

 

arrow
arrow

    muchone 發表在 痞客邦 留言(0) 人氣()