android应用开发揭秘例4-4笔记(ListView及Adapter的使用)

package com.yarin.android.Examples_04_04;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class Activity01 extends Activity
{
    LinearLayout    m_LinearLayout;
    ListView        m_ListView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        /* 创建LinearLayout布局对象 */
        m_LinearLayout = new LinearLayout(this);
        /* 设置布局LinearLayout的属性 */
        m_LinearLayout.setOrientation(LinearLayout.VERTICAL);
        m_LinearLayout.setBackgroundColor(android.graphics.Color.BLACK);

        /* 创建ListView对象 */
        m_ListView = new ListView(this);
        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        m_ListView.setBackgroundColor(Color.BLACK);

        /* 添加m_ListView到m_LinearLayout布局 */
        m_LinearLayout.addView(m_ListView, param);

        /* 设置显示m_LinearLayout布局 */
        setContentView(m_LinearLayout);

        // 获取数据库Phones的Cursor
        Cursor cur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        startManagingCursor(cur);

        // ListAdapter是ListView和后台数据的桥梁
        ListAdapter adapter = new SimpleCursorAdapter(this,
            // 定义List中每一行的显示模板
            // 表示每一行包含两个数据项
            android.R.layout.simple_list_item_2,
            // 数据库的Cursor对象
            cur,
            // 从数据库的NAME和NUMBER两列中取数据
            new String[] {PhoneLookup.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER},
            // 与NAME和NUMBER对应的Views
            new int[] { android.R.id.text1, android.R.id.text2 });
        
        /* 将adapter添加到m_ListView中 */
        m_ListView.setAdapter(adapter);

        /* 为m_ListView视图添加setOnItemSelectedListener监听 */
        m_ListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
            {
                
                DisplayToast("滚动到第"+Long.toString(arg2+1)+"项");
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0)
            {
                //没有选中
            }
        });

        /* 为m_ListView视图添加setOnItemClickListener监听 */
        m_ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
            {
                //于对选中的项进行处理
                DisplayToast("选中了第"+Integer.toString(arg2+1)+"项");
            }

        });
        
    }
    /* 显示Toast  */
    public void DisplayToast(String str)
    {
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值