can anyone tell me how to call another class which does not extend activity , I have searched in many forums I am not clear with that ...
Thanks in Advance
Based on your question and your response to other answer, i guess you want to do something like this,
class UploadImages
{
UploadImages()
{
}
void uploadImagesFunc()
{
//some operation;
}
}
//In Activity, do something like following
UploadImages mUploadImages = new UploadImages();
mUploadImages.uploadImagesFunc();
see this
class_name variable = new class_name();
variable.method_name();
Related
Hello I am struggling with a task that seems very simple. I need to write an expresso unit test but I need the activity to use a mocked class instead of the one it uses during the normal run.
the, the closest solution I saw was here:
Nullifying or overriding API calls made in an Activity under an Espresso test
But this requires getters and setters at the application level and only deals with constants.
Here is a simple example of want to do:
Activity:
public class MainActivity2 extends AppCompatActivity {
// how do I mock this without using any branches
// just have expresso replace with MainActivity2CustomClass with a mock?
private MainActivity2CustomClass mainActivity2CustomClass;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
mainActivity2CustomClass = new MainActivity2CustomClass();
mainActivity2CustomClass.foo();
}
}
the class I want to mock
// Need expresso to replace this class with a mock
public class MainActivity2CustomClass {
void foo() {
}
}
To clarify I need to do this with a large application with several classes. So using branches is NOT the solution I seek. For instance this will not work for my application
// I DO ***NOT** WANT THIS solution since it will explode in many branches in my application
Intent intent = new Intent(targetContext, PatientActivity.class);
intent.putExtra("istestrunning", 2);
// etc...
// Not acceptable solution for my problem, creates too many branches and messy code
if (getIntent().getIntExtra("istestrunning", 0) == 2) {
mainActivity2CustomClass = new MainActivity2CustomClass();
mainActivity2CustomClass.foo();
} else {
mainActivity2CustomClass2 = new MyMock();
mainActivity2CustomClass2.foo();
}
I want to know if it possible to have expresso simply replace that class at testing time. Can it be done?
Any help or leads is appreciated.
thank you.
Just started messing around with RxJava, so i am a total newbie at it.
I have a question about the correct approach or any possible solutions to the following problem. I have searched a lot on Google but i do not seem to find any appropriate solutions, just general ideas.
I have a class that does some work and has it's own interface, lets name it AClass. Now there is a BClass that implements the beforementioned interface. On the onActivityResult of BClass, if the requestCode matches the request code we provided, then the AClass calls aClassInstance.handleActivityResult(..) and does some things. Then it reports back using its interface.
How would someone approach this with RxJava Observables?
My only try at the moment is to transform the handleActivityResult(..) into an Observable and then implement an Observer interface and instantiating a subscription object in the overrided onActivityResult of BClass which apparently is an Activity. But i need to check if the subscription is null or unsubscribed before instantiating, which as a consequence instantiates the subscription only once, and then the Observer stops receiving any more objects.
I also read about RxBus implementations. I am not quite sure if that would be a good approach, cause it got me a little bit confused with the Subjects.
Thanks a lot in advance for any insights you can provide. :)
I dont really get an idea of what you really want, but try to help you.
Am I get you right? Your class AClassInstance whant to know, when the onActivityResult of BClass happened? If all like that, you don`t have to implement "Ainterface" by both classes. You need just listener of when "onActivityResult" will happens:
import rx.Observable;
public class ExampleObservers {
public static void main(String[] args) {
CallbackReceiver receiver = new CallbackReceiver();
ActivityProducer activity = new ActivityProducer();
activity.setListener(receiver);
activity.onActivityResult(444, 0);
}
}
class CallbackReceiver implements ActivityProducer.RequestCodeProducer {
#Override public void handleActivityResult(Observable<Integer> requestObservable) {
requestObservable.map(it -> {
System.out.println("here U can do some extra things with your " + it + " number ...");
return it;
}).subscribe(res -> System.out.println("Hello requestCode = " + res));
}
}
class ActivityProducer {
interface RequestCodeProducer {
void handleActivityResult(Observable<Integer> requestCode);
}
private RequestCodeProducer listener;
public void setListener(RequestCodeProducer listener) {
this.listener = listener;
}
/*
* . . . . . . .. .. . . .
* Your activity`s methods
* */
public void onActivityResult(int requestCode, int resultCode/*, Intent data*/) {
//super.onActivityResult();
Observable<Integer> observable = Observable.defer(() -> Observable.just(requestCode));
if (listener != null) {
listener.handleActivityResult(observable);
}
}
}
If you run this in IDE, you will see output "Hello requestCode = 444". So, all works fine, and you get your callback.
I am new in SO, and also new in Android. I try to make some chat application and I want to use this code
ListView list = (ListView) findViewById(R.id.listUser);
list.setAdapter(new UserAdapter());
list.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
startActivity(new Intent(UserList.this, Chat.class).putExtra(Const.EXTRA_DATA, uList.get(pos).getUsername()));
}
});
But the problem, there is error for Const.EXTRA_DATA because it can not be resolved and also uList.get(pos) too, I use Android Studio. I got that code from video tutorial.
from the comments you understood that Const is a predefined class, unfortunately it is not, there is no such class available in android library.
so your code will be like
startActivity(new Intent(UserList.this, Chat.class).putExtra("EXTRA_DATA_KEY", uList.get(pos).getUsername()));
since the key has to be used in both activities (sending activities and receiving activity) usually defines in a separate Java file.
Hello dear you are using your code snippet like
Const.EXTRA_DATA
right?
--> as you told us you are new SO please check your are having
class named
Const and
having static string variable like
EXTRA_DATA
-> SO finaly your class will appear like
public class Const{
public static String EXTRA_DATA="EXTRA_DATA";
}
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
I'm trying to call a function present in one class from another class by creating its object. Somehow it's not working. The new activity doesn't load.
My java code:
public class MessagesActivity extends TabActivity {
public WorkEntryScreenActivity workEntryObject = new WorkEntryScreenActivity() ;
public void AddWorkEntryClick(View v) {
workEntryObject.newWorkEntry();
}
}
The other class:
public class WorkEntryScreenActivity extends Activity {
public void newWorkEntry() {
try {
Intent i = new Intent(this, WorkEntryActivity.class);
i.putExtra("CurDate", mDateDisplay.getText());
i.putExtra("DD", String.valueOf(mDay));
i.putExtra("MM", String.valueOf(mMonth));
i.putExtra("YYYY", String.valueOf(mYear));
startActivity(i);
finish();
} catch (Exception e) {
System.out.println("Exception" + e.getStackTrace());
Log.d(TAG, "Exception" + e.getStackTrace());
}
}
}
You must create your workEntryObject first (it's not C++). Like this
public WorkEntryScreenActivity workEntryObject=new WorkEntryScreenActivity();
Also, I highly recommed you to read Android Basics
http://developer.android.com/guide/index.html
#biovamp is correct. It looks like you have a null reference that you're trying to call a method on. In order to call a non-static method, you need a instance of that object.
From the naming of your method, it looks like you might be trying to re-use some of your UI in another part of your application. In Android, the way to accomplish that is through Intents and Activities. If you're not familiar with those or how to use them, I would highly suggest researching them.