当前位置: 代码迷 >> Android >> AdapterView.OnItemSelectedListener.onItemSelected参数有关问题
  详细解决方案

AdapterView.OnItemSelectedListener.onItemSelected参数有关问题

热度:41   发布时间:2016-05-01 11:20:13.0
AdapterView.OnItemSelectedListener.onItemSelected参数问题
final String[] ContactsFliter = {Contacts.DISPLAY_NAME, BaseColumns._ID};
Cursor c = getContentResolver().query(Contacts.CONTENT_URI, ContactsFliter, null, null, null);
startManagingCursor(c);

ListAdapter adapter = new SimpleCursorAdapter(this,
         android.R.layout.simple_list_item_2,
         c, ContactsFliter,
         new int[]{android.R.id.text1, android.R.id.text2});
 m_ListView.setAdapter(adapter);
m_ListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
//请问,这里的arg3是什么东西?
}

问题在上面的注释里面,据文档介绍,arg3参数是:The row id of the item that is selected,但上面的例子中,arg3的值居然是数据库里面的BaseColumns._ID的值!百思不得其解。
注:我看的是2.2文档,但用的是2.1 API开发的。

------解决方案--------------------
帮顶,一直没使用过arg3
------解决方案--------------------
BaseColumns 
String _ID The unique ID for a row. 

The row id of the item that is selected
应该是说的同一个ID吧。
------解决方案--------------------
public abstract void onItemSelected (AdapterView<?> parent, View view, int position, long id) 

Since: API Level 1 Callback method to be invoked when an item in this view has been selected. Impelmenters can call getItemAtPosition(position) if they need to access the data associated with the selected item.

Parameters
parent  The AdapterView where the selection happened 
view  The view within the AdapterView that was clicked 
position  The position of the view in the adapter 
id  The row id of the item that is selected  

------解决方案--------------------
是pisition,就是第几个item吧。arg0是选的item,是个view对象。arg1是item的id号,从0开始,跟数组的id号类似。arg3就是item的位置。不知道准不准确。arg3一直没怎么用。你可以log输出看看,我觉得你之所以输出的是BaseColumns._ID的值,是因为arg3是从1开始的,你如果查的全表,那么他们的值就是相等的。你可以
log.e(TAG,"arg3 is"+(arg3+1));
log.e(TAG,"BaseColumns._ID is"+BaseColumns._ID);
看看是不是有区别