Simulate a click from onitemclick

I already have onitemclickListener and it works,

 list.setOnItemClickListener( new OnItemClickListener() { public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){ ... } }); 

however, I need to implement the same functionality from a different button.

How can I reference the onitemclickListener that already exists for my list?

I do not want to copy all the code and use it again, since it is about 500 lines.

+6
source share
6 answers

Change your code in a separate method and call this method from the onItemClick and onClick .

If your code needs the selected item, pass it as an argument from onItemClick . In your onClick button onClick you will have to look at ListView.getCheckedItemIds , since in list mode several items can be selected at the same time, and apply your logic to the items you want based on any criteria.

+3
source

There is a performItemClick that does this.

 list.performItemClick(list.getChildAt(pos), pos, list.getItemIdAtPosition(pos)); 
+17
source

create your own OnItemClickListener class that uses the code you already wrote and use it in your calls to list.setOnItemClickListener(new myOnClickListener())

0
source

you need to create an OnItemClickListener object and use it when you need:

 OnItemClickListener listener = new OnItemClickListener(){ public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { ... } } 

and whenever you need:

 list.setOnItemClickListener(listener); 
0
source

Register your onItemClickListener in the xml layout and create a generic method in your activity.

0
source

I see two solutions: you can write a general function and call this function when any button is pressed. Another solution would be to get an idea of ​​the first button when you click on the second button and then call the performClick function.

 public void Click2() { Button b = (Button)findViewById(R.id.button1); b.performClick(); } 
0
source

All Articles