1

我有我的扩展 ListActivity 的 MainActivity 类,我将适配器设置为通过方法中的自定义适配器列出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>`
4

0 回答 0