我有我的类MainActivity,它扩展了ListActivity,我通过onResume()
方法中的自定义适配器将适配器设置为列表。
@Override
protected void onResume()
{
super.onResume();
this.setListAdapter(null);
adapter=new ContactListAdapter(this, R.id.txvNNPersonId, helper.getContacts(sortByname));
setListAdapter(adapter);
}
处理列表项单击,我使用此
protected void onListItemClick(ListView l, View v, int position, long id)
{
RelativeLayout r=(RelativeLayout)v;
TextView txvPersonId=(TextView)r.getChildAt(0);
int iid=Integer.parseInt(txvPersonId.getText().toString());
Intent intent=new Intent(this,ViewContactActivity.class);
intent.putExtra("id", iid);
startActivityForResult(intent, VIEW_RECORD);
//these is not working, these event is not raised, when clicked on listitem click
}
但是当通过鼠标单击/触摸项目时它不起作用,它仅在我选择项目时使用它并且实现onItemSelectedListener
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
RelativeLayout r=(RelativeLayout)arg1;
TextView txvPersonId=(TextView)r.getChildAt(0);
int id=Integer.parseInt(txvPersonId.getText().toString());
Intent intent=new Intent(this,ViewContactActivity.class);
intent.putExtra("id", id);
startActivityForResult(intent, VIEW_RECORD);
}
这只适用于模拟器上的箭头键,但在实际设备上,(通常)它们没有箭头/导航键,如何定义点击列表项?
编辑:row_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:longClickable="true"
android:orientation="horizontal">
<TextView android:id="@+id/txvPersonId"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:visibility="invisible" /> -->
<!-- this is name -->
<TextView android:id="@+id/txvPersonName"
android:layout_marginLeft="5dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center_vertical"
android:text="asd"
android:textSize="15sp"
android:paddingBottom="5dp" />
</RelativeLayout>`