package com.example.gpsticker;
import java.util.ArrayList;
import java.util.HashMap;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity {
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
private SimpleAdapter adapter;
ListView listView01;
private static final String[] mPlaces = new String[] {
"台北市", "新北市", "台南市", "高雄市", "苗粟縣",
"台北市", "新北市", "台南市", "高雄市", "苗粟縣",
"台北市", "新北市", "台南市", "高雄市", "苗粟縣",
"台北市", "新北市", "台南市", "高雄市", "苗粟縣",
"台北市", "新北市", "台南市", "高雄市", "苗粟縣",
"台北市", "新北市", "789", "cde", "abc"
};
private static final String[] mFoods = new String[] {
"大餅包小餅", "蚵仔煎", "東山鴨頭", "臭豆腐", "潤餅",
"豆花", "青蛙下蛋","豬血糕", "大腸包小腸", "鹹水雞",
"烤香腸","車輪餅","珍珠奶茶","鹹酥雞","大熱狗",
"炸雞排","山豬肉","花生冰","剉冰","水果冰",
"包心粉圓","排骨酥","沙茶魷魚","章魚燒","度小月",
"aaa","abc","bbb","bcd","123"
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView01 = (ListView)findViewById(R.id.listView1);
//把資料加入ArrayList中
for(int i=0; i<mPlaces.length; i++){
HashMap<String,String> item = new HashMap<String,String>();
item.put( "food", mFoods[i]);
item.put( "place",mPlaces[i] );
list.add( item );
}
//新增SimpleAdapter
adapter = new SimpleAdapter(
this,
list,
android.R.layout.simple_list_item_2,
new String[] { "food","place" },
new int[] { android.R.id.text1, android.R.id.text2 } );
//ListActivity設定adapter
listView01.setAdapter( adapter );
//啟用按鍵過濾功能,這兩行資料都會進行過濾
listView01.setTextFilterEnabled(true);
// 为ListView设置列表项点击监听器
listView01.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, android.view.View view, int position, long id) {
Toast.makeText(getApplicationContext(), "您点击的是:"+"第"+(position+1)+"项", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
結果會如下 , 分隔線左右會有留空 , ListView 週邊有留空 , 另有一個物件
如果改用 extends ListActivity ,
1.
// setContentView(R.layout.activity_main); // 這行關掉
2.
//listView01.setAdapter( adapter );
setListAdapter( adapter );
3.
//listView01.setTextFilterEnabled(true);
getListView().setTextFilterEnabled(true);
4. (不知如何取得 Click , 整個先關掉)
// listView01.setOnItemClickListener(new OnItemClickListener() {
//
// @Override
// public void onItemClick(AdapterView<?> parent, android.view.View view, int position, long id) {
// Toast.makeText(getApplicationContext(), "您点击的是:"+"第"+(position+1)+"项", Toast.LENGTH_SHORT).show();
// }
// });
結果就會是滿版的 ,