Reputation: 1378
I want to create a function that will perform some operation(most time occurring) I created function like following
public void doSth()
{
//logic
ClassName.staticMethod();
//logic
}
In My application there are many times this function will be called. Only the particular line will be change. I decided to give a common function.
Now my question is: How do I pass the ClassName in function arguments so that function body use it dynamically?
Thanks
Upvotes: 1
Views: 8703
Reputation: 3364
Make use of getcount(),getitem() methods in your adapter to achieve this: Here list is the datasource you are passing to ur adapter,
public int getCount() {
int count=0;
for(int i=0;i<list.size();i++)
{
if(//ur condition to include the item)
{
count++;
}
}
return count;
}
@Override
public Object getItem(int position) {
if(//ur condition to include the item)
{
return list.get(position);
}
else
{
return null;
}
}
and in getview()
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.urlayout, parent, false);
if(list.get(position)!=null) {
}
return convertView;
}
Upvotes: 0
Reputation: 985
The more elegant solution would be to use strategy pattern. The code would change to
void f(X x) {
// some code
IStrategy strategy = decideStrategy(x);
strategy.method();
// rest of the logic
}
Upvotes: 1
Reputation: 420951
I'd recommend you to consider refactoring your code and specify interface as argument type.
You could for instance use a Runnable
and the simply do arg.run()
instead of ClassName.staticMethod()
.
Example:
public void doSth(Runnable action) {
// logic
action.run();
// logic
}
Upvotes: 1
Reputation: 283
use java's reflection mechanism
public void someFunction() {
this.getClass().getName();// returns name of class
}
so this way you don't need to pass any arguments
Upvotes: -1
Reputation: 97805
You'll probably want to pass the class directly, not the its name (otherwise use Class.forName
). Then it's just a matter of calling it using reflection:
public void doSth(Class<?> clazz) throws NoSuchMethodException,
IllegalAccessException, IllegalArgumentException,
InvocationTargetException {
Method method = clazz.getMethod("staticMethod");
if (Modifier.isStatic(method.getModifiers())) {
Object result = method.invoke(null);
//do sth with result
} else {
// ...
}
}
Upvotes: 2
Reputation: 15486
You could pass two strings, one with class and one with method name.
Then you just invoke with Class.forname(classname).getMethod(classname, null).invoke(null, null)
.
EDIT: This works only if the method is static and has no arguments (else you would replace the nulls with other values).
EDIT: Other Option you got (as mentioned strings for classes and methods are not nice), is to declare an interface and make all Classes with staticMethod implement it (if necessary a wrapper method to call the real static method for the classes, if the staticMethod name is not equal in all classes) and then you just use the interface-Type as parameter.
Upvotes: 1
Reputation: 1074038
You can do that, via Class.forName
which accepts a fully-qualified class name and returns a Class
instance. Then you have to get the Method
for the static method in question via getMethod
, and invoke it via invoke
.
But passing around class names as strings is a suspect design decision. I'd look at alternatives, such as using singletons rather than static methods and an interface, that sort of thing.
Upvotes: 5