acitvity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:ignore="MissingDefaultResource">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listviewId">
</ListView>
</RelativeLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
//数据源
private static final String[] commonFunList = new String[]{
"ListView1",
"ListView2",
"ListView3",
"ListView4",
"ListView5"
};
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化
listView = (ListView)findViewById(R.id.listviewId);
//setAdapter:布局
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, commonFunList));
//setOnItemClickListener:监听点击了哪一条list
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Dialog alertDiaLog = new AlertDialog.Builder(MainActivity.this).
setMessage("您点击了第" + i + "个item").create();
alertDiaLog.show();
}
});
}
}
网友评论