1

I have my class MainActivity which extends ListActivity, i set the adapter to list via custom adapter in onResume() method.

@Override
protected void onResume() 
{
    super.onResume();
    this.setListAdapter(null);
    adapter=new ContactListAdapter(this, R.id.txvNNPersonId, helper.getContacts(sortByname));       
    setListAdapter(adapter); 
}

to handle list item click, i use this

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
    }

but it does not work when click/touch on item via mouse, it only works when i use it with item selected and implementing 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);
}

and this only works with the arrow keys on emulator, but on real devices,(usually)they do not have arrow/navigation keys, how to define click for list item?

EDIT:the 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>`
Akhil Jain
  • 13,872
  • 15
  • 57
  • 93
  • 1
    Can you post the XML for your row? Just want to rule out that something in your row is stealing the focus/consuming your touch event instead of the row itself. – Brent Hronik Feb 20 '13 at 16:05
  • ... and if so (i mean if something is "stealing focus" (Clickable elements like Buttons or ImageButtons)) than this question is asked 100 a month – Selvin Feb 20 '13 at 16:07
  • which xml, do you want, main.xml or row_item.xml – Akhil Jain Feb 20 '13 at 16:12
  • row_item or just check if you don't have `Button` or `ImageButton` there – Selvin Feb 20 '13 at 16:14
  • And If you are trying to listen for a click of a row, you would want to implement an `OnItemClickListener` not an `OnItemSelectedListener`. The former is for `ListView`s while the latter is for `Spinner`s, with reference http://stackoverflow.com/questions/4177507/difference-between-onitemclicklistener-and-onitemselectedlistener-of-adapterview. – Brent Hronik Feb 20 '13 at 16:18
  • Are you trying to write code for touch events and mouse / trackball / keyboard navigation? – Sam Feb 20 '13 at 16:30
  • i want to have display detail activity when touched/tapped/clicked(emulator) on list item – Akhil Jain Feb 20 '13 at 16:31
  • And if you were to remove the OnItemSelectedListener, nothing would happen? – Brent Hronik Feb 20 '13 at 16:35
  • no currently i have it commented it and removed the interface for it – Akhil Jain Feb 20 '13 at 16:39

0 Answers0