zoukankan      html  css  js  c++  java
  • ListView

    代码:

    ListViewActivity:

     1 package com.vanceinfo.ListView;
    2
    3 import android.app.Activity;
    4 import android.database.Cursor;
    5 import android.graphics.Color;
    6 import android.os.Bundle;
    7 import android.provider.ContactsContract;
    8 import android.provider.ContactsContract.PhoneLookup;
    9 import android.view.View;
    10 import android.widget.AdapterView;
    11 import android.widget.LinearLayout;
    12 import android.widget.ListAdapter;
    13 import android.widget.ListView;
    14 import android.widget.SimpleCursorAdapter;
    15 import android.widget.Toast;
    16
    17 public class ListViewActivity extends Activity
    18 {
    19 LinearLayout mLinearLayout;
    20
    21 ListView mListView;
    22
    23 @Override
    24 public void onCreate(Bundle savedInstanceState)
    25 {
    26 super.onCreate(savedInstanceState);
    27 // setContentView(R.layout.main);
    28 // 创建linearLayout布局对象
    29 mLinearLayout = new LinearLayout(this);
    30 // 设置布局LinearLayout的属性
    31 mLinearLayout.setOrientation(LinearLayout.VERTICAL);
    32 mLinearLayout.setBackgroundColor(android.graphics.Color.BLACK);
    33 // 创建ListView对象
    34 mListView = new ListView(this);
    35 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
    36 LinearLayout.LayoutParams.WRAP_CONTENT);
    37 mListView.setBackgroundColor(Color.BLACK);
    38 // 添加mListView到mLinearLayout布局
    39 mLinearLayout.addView(mListView, params);
    40 // 设置显示mLinearLayout
    41 setContentView(mLinearLayout);
    42 // 获取数据库Phones的Cursor
    43 Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    44 startManagingCursor(cursor);
    45 // ListAdapter是ListView和后台数据的桥梁
    46 ListAdapter adapter = new SimpleCursorAdapter(this,
    47 // 定义List中每一行的显示模板
    48 // 表示每一行包含两个数据项
    49 android.R.layout.simple_list_item_2,
    50 // 数据库的Cursor对象
    51 cursor,
    52 // 从数据库的NAME和NUMBER两个列中取数据
    53 new String[] {PhoneLookup.DISPLAY_NAME},
    54 // 与NAME和NUMBER对应的Views
    55 new int[] {android.R.id.text1});
    56 // 将adpter添加到mListView中
    57 mListView.setAdapter(adapter);
    58 // 为mListView视图添加setOnItemSelectedListener监听
    59 mListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    60 {
    61
    62 @Override
    63 public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
    64 {
    65 DisplayToast("滚动到第" + Long.toString(parent.getSelectedItemId()) + "项");
    66 }
    67
    68 @Override
    69 public void onNothingSelected(AdapterView<?> parent)
    70 {
    71 // 没有选中
    72 }
    73 });
    74 // 为mListView视图添加setOnItemClickListener监听
    75 mListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
    76 {
    77
    78 @Override
    79 public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    80 {
    81 DisplayToast("选中了第" + Integer.toString(position + 1) + "项");
    82 }
    83 });
    84 }
    85
    86 // 显示Toast
    87 public void DisplayToast(String string)
    88 {
    89 Toast.makeText(this, string, Toast.LENGTH_SHORT).show();
    90 }
    91 }

    在AndroidManifest.xml中添加权限:

     1 <?xml version="1.0" encoding="utf-8"?>
    2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    3 package="com.vanceinfo.ListView"
    4 android:versionCode="1"
    5 android:versionName="1.0">
    6 <uses-sdk android:minSdkVersion="8" />
    7
    8 <application android:icon="@drawable/icon" android:label="@string/app_name">
    9 <activity android:name="ListViewActivity"
    10 android:label="@string/app_name">
    11 <intent-filter>
    12 <action android:name="android.intent.action.MAIN" />
    13 <category android:name="android.intent.category.LAUNCHER" />
    14 </intent-filter>
    15 </activity>
    16 </application>
    17 <!--获得了电话本数据,需要在AndroidManifest.xml中加上权限 -->
    18 <uses-permission android:name="android.permission.READ_CONTACTS"/>
    19 </manifest>

    效果图





  • 相关阅读:
    tomcat createSecureRandom 花费了将近10分钟
    tcpdump取数据保存
    Linux TOP命令 按内存占用排序和按CPU占用排序
    在四合院里写code是什么体验(非拉仇恨)
    严重的抑郁与焦虑症
    CodeIgniter 下引入ORM Doctrine
    linux下解压
    ssh 使用密钥与登录进行远程cp
    mac os 下的sublime --- 快捷键
    pixelmator处理png图片,处理掉过白的留白。
  • 原文地址:https://www.cnblogs.com/jh5240/p/2229237.html
Copyright © 2011-2022 走看看