Activity中与ListActivity中使用listview区别
2015-03-14 14:57
423 查看
一.Activity中与ListActivity中使用listview区别(本身没多大区别,只是ListActivity在listview的显示上做了一些优化)
Activity中使用Listview步骤:
1.xml布局中,ListView标签id可以任意取值如:
2.程序中通过findViewById获取该Listview对象如:
3.实例化Adapter
4.然后通过listView.setAdapter(adapter); 设置adapter绑定数据如:
ListActivity中使用Listview步骤:
1.xml布局中,ListView标签id必须写成android:id="@+id/android:list"如:
2.实例化Adapter
3.然后通过setListAdapter(adapter);设置adapter绑定数据
补充:程序中如果想获取Listview对象,通过getListView()方法
上图:
源码目录结构:
完整源码:
1>AndroidManifest.xml
2>main.xml
3>activity.xml
4>listactivity.xml
5>mylist_item.xml
6>strings.xml
7>MainActivity.java
8>ListViewInActivity.java
9>ListViewInListActivity.java
Activity中使用Listview步骤:
1.xml布局中,ListView标签id可以任意取值如:
<ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/listview" />
2.程序中通过findViewById获取该Listview对象如:
ListView lv = (ListView)findViewById(R.id.listview);
3.实例化Adapter
4.然后通过listView.setAdapter(adapter); 设置adapter绑定数据如:
lv.setAdapter(adapter);
ListActivity中使用Listview步骤:
1.xml布局中,ListView标签id必须写成android:id="@+id/android:list"如:
<ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/android:list" />
2.实例化Adapter
3.然后通过setListAdapter(adapter);设置adapter绑定数据
setListAdapter(adapter);
补充:程序中如果想获取Listview对象,通过getListView()方法
ListView lv = getListView();
上图:
源码目录结构:
完整源码:
1>AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.bravestarr.app.listview1" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="7" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".ListViewInActivity" android:label="@string/app_name" > </activity> <activity android:name="com.bravestarr.app.listview1.ListViewInListActivity" android:label="@string/app_name" > </activity> </application> </manifest>
2>main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btn1" android:id="@+id/btn1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btn2" android:id="@+id/btn2" /> </LinearLayout>
3>activity.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/listview_in_activity" /> <ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/listview" /> </LinearLayout>
4>listactivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/listview_in_listactivity" />
<ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/android:list" />
</LinearLayout>
5>mylist_item.xml
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/item"/>
6>strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="btn1">继承自Activity的listview使用</string> <string name="btn2">继承自ListActivity的listview使用</string> <string name="listview_in_activity">说明:Activity中使用Listview\nxml布局中,ListView标签id可以任意取值,\n程序中通过findViewById找到该Listview对象,\n然后通过listView.setAdapter(adapter); 绑定设置adapter绑定数据</string> <string name="listview_in_listactivity">说明:ListActivity中使用Listview\nxml布局中,ListView标签id必须写成android:id="@+id/android:list",\n程序中通过getListView()找到该Listview对象,\n然后通过setListAdapter(adapter);设置adapter绑定数据,无需得到listview</string> <string name="app_name">ListView的使用</string> </resources>
7>MainActivity.java
package com.bravestarr.app.listview1; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; /** * @Author BraveStarr * @QQ 1733259520 * @Blog http://www.cnblogs.com/bravestarrhu/,http://blog.sina.com.cn/wanghubravestarr * */ public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn1,btn2; btn1 = (Button)findViewById(R.id.btn1); btn2 = (Button)findViewById(R.id.btn2); btn1.setOnClickListener(l); btn2.setOnClickListener(l); } private View.OnClickListener l = new View.OnClickListener() { Intent i; @Override public void onClick(View v) { switch(v.getId()){ case R.id.btn1: i = new Intent(MainActivity.this,ListViewInActivity.class); startActivity(i); break; case R.id.btn2: i = new Intent(MainActivity.this,ListViewInListActivity.class); startActivity(i); break; } } }; }
8>ListViewInActivity.java
package com.bravestarr.app.listview1; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; public class ListViewInActivity extends Activity { private static final String[] names = new String[]{"斗破苍穹","网游之终极牧师","琴帝"}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity); ListView lv = (ListView)findViewById(R.id.listview); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.mylist_item, names); lv.setAdapter(adapter); } }
9>ListViewInListActivity.java
package com.bravestarr.app.listview1; import android.app.ListActivity; import android.os.Bundle; import android.widget.ArrayAdapter; /** * @Author BraveStarr * @QQ 1733259520 * @Blog http://www.cnblogs.com/bravestarrhu/,http://blog.sina.com.cn/wanghubravestarr * */ public class ListViewInListActivity extends ListActivity { private static final String[] names = new String[]{"斗破苍穹","网游之终极牧师","琴帝"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listactivity); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.mylist_item, names); setListAdapter(adapter); } }
相关文章推荐
- Android中Activity中与ListActivity中使用listview区别
- Android中Activity中使用ListView与ListActivity中使用ListView的区别与联系
- Activity中与ListActivity中使用listview区别
- Android之 ListActivity 的使用和在 ListView中添加图片文字(ListActivity与Activity的区别)
- 第四章--android核心组件之Activity与ListActivity中使用ListView的区别
- Activity中与ListActivity中使用listview区别
- Activity中与ListActivity中使用listview区别
- ListActivity与Activity的使用与区别
- Android关于ListView的总结--使用ListActivity
- Android之ListActivity的使用和在ListView中添加图片文字
- Android关于ListView的总结--使用ListActivity
- android ListView布局之一(继承listActivity、使用arrayAdapter)
- TabActivity,ActivityGroup,ExpandableListActivity,ListView联合使用问题解决。
- ExpandableListView 和 ExpandableListActivity的使用及数据更新
- 【Android基础】listview控件的使用(2)-------继承自ListActivity的普通listview
- Android ListActivity的使用和在ListView中添加图片文字
- listactivity与activity的区别
- Activity在GridView和ListView之间切换使用一个布局实现的方法
- 通过ListActivity使用ListView布局方法
- ExpandableListView 和 ExpandableListActivity的使用及数据更新