"ActivityName.this" How to convert C#? - android

i like use a ActivityName.this (Java Code)
How to convert to C#(Xamarin)?
"this" is not a member of ActivityName
thanks.
public static class a{
public staric void DoWork(Context context){}
}
class b{
a.DoWork((Context)ActivityName)<- error is type but is used like a variable
a.DoWork(ActivityName) <- error is type but is used like a variable
a.DoWork(ActivityName.Context) <-no member Conttext
a.DoWork(ActivityName.this)<-no member this
}
class b is not Activity so this Just not available

There is no this word for Activity.
If you write your app in Xamarin you should use just this keyword.
Please see this, I am creating new CustomProgressDialog object:
CustomProgressDialog _customProgressDialog = new CustomProgressDialog(this);
Hope this will help.

Maybe this could be helpful for you -> For example if you have this code in Java
orientationEventListener = new OrientationEventListener(this) {
#Override
public void onOrientationChanged(int orientation) {
MainActivity.this.onOrientationChanged(orientation);
}
};
You could transfer it to this one in C# (Xamarin)
var myOrientationListener = new MyListener();
myOrientationListener.OrientationChanged += (s, e) => {
MyActivity.onOrientationChanged(e);
};
For a whole example, take a look at this forum post http://forums.xamarin.com/discussion/32576/need-help-porting-java-to-c-how-to-handle-anonymous-inner-class

Related

how to use interface for communicatiion among classes and activities

I have a class and an activity. I need to implement an interface to track whenever there is a change in a variable in class then it should reflect on activity. Can someone please explain how to use interface for this purpose?
You can use this code for reference, create interface to trace event, implement that interface in your activity and set its call back from change var value event of your class.
interface ChangeListener{
public void onVarChanged(String value);
}
class Abc{
String var ="";
ChangeListener changeListeners;
public Abc(ChangeListener changeListeners){
this.changeListeners = changeListeners;
}
public void setVar(String str){
var = str;
changeListeners.onVarChanged(var);
}
}
class MyActivity extends Activity implements ChangeListner{
/*
your stuff...........
*/
onCreate(...){
Abc abc = new Abc(this);
abc.setVar("My new string");
}
#Override
public void onVarChanged(String value)
{
Log.v("","==== Variable is changed ==== "+value);
}
}
Observer pattern is the right fit here. Have a look at this http://developer.android.com/reference/java/util/Observable.html . The activity will be an Observer and the other class will be the Observable

is it possible to pass a class<protocol> as parameter?

Actually I want to create a function that will be able to handle multiple types of ArrayList.
public void someFunction(Class ArrayList<protocol>) {
ArrayList<?> object = new ArrayList<protocol>;
}
Something like that, any advice?
May be it help u..
public class TestClass {
public void function(List<? extends Object> temp){
ArrayList<? extends Object> obj=(ArrayList<? extends Object>) temp;
}
}
Then you may call this function in this way using different type of parameter.
TestClass test= new TestClass();
test.function(new ArrayList<String>());
test.function(new ArrayList<Integer>());
test.function(new ArrayList<TestClass>());
I think you are probably looking for the power of generics, something like this might help:
public class MyClass<T>
{
public void someFunction(ArrayList<T> myArray)
{
... //do anything with generic T array.
}
}
Then you could easily instantiate MyClass, and call method someFunction with compile-safe coding.
Good Luck

What is the data type of 'MyActivity.this' - Android

well my question is that there aren't pointers in JAVA ...
but when we have to start another activity we do like this :
Intent in = new Intent(MyActivity.this, NewActivity.class);
startAcitivity(in);
So my question is that what is the data type of MyActivity.this ??
In java pointers are not explicitly allowed,
However passing by reference(object) in Java is something which is implicitly based on pointer concept.
In your case, you are passing the context of parent class to child class,
which is actually passing by reference concept.
Hope this helps.
Writing MyActivity.this is the same as writing this, if you are in a non-nested class, or to top-level class.
See this example:
public class TopLevel{
public static void main(String[] args){
new TopLevel().printClass();
}
public TopLevel(){
new LowerLevel().printClass();
}
public void printClass(){
System.out.println("Outer Class: ");
// Will print something like "TopLevel.class"
System.out.println(this.getClass());
}
public class LowerLevel{
// This is a Nested-Class.
public void printClass(){
System.out.println("Nested Class: ");
// Will print "TopLevel$LowerLevel.class"
System.out.println(this.getClass());
// Will print "TopLevel.class" again
System.out.println(TopLevel.this.getClass());
}
}
}
Some using this in the nested-class does not reference to the same instance as when using it in the top-level class. Therefor, to get the "context" of the outer class in your nested class, you also specify the class you want the this-context from.

how to pass, from a class to another, the name of a variable (NOT the value)

I need to pass name of a variable created in Class A to the Class B, so I can put a value in that variable (in Class B).
But, in Class B I do not know the name of that variable.
The code is something like this:
Class A
public class A {
int valore; // this is the variable, in Class b, I don't know this name!
public void callClassB(){
ClassB.Method(what shoudld i put here?)
}
}
This is the Class B
public class B {
public void Method(the_Name_Of_TheVariable_I_get){
the_Name_Of_TheVariable_I_get = 5; // i need to do this
}
}
Why do you need the variable name? Simply pass the variable itself. In class B create a method
public int getValore(){
return valore;
}
Then in Class A use modify the code as
public void callClassB(){
ClassB.Method(getValore())
}
I do not really understand what you are trying to achieve here?
You can also use the following appraoch:
interface ValueSetter {
void setValue(int value);
}
Class A
public class A implements ValueSetter{
int valore;
public void callClassB(){
ClassB.Method(this)
}
void setValue(int value){
valore = value;
}
}
This is the class B
public class B{
public void Method(ValueSetter valueSetter){
ValueSetter.setValue(5);
}
}
This is more inline with OOPS..
You will need to use reflection for this.
Here is a tutorial from Oracle: http://docs.oracle.com/javase/tutorial/reflect/index.html
You cant get the name of variable at runtime though. But assuming you have the name of the field the code would look something like this:
this.getClass().getDeclaredField(the_Name_Of_TheVariable_I_get).set(this, 5);
you can pass the name of the variable "valore", then you need reflection to assign it in your method :
a = new A();
Field f = a.getClass().getDeclaredField(varName);
f.set(a, 5);
a can be a parameter too. (it is necessary to give the instance that possesses the member).
However, this is not a recommended way of treating your issue, as it is unreliable in the sense that the compiler will not be able to check you are accessing items that actually exist.
It would be better to use an interface, for instance :
public interface Settable {
public void set(int value);
}
and then:
public class A implements Settable {
private int valore;
public void set(int value) {
valore = value;
}
public void callClassB(){
ClassB.Method(this);
}
}
and in B:
public class B{
public void Method(Settable settable){
settable.set(5);
}
}

getstring() from a call without extend activity

I have a problem with a simple class that contains information that I will like to call from another class. For example here is the class Util which contains the info:
public class Util{
public ArrayList<RecetaBean> getRellenas() {
ArrayList<RecetaBean> MiLista = new ArrayList<RecetaBean>();
RecetaBean receta1 = new RecetaBean();
String ingrediente1[] = { getString(R.string.app_name),getString(R.string.app_name),
};
receta1.setIngredientesLista(ingrediente1);
MiLista.add(receta1);
MiLista.add(receta1);
MiLista.add(receta1);
return MiLista;
}
}
Then in another class I get the Items calling like this:
Util u = new Util();
ArrayList<RecetaBean> Recetas = u.getRellenas();
So, I have a execution problem in the class Util with the GETSTRING, because I would like to get a different string (because of different languages). The way to quit the error is to extend the class Util from Activity, but Util is not an Activity! And if I extend from Activity, the app crash.
all you need to do is Define a Context in the Method .
and call it like this;
Util u = new Util();
ArrayList<RecetaBean> Recetas = u.getRellenas(this);
and you the context in you Methods like this:
public ArrayList<RecetaBean> getRellenas(Context con) {
ArrayList<RecetaBean> MiLista = new ArrayList<RecetaBean>();
RecetaBean receta1 = new RecetaBean();
String ingrediente1[] = { con.getString(R.string.app_name), con.getString(R.string.app_name),
};
receta1.setIngredientesLista(ingrediente1);
MiLista.add(receta1);
MiLista.add(receta1);
MiLista.add(receta1);
return MiLista;
}
have you thought about using a string array? It would be so much simpler than what you are trying to do.
I do not think that your concerns about language change are justified, in terms of the overall user experience.

Categories

Resources