Activity中与ListActivity中使用listview区别

一.Activity中与ListActivity中使用listview区别(本身没多大区别,只是ListActivity在listview的显示上做了一些优化)

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);
    }

}
复制代码

源码打包下载

 

转自:http://www.cnblogs.com/bravestarrhu/archive/2012/05/06/2486703.html

posted @ 2016-07-06 09:12  N神3  阅读(147)  评论(0编辑  收藏  举报