Lesson 1 : This is a ListView
菜鳥的ListView第一課,這個東東很常用,所以先來個小菜一碟吧,來個字串版!!!重點就是Adapter,不出聲誰知道你是什麼倫,不用Adapter,誰知道你戳了誰。
1. 建立專案
2.layout新增ListView
3. Source Code
package
com.example.ex006;
import
android.os.Bundle;
import
android.app.Activity;
import
android.app.AlertDialog;
import
android.view.Menu;
import
android.view.View;
import
android.widget.AdapterView;
import
android.widget.AdapterView.OnItemClickListener;
import
android.widget.ArrayAdapter;
import
android.widget.ListView;
public class MainActivity extends Activity {
private String[] items = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};
private ListView lv;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView)
MainActivity.this.findViewById(R.id.listView1);
ArrayAdapter<String>
list = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
lv.setAdapter(list);
lv.setOnItemClickListener(new
OnItemClickListener()
{
@Override
public void
onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
new
AlertDialog.Builder(MainActivity.this)
.setTitle("星期幾?")
.setMessage(items[arg2])
.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;
}
}
|
4.跑一下吧
5. 點到星期幾呢? 之前那麼多篇AlertDailog,要恢復記憶的看這篇吧! 真希望天天都是星期天之 AlertDialog & CheckBox
有這麼簡單就好了,but,事情絕對不是憨人想的這麼簡單,憨人是誰?當然是洗襪,菜鳥工程師了,字串只有在範例才會用,好自為之吧!
標籤: AlertDialog, ListView
0 個意見:
張貼留言
<< 首頁