mayur rahatekar
mayur rahatekar

Reputation: 4460

How to Set On click Listener On the Custom List view in android

I am trying to set On Click Listener on the List view. I have used the View holder and Base Adapter for Inflating the List view . I have used following Code:: Myonclicklistneer

myonclicklistneer = new Myonclicklistneer();
listView.setOnItemClickListener(myonclicklistneer); 

//Listneer Is 
class Myonclicklistneer implements OnItemClickListener
    {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long arg3) {
            Log.i("MyLog", "DONE DONE Listneer Is set!!!");

        }

    }

What is my problem that Listneer is setting and I am Inflating 3 Text View and 1 EditText.Whenever I click on any of the widgets in the row Listener has to be set.

Upvotes: 3

Views: 7883

Answers (2)

kaspermoerch
kaspermoerch

Reputation: 16570

As I understand it, you have created your own custom Adapter by extending BaseAdapter.

Instead of setting an OnItemClickListener you should set an OnClickListener on each view inside the getView()-method of your Adapter.

Add the OnClickListener to your Adapter-class as an inner class:

private class MyOnClickListener implements OnClickListener
{
  @Override
  public void onClick(View view) {
     //Do what needs to be done.
  }
}

Then inside your getView()-method:

public View getView() {
   //Inflate the view and get references to your TextViews/EditText.
   TextView text = (TextView) view.findViewById( R.id.myListItemTextView );

   //Set the onClickListener for each of the TextViews/EditTexts.
   text.setOnClickListener( new MyOnClickListener() );
}

Upvotes: 4

Lalit Poptani
Lalit Poptani

Reputation: 67286

Add android:focusable="false" to other view of the ListView to make ListView Clickable.

Upvotes: 5

Related Questions