当前位置:首页 > android常用控件总结
android:paddingBottom=\ android:id=\ android:layout_width=\ android:layout_height=\ android:textSize=\ android:singleLine=\ android:id=\ android:layout_width=\ android:layout_height=\ android:gravity=\ android:textSize=\ --------------------------------------------------------------------------------------------------------------------- public class ListView1 extends ListActivity{//继承的是ListActivity public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); ArrayList HashMap list.add(map3); SimpleAdapter listAdapter = new SimpleAdapter(this,list,R.layout.simpleadapter2, new String[]{\int[]{R.id.user_name,R.id.user_ip}); setListAdapter(listAdapter);//调用ListActivity中的setListAdapter()方法设置显示ListView } } ---------------------------------------------------------------------------------------------------------------------- * 此为ListActivity和listView数据适配器 。方法中的第一参数为当前对象,第二个参数为 * 我们要映射成Mapd的List结构,第三个参数为每一行列表的布局(此处为一行显示两个listView) * 第四个参数是一个字符串数组,并规定了顺序,第五个参数是依次把第四个参数的文本隐射到listView的布局中去 * 本适配器的格式也可为:(注意映射的顺序) * setListAdapter(new SimpleAdapter(this,list,R.layout.simpleadapter2, new String[]{\int[]{R.id.user_name,R.id.user_ip}); ======================================================================================================================= 第五个例子:通过ListView来显示电话本中信息。 先创建LinearLayout对象和ListView对象,LinearLayout用来显示ListView 然后通过ListAdapter将获得的电话本数据与ListView连接起来 接着将ListAdapter添加到ListView中 最后将ListView添加到Linearlayout中,让屏幕显示LinearLayout。 要处理ListView事件需要为其添加setOnItemSelectedListener监听以及setOnItemClickListener监听 例: public class ListView2 extends Activity{ LinearLayout linearLayout; ListView listView1; public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); linearLayout = new LinearLayout(this);//创建LinearLayout布局对象----------------------------------------------------------在代码中创建LinearLayout linearLayout.setOrientation(linearLayout.VERTICAL);//设置方向 linearLayout.setBackgroundColor(android.graphics.Color.BLUE);//设置背景色 listView1 = new ListView(this);//创建ListView对象 LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); listView1.setBackgroundColor(Color.BLACK); linearLayout.addView(listView1,param);//添加listView到linearlayout中去 setContentView(linearLayout);//设置显示LinearLayout布局 //获取数据库Phones的Cursor Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null); startManagingCursor(cur); //ListAdapter是ListView和后台数据的桥梁。 //SimpleCursorAdapter是把数据中查询到的结果映射到listView中 ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2,//表示每一行的布局包含两个数据项 cur,//数据库的Cursor对象 new String[]{PhoneLookup.DISPLAY_NAME,PhoneLookup.NUMBER},//从数据库的NAME和NUMBER两列中取数据 new int[]{android.R.id.text1,android.R.id.text2});//与NAME和NUMBER对应的Views listView1.setAdapter(adapter);//将adapter添加到listView中 //在ListView中当鼠标滚动时会触发setOnItemSelectedListener listView1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){-------------------------------------------------为listView1的视图添加setOnItemSelectedListener监听 @Override public void onItemSelected(AdapterView> arg0, View arg1,int arg2, long arg3) { DisplayToast(\滚动到第\项\ } @Override public void onNothingSelected(AdapterView> arg0) { //没有选中 } }); //在ListView中当点击时会触发setClickListener listView1.setOnItemClickListener(new AdapterView.OnItemClickListener(){ @Override public void onItemClick(AdapterView> arg0, View arg1, int arg2,long arg3) {
共分享92篇相关文档