user552978
user552978

Reputation: 49

How can i attach an listener to listview?

I have a ListView with a custom layout for each row having one TextView and three EditText. When I am clicking on individual row in ListView one activity is started and that takes you to another page.

I wrote some code but its not working. The code is shown below.

In adapter class getView() method i have place the below code

   convertView = mInflater.inflate(R.layout.editcategorylist, null);
   convertView.setClickable(true); 
   convertView.setOnClickListener(clickListener);

and I declare the click listener in your ListActivity as follows

   lv=getListView();  
   myClickListener = new OnClickListener(){
       public void onClick(View v) {
           Intent intent = new Intent(CategoryList.this,AddSubCategoryList.class);
           startActivity(intent);    
       }
   };

Thank you.

Upvotes: 2

Views: 13024

Answers (2)

ingsaurabh
ingsaurabh

Reputation: 15269

Use something like this

lv.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 
{
 public void onItemSelected(AdapterView parentView, View childView, int position, long id) 
 {
    //Here write your code for starting the new activity on selection of list item
 }
 public void onNothingSelected(AdapterView parentView) 
 {
 }
});

Upvotes: 4

Jett Hsieh
Jett Hsieh

Reputation: 3159

Please use setOnItemClickListener

Upvotes: 3

Related Questions