通讯录-AlphabetIndexer的使用

AlphabetIndexer,实现了SectionIndexer接口,是adapter的一个辅助类,辅助实现在快滑时,显示索引字母。

使用字母索引的话,必须保证数据列表是按字母顺序排序,以便AlphabetIndexer采用二分查找法快速定位

下面使用AlphabetIndexer来模拟快速滑动展示通讯录。

自定义适配器,IndexAdapter,

public IndexAdapter(Context context, List<? extends Map<String, ?>> data,
			int resource, String[] from, int[] to) {
		super(context, data, resource, from, to);
		// 设置数据游标
		// 设置索引字母列表
		/**
		 * Cursor表示数据游标 sortedColumnIndex按字母索引的游标中的列号
		 * alphabet字母列表,用的最多的是"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
		 */
		alphabetIndexer = new AlphabetIndexer(new IndexCursor(this), 0,
				"012346789");
//		alphabetIndexer = new AlphabetIndexer(new IndexCursor(this), 0,
//				"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
	}



alphabetIndexer = new AlphabetIndexer(new IndexCursor(this), 0,"012346789");

第一个参数:游标,第二个参数:游标中列号进行排序,第三个参数:快速展示的索引值


自定义游标,获取展示的数据


private class IndexCursor implements Cursor {

		private ListAdapter adapter;
		private int position;
		private Map<String, String> map;

		public IndexCursor(ListAdapter adapter) {
			this.adapter = adapter;
		}

		@Override
		public int getCount() {
			return this.adapter.getCount();
		}

		/**
		 * 取得索引字母,这个方法非常重要,根据实际情况具体处理
		 */
		@SuppressWarnings("unchecked")
		@Override
		public String getString(int columnIndex) {
			map = (HashMap<String, String>) adapter.getItem(position);
			return map.get("itemText").substring(0, 2);
		}

		@Override
		public boolean moveToPosition(int position) {
			if (position < -1 || position > getCount()) {
				return false;
			}

			this.position = position;
			// 如果不满意位置有点向上偏的话,下面这几行代码是修复定位索引值为顶部项值的问题
			// if(position+2>getCount()){
			// this.position = position;
			// }else{
			// this.position = position + 2;
			// }
			return true;
		}

		@Override
		public int getPosition() {
			// TODO Auto-generated method stub
			return 0;
		}



在getString(int columnIndex)获取去索引信息,按照索引排序和快速锁定到对应的位置



<span style="font-family:SimSun;">public clas</span>s IndexMainActivity extends Activity {

	private ListView tweakedListView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setView();
		initView();
		setListener();
	}

	public void setView() {
		setContentView(R.layout.activity_listview_index_main);

	}

	public void initView() {
		tweakedListView = (ListView) findViewById(R.id.tweaked_list);

		// 准备数据
		List<Map<String, String>> itemList = getData();
		ListAdapter adapter = new IndexAdapter(this, itemList,
				R.layout.item_index_tweake_list_, new String[] { "itemText" },
				new int[] { R.id.tweaked_item_text });
		tweakedListView.setAdapter(adapter);

	}

	public void setListener() {
		// TODO Auto-generated method stub

	}

	public List<Map<String, String>> getData() {
		List<Map<String, String>> itemList = new ArrayList<Map<String, String>>();
		String alphas = "0123456789";
//		String alphas = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

		Map<String, String> map = null;
		for (char c : alphas.toCharArray()) {
			for (int i = 0; i < 10; i++) {
				map = new HashMap<String, String>();
				map.put("itemText", "" + c + i);
				itemList.add(map);
			}
		}
		return itemList;
	}
}


ListAdapter adapter = new IndexAdapter(this, itemList,R.layout.item_index_tweake_list_, new String[] { "itemText" },new int[] { R.id.tweaked_item_text });
第一个参数:上下文 , 第二个参数:展示的数据map集合 ,第三个参数:item展示的布局文件,第四个参数:map的键集合 ,第五个参数:item中条目的子控件id的集合


设置展示的数据List<Map<String,String>>,这里是值是String,当然也可以是自定义的对象,这里以String为例,就使用SimpleAdapter适配器。

如果是item是其他的布局,那么就在对应的是适配器中添加AlphabetIndexer,让其快速的滑动时,显示对应的索引值。

源码下载http://download.csdn.net/detail/forwardyzk/8380273


效果图:




很抱歉,我无法提供C语言代码。但是,我可以向您介绍如何使用Node.js模块来创建、读取、更新和删除macOS上的用户联系人数据库。 1. 创建通讯录 要创建通讯录,您需要使用Node.js模块node-mac-contacts。首先,您需要安装该模块。在终端中运行以下命令: ``` npm i node-mac-contacts ``` 安装完成后,您可以使用以下代码创建一个联系人: ```javascript const contacts = require('node-mac-contacts'); const newContact = { firstName: 'John', lastName: 'Doe', email: 'johndoe@example.com', phone: '123-456-7890' }; contacts.addContact(newContact, (err, contact) => { if (err) { console.log(err); } else { console.log('Contact created:', contact); } }); ``` 2. 显示通讯录 要显示通讯录,您可以使用以下代码: ```javascript const contacts = require('node-mac-contacts'); contacts.getAllContacts((err, contacts) => { if (err) { console.log(err); } else { console.log('Contacts:', contacts); } }); ``` 3. 查询通讯录 要查询通讯录,您可以使用以下代码: ```javascript const contacts = require('node-mac-contacts'); const query = { firstName: 'John', lastName: 'Doe' }; contacts.findContacts(query, (err, contacts) => { if (err) { console.log(err); } else { console.log('Contacts:', contacts); } }); ``` 4. 修改通讯录 要修改通讯录,您可以使用以下代码: ```javascript const contacts = require('node-mac-contacts'); const query = { firstName: 'John', lastName: 'Doe' }; const update = { email: 'newemail@example.com' }; contacts.updateContact(query, update, (err, contact) => { if (err) { console.log(err); } else { console.log('Contact updated:', contact); } }); ``` 5. 添加通讯录 要添加通讯录,您可以使用第1步中的代码。 6. 删除通讯录 要删除通讯录,您可以使用以下代码: ```javascript const contacts = require('node-mac-contacts'); const query = { firstName: 'John', lastName: 'Doe' }; contacts.deleteContact(query, (err) => { if (err) { console.log(err); } else { console.log('Contact deleted'); } }); ``` 7. 退出系统 要退出系统,您可以使用以下代码: ```javascript process.exit(); ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值