Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

Java How to separate the function ?

views
     
TSnarf03
post Nov 9 2018, 05:21 AM, updated 6y ago

Look at all my stars!!
*******
Senior Member
4,544 posts

Joined: Dec 2004
From: Metro Prima, Kuala Lumpur, Malaysia, Earth, Sol


CODE

        listView.setOnItemClickListener(new OnItemClickListener() {

                     @Override
                     public void onItemClick(AdapterView<?> parent, View view,
                       int position, long id) {
                       
                      // ListView Clicked item index
                      int itemPosition     = position;
                     
                      // ListView Clicked item value
                      String  itemValue    = (String) listView.getItemAtPosition(position);
                         
                       // Show Alert
                       Toast.makeText(getApplicationContext(),
                         "Position :"+itemPosition+"  ListItem : " +itemValue , Toast.LENGTH_LONG)
                         .show();
                   
                     }
             });


in the code above, it create a new "OnItemClickListener() " then instantly have to create the function body, how do i tell it to use other function ? like i have 2 listview that perform the same thing, i dont want to duplicate the code twice, so i create a function, then pass this function into the 2 listview's setOnItemClickListener
malleus
post Nov 9 2018, 08:35 AM

Look at all my stars!!
*******
Senior Member
2,096 posts

Joined: Dec 2011
CODE

private void doSomething(int position, ListView listView) {
 //go do something
}


TSnarf03
post Nov 9 2018, 03:26 PM

Look at all my stars!!
*******
Senior Member
4,544 posts

Joined: Dec 2004
From: Metro Prima, Kuala Lumpur, Malaysia, Earth, Sol


QUOTE(malleus @ Nov 9 2018, 08:35 AM)
CODE

private void doSomething(int position, ListView listView) {
 //go do something
}

*
then how to bind the function to OnItemClickListener ?


in vb.net i do like this
CODE

Sub TestEvents()
   AddHandler Obj.Ev_Event, AddressOf EventHandler  <------ this is what im looking for
   Obj.CauseSomeEvent()
   RemoveHandler Obj.Ev_Event, AddressOf EventHandler <-------- this too, to unbind
   Obj.CauseSomeEvent()
End Sub

Sub EventHandler()
   MsgBox("EventHandler caught event.")
End Sub

alphasp
post Nov 9 2018, 05:04 PM

New Member
*
Junior Member
39 posts

Joined: May 2005
Just create an instance of OnItemClickListener
CODE

private OnItemClickListener myOnItemClickListener = new OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    //
  }
};

// listView.setOnItemClickListener(myOnItemClickListener);

life4
post Nov 9 2018, 08:38 PM

Getting Started
**
Junior Member
211 posts

Joined: Apr 2012
just create a listener class and reuse the same class only.
malleus
post Nov 9 2018, 09:35 PM

Look at all my stars!!
*******
Senior Member
2,096 posts

Joined: Dec 2011
QUOTE(narf03 @ Nov 9 2018, 03:26 PM)
then how to bind the function to OnItemClickListener ?
for my suggestion, you do it like this:

CODE

listView.setOnItemClickListener(new OnItemClickListener() {
 @Override
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
   doSomething(position, listView);
 }
}


but the suggestion from alphasp to create a custom instance of OnItemClickListener is probably tidier
TSnarf03
post Nov 9 2018, 10:02 PM

Look at all my stars!!
*******
Senior Member
4,544 posts

Joined: Dec 2004
From: Metro Prima, Kuala Lumpur, Malaysia, Earth, Sol


QUOTE(malleus @ Nov 9 2018, 09:35 PM)
for my suggestion, you do it like this:

CODE

listView.setOnItemClickListener(new OnItemClickListener() {
 @Override
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
   doSomething(position, listView);
 }
}


but the suggestion from alphasp to create a custom instance of OnItemClickListener is probably tidier
*
Still don't get what I mean, I have a function named fct001, it has 1000 lines of code, now I have 10 buttons, all 10 buttons I need to call this function and don't repeat those 1000 lines of code. In your example the overrides section will be repeat 10 times, can that be avoided? Can it be made 1 liner?

This post has been edited by narf03: Nov 9 2018, 10:04 PM
malleus
post Nov 9 2018, 10:40 PM

Look at all my stars!!
*******
Senior Member
2,096 posts

Joined: Dec 2011
QUOTE(narf03 @ Nov 9 2018, 10:02 PM)
Still don't get what I mean, I have a function named fct001, it has 1000 lines of code, now I have 10 buttons, all 10 buttons I need to call this function and don't repeat those 1000 lines of code. In your example the overrides section will be repeat 10 times, can that be avoided? Can it be made 1 liner?
*
Then go with the suggestion from alphasp

Declare a single instance of OnItemClickListener and do setOnItemClickListener on your buttons to that instance.

But if you do this, you'll then need to have a way to track which button is it that's clicked on
TSnarf03
post Nov 10 2018, 04:19 AM

Look at all my stars!!
*******
Senior Member
4,544 posts

Joined: Dec 2004
From: Metro Prima, Kuala Lumpur, Malaysia, Earth, Sol


QUOTE(mistercoder @ Nov 10 2018, 12:34 AM)
You really got patience with this guy. He totally lacks foundations and dabble with things above his paygrade and would not be grateful or appreciate or even recognise solutions given to him.
*
seems like you really have problem on me, im not trying to solve a problem, im trying to improve or tune the language to my style of programming, i already said what i want at the beginning, those code works, from the very beginning, but i dont like it, i want another style.
silkworm
post Nov 10 2018, 01:02 PM

Enthusiast
Group Icon
Elite
962 posts

Joined: Jan 2003
From: Kajang


QUOTE(narf03 @ Nov 9 2018, 05:21 AM)
CODE

        listView.setOnItemClickListener(new OnItemClickListener() {

                     @Override
                     public void onItemClick(AdapterView<?> parent, View view,
                       int position, long id) {
                       
                      // ListView Clicked item index
                      int itemPosition     = position;
                     
                      // ListView Clicked item value
                      String  itemValue    = (String) listView.getItemAtPosition(position);
                         
                       // Show Alert
                       Toast.makeText(getApplicationContext(),
                         "Position :"+itemPosition+"  ListItem : " +itemValue , Toast.LENGTH_LONG)
                         .show();
                   
                     }
             });


in the code above, it create a new "OnItemClickListener() " then instantly have to create the function body, how do i tell it to use other function ? like i have 2 listview that perform the same thing, i dont want to duplicate the code twice, so i create a function, then pass this function into the 2 listview's setOnItemClickListener
*
This is a textbook implementation of an Anonymous Class. It's a legitimate style to use when that particular implementation of "OnItemClickListener" has no intention of being used elsewhere.

You have to "instantly create the function body" because, in actuality what's going on here is:
1. A new anonymous class that extends "OnItemClickListener" is being defined.
1a. "OnItemClickListener" may be an interface or abstract class, that cannot be instantiated into an object until the abstract method "onItemClick" is defined.
2. The anonymous class is instantiated into an object by the "new" operator, and the object is injected into the listView via a setter method.

If there were multiple "listView" in the same Java class that you wanted to have the same "onItemClick" behaviour, you can pass around the same OnItemClickListener object by giving it a handle (by assigning it to a variable). This was the solution suggested by alphasp.

However, if the many "listView" are in different classes (.java files), you'd want to promote the OnItemClickListener implementation into a standalone class, as suggested by life4.

Some Java concepts to take away here:
1. Classes vs Objects
2. Abstract types such as Interfaces and Abstract Classes
3. Nested Classes (the category that Anonymous Classes belong to)
4. Polymorphism and Encapsulation, which are the main OO concepts at play here.
TSnarf03
post Nov 10 2018, 06:50 PM

Look at all my stars!!
*******
Senior Member
4,544 posts

Joined: Dec 2004
From: Metro Prima, Kuala Lumpur, Malaysia, Earth, Sol


still struggle making things work, wondering how do i get the "name" of a button, like btn1 -> btn9, accessing id will give me integer instead.

user posted image


bumpo
post Nov 12 2018, 12:21 PM

On my way
****
Junior Member
632 posts

Joined: Mar 2013


QUOTE(narf03 @ Nov 9 2018, 10:02 PM)
Still don't get what I mean, I have a function named fct001, it has 1000 lines of code, now I have 10 buttons, all 10 buttons I need to call this function and don't repeat those 1000 lines of code. In your example the overrides section will be repeat 10 times, can that be avoided? Can it be made 1 liner?
*
the "doSomething(position, listView);" function is where you put your 1000 lines of code.
your 10 buttons or more call doSomthing. the 1k lines of code is not repeated

if my guess is correct, the part that is giving you problem in implementing those suggested solutions is this line "String itemValue = (String) listView.getItemAtPosition(position);" ? hmm.gif
TSnarf03
post Nov 12 2018, 02:23 PM

Look at all my stars!!
*******
Senior Member
4,544 posts

Joined: Dec 2004
From: Metro Prima, Kuala Lumpur, Malaysia, Earth, Sol


QUOTE(bumpo @ Nov 12 2018, 12:21 PM)
the "doSomething(position, listView);" function is where you put your 1000 lines of code.
your 10 buttons or more call doSomthing. the 1k lines of code is not repeated

if my guess is correct, the part that is giving you problem in implementing those suggested solutions is this line "String  itemValue    = (String) listView.getItemAtPosition(position);" ?  hmm.gif
*
Temporary got it working, using recycled view atm.
malleus
post Nov 12 2018, 10:28 PM

Look at all my stars!!
*******
Senior Member
2,096 posts

Joined: Dec 2011
QUOTE(narf03 @ Nov 10 2018, 06:50 PM)
still struggle making things work, wondering how do i get the "name" of a button, like btn1 -> btn9, accessing id will give me integer instead.

user posted image
*
While not directly related to your question, you might find this useful: https://github.com/JakeWharton/butterknife

It may help you resolve some of the things that you're trying to figure out

TSnarf03
post Nov 12 2018, 10:55 PM

Look at all my stars!!
*******
Senior Member
4,544 posts

Joined: Dec 2004
From: Metro Prima, Kuala Lumpur, Malaysia, Earth, Sol


QUOTE(malleus @ Nov 12 2018, 10:28 PM)
While not directly related to your question, you might find this useful: https://github.com/JakeWharton/butterknife

It may help you resolve some of the things that you're trying to figure out
*
might work, will try, currently researching on dynamically creating controls, dynamically bind them to their events.
malleus
post Nov 12 2018, 11:51 PM

Look at all my stars!!
*******
Senior Member
2,096 posts

Joined: Dec 2011
QUOTE(narf03 @ Nov 12 2018, 10:55 PM)
might work, will try, currently researching on dynamically creating controls, dynamically bind them to their events.
*
I just remembered you're using Kotlin. Check this out as an alternative to using butterknife: https://kotlinlang.org/docs/tutorials/android-plugin.html

TSnarf03
post Nov 13 2018, 01:18 AM

Look at all my stars!!
*******
Senior Member
4,544 posts

Joined: Dec 2004
From: Metro Prima, Kuala Lumpur, Malaysia, Earth, Sol


QUOTE(malleus @ Nov 12 2018, 11:51 PM)
I just remembered you're using Kotlin. Check this out as an alternative to using butterknife: https://kotlinlang.org/docs/tutorials/android-plugin.html
*
it doesnt matter, they are the same, any java code that being paste into the project will instantly get translated without any work.
malleus
post Nov 14 2018, 07:32 PM

Look at all my stars!!
*******
Senior Member
2,096 posts

Joined: Dec 2011
QUOTE(narf03 @ Nov 13 2018, 01:18 AM)
it doesnt matter, they are the same, any java code that being paste into the project will instantly get translated without any work.
*
Not exactly in this case. I don’t think the kotlin extensions will work if your activity or fragment is in Java

 

Change to:
| Lo-Fi Version
0.0156sec    0.37    5 queries    GZIP Disabled
Time is now: 28th March 2024 - 05:44 PM