How can I start getdata() method to another class. I want to show the output to a `ListView which is in another Java class.
tab1Background.java
public class tab1Background extends Activity{
ListView lv;
lv = (ListView) findViewById(R.id.lv);
//some code here
public void getdata() {
//some code here
}
}
tab1report.java i want to start the getdata() activity to show data in listview
public class tab1report extends Fragment {
//some code here
}
public class tab1report extends Fragment {
public void startSomething()
{
((tab1Background) getActivity()).getdata();
}
}
You could use callback method.
public class tab1report extends Fragment {
Callback callback;
public interface Callback {
void call();
}
onAttach(Context context) {
callback = (Callback) context;
}
onStart() {
// Some code here
callback.call();
}
}
public class tab1Background extends Activity implements tab1report.Callback {
ListView lv;
lv = (ListView) findViewById(R.id.lv);
//some code here
public void getdata() {
//some code here
}
public void call() {
getdate();
}
}
This will ensure that the functionality is passed back to the activity.
Related
I have an Activity which creates a class that does some work. What is the typical Android method of having this class report back to the Activity in order to update the UI?
My activity, which creates the class:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyClass obj = new MyClass(this);
obj.DoWork();
}
}
The class that does the work, and wants to report back some
public class MyClass(Context context) {
private Context context;
public void DoWork() {
//Do some work with a countdown timer
//Report back some values
}
}
You can create your own interface like this:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyClass obj = new MyClass(this);
obj.setOnWorkDoneListener(new MyClass.OnWorkDoneListener(){
#Override
public void onDone(Values values) {
//Work done, use values
updateUI(values);
}
});
obj.DoWork();
}
}
public class MyClass(Context context) {
private Context context;
public interface OnWorkDoneListener{
void onDone(Values values);
}
private OnWorkDoneListener listener;
public void setOnWorkDoneListener(OnWorkDoneListener listener){
this.listener = listener;
}
public void DoWork() {
//Do some work with a countdown timer
when(workEnded) listener.onDone(backValues);
}
}
I try to implement a timer using Work Manager.
"Timer button was hit" is appear in logcat, but nothing comes from worker. What I do wrong?
This is my ViewModel class:
public class MainViewModel extends ViewModel {
public static final String LOG_TAG = "MainActivity";
private final WorkManager workManager;
public MainViewModel(WorkManager workManager) {
this.workManager = workManager;
}
public void count() {
Log.d(LOG_TAG, "Timer button was hit!");
OneTimeWorkRequest request = new OneTimeWorkRequest.Builder(MainWorker.class).build();
workManager.beginUniqueWork("count-work", ExistingWorkPolicy.APPEND, request).enqueue();
}
}
This is my worker. Nothing appear in logcat from here. I don't know why.
public class MainWorker extends Worker {
public MainWorker(#NonNull Context context, #NonNull WorkerParameters workerParams) {
super(context, workerParams);
Log.d(MainViewModel.LOG_TAG, "Created");
}
#NonNull
#Override
public Result doWork() {
Log.d(MainViewModel.LOG_TAG, "Work start");
return Result.success();
}
}
This is my activity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MainViewModel viewModel = new MainViewModel(WorkManager.getInstance(this));
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.setMainViewModel(viewModel);
}
}
ViewModels are not initialized the way you did. If you have to pass something to ViewModel constructor, you should probably consider using ViewModelProvider.Factory().
You can make your own ViewModelProvider.Factory()
Refer this, https://medium.com/koderlabs/viewmodel-with-viewmodelprovider-factory-the-creator-of-viewmodel-8fabfec1aa4f#:~:text=And%20it%20is%20because%20you,it%20will%20create%20your%20ViewModel.
I want to call method of Other activity. I searched alot and tried by myself but failed. It work when i call from class to class like classA m=new classA();
m.function();
But i want to call from another activity method. Please help
public void onNotificationPosted(StatusBarNotification sbn) {
ChatActivity ch=new ChatActivity();
ch.location();
}
public class ApplicationContext extends Application {
public ActivityMain activityMain;
public void setActivityMain(ActivityMain activityMain) {
this.activityMain = activityMain;
}
public ActivityMain getActivityMain() {
return activityMain;
}
In ActivityMain.class
public class ActivityMain extends BaseProject implements MainView {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ApplicationContext.getInstance().setActivityMain(this);
}
#Override
protected void onDestroy() {
super.onDestroy();
ApplicationContext.getInstance().setActivityMain(null);
}
Use it anywhere like Activity, Fragments, Broadcast Receiver or Service like
if (ApplicationContext.getInstance().getActivityMain()!=null){
ApplicationContext.getInstance().getActivityMain().callAnyMethod();
}
Enjoy :)
So I have a custom listview. It's like Instagram layout with 1 image and bunch of buttons in each list items. So here's the problem:
I want to implement the share button. To do this, I tried to create a callback from adapter to activity. But it didn't seem to work. Here is what I have so far(I cropped out the unrelated parts):
MainActivity
public class MainActivity extends ActionBarActivity implements ListAdapter.OnShareClickedListener{
ListView main_list;
List<String> url_list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main_list = (ListView) findViewById(R.id.listView);
ListAdapter nListAdapter = new ListAdapter(context, url_list);
main_list.setAdapter(nListAdapter);
}
#Override
public void ShareClicked(String url) {
Log.e("Test",url);
}
}
ListAdapter
public class ListAdapter extends BaseAdapter implements View.OnClickListener {
OnShareClickedListener mCallback;
Context context;
public static List<String> url_list;
public ListAdapter(Context c, List<String> list) {
this.context = c;
url_list = list;
}
public interface OnShareClickedListener {
public void ShareClicked(String url);
}
#Override
public void onClick(View v) {
mCallback.ShareClicked("Share this text.");
}
}
}
Error Log:
Attempt to invoke interface method 'void com.packagename.ListAdapter$OnShareClickedListener.ShareClicked(java.lang.String)' on a null object reference
You need to tell the adapter which implementation of the OnShareClickedListener() to use. Right now in your adapter the field mCallback is never assigned to, either you need to have a setOnSharedClickedListener() method in your adapter which you then call from your mainActivity and set it with the main activity's implementation or you need to take in the constructor.
My suggestion would be to use a setter instead of constructor. So what you need to do is this.
Your ListAdapter
public class ListAdapter extends BaseAdapter implements View.OnClickListener {
OnShareClickedListener mCallback;
Context context;
public static List<String> url_list;
public ListAdapter(Context c, List<String> list) {
this.context = c;
url_list = list;
}
public void setOnShareClickedListener(OnShareClickedListener mCallback) {
this.mCallback = mCallback;
}
public interface OnShareClickedListener {
public void ShareClicked(String url);
}
#Override
public void onClick(View v) {
mCallback.ShareClicked("Share this text.");
}
}
Your MainActivty
public class MainActivity extends ActionBarActivity implements ListAdapter.OnShareClickedListener{
ListView main_list;
List<String> url_list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main_list = (ListView) findViewById(R.id.listView);
ListAdapter nListAdapter = new ListAdapter(this, url_list);
nListAdapter.setOnShareClickedListener(this);
main_list.setAdapter(nListAdapter);
}
#Override
public void ShareClicked(String url) {
Log.e("Test", url);
}
}
I have 3 classes with the same function. The class H is a controller class, so i have made a setactivity class to get the instance of the activity that called it. Now i have to call the function userleft() of the currently initialized class from the controller class H which is a non Activity.
The error i am getting is "The method userleft() is undefined for the type Activity" and i know that the exception would come, since the class Activity does not contain a method called userLeft(). But what is the best way to make this work?
See the structure of the app below.
public class A extends Activity {
void onCreate(Bundle savedInstanceState) {
H.getInstance().setactivity(this);
}
public void userleft() {
}
}
public class B extends Activity {
void onCreate(Bundle savedInstanceState) {
H.getInstance().setactivity(this);
}
public void userleft() {
}
}
public class C extends Activity {
void onCreate(Bundle savedInstanceState) {
H.getInstance().setactivity(this);
}
public void userleft() {
}
}
and i have another non activity class
public class H {
public static H getInstance() {
if (instance == null) {
instance = new H();
}
return instance;
}
public void setActivity(Activity activity) {
this.activityGame = activity;
}
public void randomCallBack(){
activityGame.userleft();
}
}
Thanks!
You need to redesign your classes like below
Declaire a base activity class
public abstract class BaseActivity extends Activity {
public abstract void userleft();
}
And declare your classes like
public class A extends BaseActivity {
void onCreate(Bundle savedInstanceState) {
H.getInstance().setactivity(this);
}
#Override
public void userleft() {
}
}
And now modify your Non-activity class like
public class H {
public static H getInstance() {
if (instance == null) {
instance = new H();
}
return instance;
}
public void setActivity(BaseActivity activity) {
this.activityGame = activity;
}
public void randomCallBack(){
activityGame.userleft();
}
}
Now you will be able to call your method for any subclass activity.
In your randomCallBack() method you have an Activity object, the class Activity does not contain a method called userLeft(), so it is normal that you get such an exception. So, you have two ways of solving this issue:
Either, you cast the activityGame to its corresponding class as:
public void randomCallBack(){
if (activityGame instanceof A) {
((A)activityGame).userleft();
}
if (activityGame instanceof B) {
((B)activityGame).userleft();
}
if (activityGame instanceof C) {
((C)activityGame).userleft();
}
}
Or, in a better way, you create an Interface, which is implemented by your clasess A, B, and C, and used by H class. Like this:
public interface UserLeft {
public void userleft();
}
public class A extends Activity implements UserLeft {
void onCreate(Bundle savedInstanceState) {
H.getInstance().setactivity(this);
}
public void userleft() {
}
}
public class B extends Activity implements UserLeft {
void onCreate(Bundle savedInstanceState) {
H.getInstance().setactivity(this);
}
public void userleft() {
}
}
public class C extends Activity implements UserLeft {
void onCreate(Bundle savedInstanceState) {
H.getInstance().setactivity(this);
}
public void userleft() {
}
}
public class H {
UserLeft activity;
public static H getInstance() {
if (instance == null) {
instance = new H();
}
return instance;
}
public void setActivity(UserLeft activity) {
this.activityGame = activity;
}
public void randomCallBack(){
activityGame.userleft();
}
}
Activity class does not have the method of userleft , so that you can't call that method by the reference activity . You can define one BaseActivity with the method userleft as the parent class of these classes.
You forgott your instance variable declaration :) private Activity activityGame = null;
Of course class Activity has no method called userleft().
You need to cast your variable activityGame to your specific Class like that:
public class H {
private Activity activityGame = null;
public static H getInstance() {
if (instance == null) {
instance = new H();
}
return instance;
}
public void setActivity(Activity activity) {
this.activityGame = (<CAST TO YOUR CLASS>) activity;
}
public void randomCallBack(){
activityGame.userleft();
}
}
EDIT:
Please try this:
public void setActivity(Activity activity) {
this.activityGame = (activity.getClass()) activity;
}