Android学习笔记-ListView

本文深入探讨了Android开发中ListView组件的实现原理及应用技巧,包括如何从联系人数据库查询并展示联系人的姓名与电话号码,以及ListView的交互事件处理。通过实例代码展示,帮助开发者更好地理解和掌握ListView的使用。
package org.ben.test;

import android.app.Activity;
import android.content.ContentResolver;
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 AndroidTest_ListViewActivity extends Activity {

//LinearLayout是ListView的容器
LinearLayout m_LinearLayout;
ListView m_ListView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建LinearLayout布局对象
m_LinearLayout = new LinearLayout(this);
m_LinearLayout.setOrientation(LinearLayout.VERTICAL);
m_LinearLayout.setBackgroundColor(android.graphics.Color.BLACK);
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_LinearLayout.addView(m_ListView,param);

setContentView(m_LinearLayout);

ContentResolver cr = getContentResolver();
// String str1="" ;
// String str2="" ;
Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null,null);
// startManagingCursor(cursor);
//
// while(cursor.moveToNext())
// {
// int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
// String name = cursor.getString(nameFieldColumnIndex);
// str1+=name;
//
// String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
//
// Cursor phone = cr.query(ContactsContract.CommonDaiangtaKinds.Phone.CONTENT_URI, null,
// ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+contactId,
// null, null);
//
// while (phone.moveToNext())
// {
// String strPhoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// str2 += (":" + strPhoneNumber);
// }
// phone.close();
// }



ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2,
cursor,
//android2.0以后获取联系人号码都必须使用ContactsContract.CommonDataKinds.Phone.NUMBER

new String[]{ ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER},
new int[] { android.R.id.text1, android.R.id.text2});

m_ListView.setAdapter(adapter);

m_ListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){

public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
DisplayToast("滚动到第"+Long.toString(arg0.getSelectedItemId())+"项");
}

public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
//表示没有选中
}
});

m_ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
DisplayToast("选中了第"+Integer.toString(arg2+1)+"项");
}
});
}

public void DisplayToast(String str)
{
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
}
posted on 2011-11-06 22:48 Jesuca 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/Jesuca/archive/2011/11/06/2238469.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值