Android中Activity中与ListActivity中使用listview区别
来源:互联网 发布:mac系统崩溃重装系统 编辑:程序博客网 时间:2024/05/17 06:07
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()方法
MainActivity.java
package com.whisker.listviewtext;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {private Button acButton;private Button listAcButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); acButton = (Button) findViewById(R.id.ac); listAcButton = (Button) findViewById(R.id.listac); acButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {Intent intent = new Intent(MainActivity.this,ListInActivity.class);startActivity(intent);}}); listAcButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {Intent intent = new Intent(MainActivity.this,ListInListActivity.class);startActivity(intent);}}); }}
ListInActivity.java
package com.whisker.listviewtext;import android.app.Activity;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.ListView;public class ListInActivity extends Activity{private final static String[] name = new String[] {"First","Second","Third"};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.listinactivity);ListView listView = (ListView) findViewById(R.id.listInActivity);ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item,name);listView.setAdapter(adapter);}}
LitsInListActivity.java
package com.whisker.listviewtext;import android.app.ListActivity;import android.os.Bundle;import android.widget.ArrayAdapter;public class ListInListActivity extends ListActivity{private final static String[] name = new String[] {"First","Second","Third","Fourth"};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.listinlistactivity);ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item,name);setListAdapter(adapter);}}
activity_main.xml
<LinearLayout 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" android:orientation="vertical" > <Button android:id="@+id/ac" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Activity" android:layout_gravity="center" android:gravity="center"/> <Button android:id="@+id/listac" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ListActivity" android:layout_gravity="center" android:gravity="center"/></LinearLayout>
listinactivity.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="ListInActivity" android:gravity="center"/> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/listInActivity"></ListView> </LinearLayout>
listinlistactivity.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="ListInListActivity" android:gravity="center"/> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/android:list"></ListView> </LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent"/>
一开始调试的时候,一直出错,后来发现是因为在Adapter实例化的时候,对应的xml文件list_item一开始写的是LinearLayout,虽然不知道为什么,但应该是ArrayAdapter对应的xml是这样要求的,SimpleAdapter就是可以LinearLayout。
效果图:
0 0
- Android中Activity中与ListActivity中使用listview区别
- Android中Activity中使用ListView与ListActivity中使用ListView的区别与联系
- Activity中与ListActivity中使用listview区别
- Activity中与ListActivity中使用listview区别
- Activity中与ListActivity中使用listview区别
- Activity中与ListActivity中使用listview区别
- Android之 ListActivity 的使用和在 ListView中添加图片文字(ListActivity与Activity的区别)
- 第四章--android核心组件之Activity与ListActivity中使用ListView的区别
- 浅谈对Android 中ListActivity与Activity的区别
- Android中ListView的使用(不用ListActivity)
- ListActivity 中 ListView 的使用
- Android中ListActivity的使用
- android中listactivity的使用
- ListActivity中点击listview传值到一个新的activity中
- ListActivity中listview的获取
- Android中ListActivity的使用和在ListView中添加图片文字
- ListActivity与Activity的使用与区别
- ListActivity 的使用和在 ListView中添加图片文字
- 掷骰子,求得每个数的概率
- android 杂记 3
- 国内镜像地址
- 腾讯面试题总结,似乎是面试C++方向的(小巫总结)
- [UVA1595]Symmetry
- Android中Activity中与ListActivity中使用listview区别
- hdoj 1856 More is better(并查集)
- 音视频编码格式汇总
- How Many Tables
- ROS学习--(十)roslaunch
- HDU 1074 Doing Homework(状压dp)
- css基础
- 建造者模式
- hadoop2.x常用端口、定义方法及默认端口、hadoop1.X端口对比