public void onListItemClick (ListView parent, View view, int position, long id){ Toast.makeText(getApplicationContext(), Chapter8.items[position], Toast.LENGTH_SHORT).show(); }
弱弱问句:Chapter8.items[postion]怎么理解的?
随便给的一个例子 public void onListItemClick (ListView parent, View view, int position, long id){
//int position 是点击选中的列表的index }
真是感谢了,搞定了。 /** * when click one,show the title * @param l * @param v * @param postion * @param id */ @Override protected void onListItemClick (ListView l, View v, int position, long id) {
if(null == v){
Log.e(_TAG,"onListItemClick : paramter : v is null"); }else{ //get the _TITLE of the clicked item HashMap<String,Object> item = (HashMap<String,Object>)l.getItemAtPosition(position); String title = (String)item.get(_TITLE); Toast.makeText(getApplicationContext(), "Kick it !" + title, Toast.LENGTH_SHORT).show(); } }
ListView中的每一项都可以看出是一个对象,而每个对象的类型由ListView关联的适配器XXXAdapter的参数确定。例如:Cursor cursor = managedQuery(getIntent().getData(), PROJECTION, null, null, DiaryColumns.DEFAULT_SORT_ORDER); //构建适配器 SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.diary_row, cursor, new String[] { DiaryColumns.TITLE, DiaryColumns.CREATED }, new int[] { R.id.text1, R.id.created }); setListAdapter(adapter);那么,在重写方法onListItemClick(...)时,便需要将获取到的ListView中的项类型定义为Cursor类型 如下Cursor c = (Cursor)l.getItemAtPosition(position); String s = c.getString(1);
Toast.makeText(getApplicationContext(), Chapter8.items[position], Toast.LENGTH_SHORT).show();
}
public void onListItemClick (ListView parent, View view, int position, long id){
//int position 是点击选中的列表的index
}
* when click one,show the title
* @param l
* @param v
* @param postion
* @param id
*/
@Override
protected void onListItemClick (ListView l, View v, int position, long id) {
if(null == v){
Log.e(_TAG,"onListItemClick : paramter : v is null");
}else{
//get the _TITLE of the clicked item
HashMap<String,Object> item = (HashMap<String,Object>)l.getItemAtPosition(position);
String title = (String)item.get(_TITLE);
Toast.makeText(getApplicationContext(), "Kick it !" + title, Toast.LENGTH_SHORT).show();
}
}
null, DiaryColumns.DEFAULT_SORT_ORDER);
//构建适配器
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.diary_row, cursor, new String[] { DiaryColumns.TITLE,
DiaryColumns.CREATED }, new int[] { R.id.text1,
R.id.created });
setListAdapter(adapter);那么,在重写方法onListItemClick(...)时,便需要将获取到的ListView中的项类型定义为Cursor类型
如下Cursor c = (Cursor)l.getItemAtPosition(position);
String s = c.getString(1);
Toast.makeText(ActivityMain.this, "" + s, Toast.LENGTH_LONG).show();总的来说,如若需要重写ListView中的onListItemClick(即响应该项被单击),获取该具体项时,其类型由构造ListView的适配器决定。