public void PrintRecordToResultTA() {
int i = 0;
Log.d("data","\nCodec: " + avlRecordCollection.getCodecID());
Log.d("data","\nRecord Count: " + avlRecordCollection.getRecordCount());
I have used log to see if my program woks, but now I need to display this data on UI thread in MainActivity, this method was used to display data in java program, I was thinking should I recreate this class as Activity to reach data from another Activity to Main?
EDIT:
I have created ArrayList of AVL Records
public List<AVLRecord> avlRecords = new ArrayList<>();
public AVLRecordCollection CreateCollection() { // private
return new AVLRecordCollection(codec, recordC, avlRecords);
}
And method to Create Records, which get all data I need to display... And I use avlRecord.add(AVLRecord) to pass all data.
public void CreateRecord() {
AVLRecord AVLRecord;
RecordHeader recordHeader = GetRecord_Data();
RecordGPS_Element recordGPS_element = GetRecord_GPS();
RecordIO_Element recordIOElement = GetRecord_IO();
AVLRecord = new AVLRecord(recordHeader, recordGPS_element, recordIOElement);
avlRecords.add(AVLRecord);
}
Can someone give me an example how can I display data in MainActivity
If you are calling this method from MainActivity then you can use return to send back data to Activity class or can call a method to display with your data arrayList in Activity class.
To use return change your method return type from void to arraList of your data type.
public ArrayList<DataType> PrintRecordToResultTA() {
ArrayList<DataType> avlRecordCollectionArray = new ArrayList<DataType>;
// add data into avlRecordCollectionArray arraylist
return avlRecordCollectionArray;
}
And in Activity class change method calling,
ArrayList<DataType> avlRecordCollectionArray = ClassName.PrintRecordToResultTA();
Then you will have arraylist of data in Activity class. Display data.
Related
So I'm having 4 Activities. Let's call them Activity1, Activity2, Activity3 and Activity 4.
Now what i want to do is:
I enter data as an array in Activity2 and want to use it in Activity4. Now the solution that i found says, that i can just use intent.putExtra, and then startActivity(intent).
But my problem is: I don't want to start Activity 4 after Activity2. For Example: i want to be able to enter the data in Activity2 then go to Activity3 or 1 and afterwards go to activity4 and be able to use the data from Activity2. So is there another way i can pass the data to activity4 without being forced to start it? Im talking about something like for example "sessionStorage.set("",)" in Javascript/JQuery.
I hope my question isn't too confusing and thank you in advance.
If you don't want to start the Activity4. then you have 2 options to get the data in Activity4 without start to it. Saved the data into shared preferences or into the database, where you enter the data in Activity.
For saving the array data into shared preference, check this link.
Save ArrayList to SharedPreferences
Another option might be this. You can create a singleton class. You can access this class from any activities.
public class Globals {
private static Globals instance;
private String [] strings = null;
private Globals() {
}
public static synchronized Globals getInstance() {
if (instance == null) {
instance = new Globals();
}
return instance;
}
public void clear(){
strings = null;
}
public String[] getStrings() {
return strings;
}
public void setStrings(String[] strings) {
this.strings = strings;
}
}
Activity:
String [] strings = new String[]{"item1","item2"};
Globals.getInstance().setStrings(strings);
Other Activity:
String [] strings = Globals.getInstance().getStrings();
All data is delete when the application closed.
You can store the data entered into SharedPreferences in Activity2 and retrieve it in Activity4.
Alternatively, you can send the data to Activity3 or Activity1 and then afterwards send the data to Activity4.
I am creating an app that "Catches" all the notifications during a specified time period and then displays them all at one time. However, I am running into an issue with my NotificationListenerService Java class.
I currently am able to "Catch" the nofifications as they come through and stop them from displaying. I am also able to preserve the notification information in ArrayLists (as you can see in the onNotificationPosted method). However, when I try to use one of the ArrayList getters to pull the information into another class, the ArrayList is completly empty. Any thoughts as to why this is and why I can't pull this informaiton in another Java class?
NotificationListenerService Class
public class NotificationListenerServiceUsage extends NotificationListenerService {
private static final String TAG = "NotificationListenerSer";
ArrayList<Integer> idMap = new ArrayList<>();
ArrayList<Notification> notificationMap = new ArrayList<>();
#Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "onBind: ");
return super.onBind(intent);
}
#Override
public void onNotificationPosted(StatusBarNotification sbn){
Integer notificationInt = sbn.getId();
Notification notificationContent = sbn.getNotification();
idMap.add(notificationInt);
notificationMap.add(notificationContent);
cancelAllNotifications();
}
#Override
public void onNotificationRemoved(StatusBarNotification sbn){
}
public ArrayList<Integer> getIdMap() {
return idMap;
}
public ArrayList<Notification> getNotificationMap() {
return notificationMap;
}
}
Implementation Class
public class Batch_Notifications extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
public void getHeldNotifications(View view){
NotificationListenerServiceUsage noteListenerService = new NotificationListenerServiceUsage();
ArrayList<Integer> idMap = noteListenerService.getIdMap();
ArrayList<Notification> notificationMap = noteListenerService.getNotificationMap();
Log.d(TAG, "getHeldNotifications: " + idMap + notificationMap);
}
}
You can not persist data in runtime memory. Your NotificationListenerService will not always be running it will destroy and then again Instantiated and now all your properties will reinitialized.
The best way to this type of task is preserve data in a persistent Storage i.e Database. And when you try to send batch notification you get from the database. You can Use Sqlite database with Android-Room for easy implantation.
Have a look at https://developer.android.com/training/data-storage.
sorry if this is a convoluted question. Working on creating an app for a college course and I'm running into (what appears to be) a race condition in my OnCreate method.
TL;DR - sometimes my spinner populates and I can get an index from it. Sometimes it's not populated yet when trying to get a specific index. Details and code below.
The app is a "course scheduler" for a college student.
I'm creating an Activity that displays existing course information and allows you to edit it. In the OnCreate method for this Activity, I am filling a spinner for "Mentors" for the course and a spinner for which "Term" the course belongs in. This information is being pulled from a Room DB.
I have a seperate activity for a new course and for editing a course. For the "new course" activity, everything works fine. I getAllMentors() or getAllTerms() successfully and fill the spinner list.
For the "Edit Course" Activity, there's an extra step involved and it seems to be causing me some issues.
When editing a course, I pass the intent from the originating Activity with all the necessary EXTRAS. This is successful.
In OnCreate for EditCourseActivity, I do the following:
I get the mentorID from the EXTRA that's passed in from the originating Activity.
I access my MentorViewModel and call my getAllMentors() method which returns LiveData> of all mentors in the db.
because it returns LiveData, I use an observer and loop through the LiveData adding the Name of each mentor to a List and the
entire mentor to a List.
I populate my spinner with the information in List full of mentor names.
then I do a for loop, looping through List looking for one that has the same id as what I grabbed form the EXTRA in step 1.
If I find a match in that list, I call a getMentorName() method to snag their name as a string.
I have a methond getIndex(spinner, string) that will loop through the provided spinner, trying to find a match for the string that's
passed in (mentors name) that I grabbed that should match the ID of
the mentor assigned to the course. This method returns index location
of the matched string in the spinner.
I set the spinner selection to the index found.
I do basically the same process for term.
Me being a new developer, I'm not used to OnCreate running the code synchronously.
Because of this, it appears that I have a race condition somewhere between populating the List of mentor names that populates the spinner, and calling my getIndex() method.
Sometimes the spinner is populated and getIndex works properly and sets the correct mentor. Sometimes the spinner is empty and my getIndex() returns -1 (which it should do in a no-find situation) that populates the spinner with the first item in the list (once it's populated).
protected void onCreate(Bundle savedInstanceState) {
//////////////////////////Handling Mentor spinner menu/////////////////////////////////////////////////
int mentorId = courseData.getIntExtra(EXTRA_COURSE_MENTOR_ID, -1);
final ArrayAdapter<String> sp_CourseMentorAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, mentorNameList);
sp_CourseMentorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp_CourseMentor.setAdapter(sp_CourseMentorAdapter);
final MentorViewModel mentorViewModel = ViewModelProviders.of(this).get(MentorViewModel.class);
//Mentor test = mentorViewModel.getMentorById(mentorId);
mentorViewModel.getAllMentors().observe(this, new Observer<List<Mentor>>() {
#Override
public void onChanged(#Nullable List<Mentor> mentorList) {
if (mentorList != null) {
for (Mentor m : mentorList) {
mentorNameList.add(m.getMentor_name());
mentorListMentor.add(m);
}
}
sp_CourseMentorAdapter.notifyDataSetChanged();
}
});
for(Mentor m: mentorListMentor){
if (m.getMentor_id()==mentorId){
String test = m.getMentor_name();
int spinnerSelectionM2 = getIndexM(sp_CourseMentor, test);
sp_CourseMentor.setSelection(spinnerSelectionM2);
}
}
Is there a way to get them to run asynchronously? Somehow to get the observer doing my getAllMentors() to complete first and populate the spinner, THEN have the for loop run?
Or a better way to handle this?
Thanks in advance.
Room always runs the code on a separated thread, not the Main/UI thread. You can change that behavior with
allowMainThreadQueries()
after initializating your database. This will make the query run first, populate your list and then run your for-loop code. I do not recommend this approach, since it is a bad practice to make queries on the UI thread.
You have two options:
Change your foor loop to a function and call it after adding the values from the observer:
mentorViewModel.getAllMentors().observe(this, new Observer<List<Mentor>>() {
#Override
public void onChanged(#Nullable List<Mentor> mentorList) {
if (mentorList != null) {
for (Mentor m : mentorList) {
mentorNameList.add(m.getMentor_name());
mentorListMentor.add(m);
}
lookForMentor();
}
}
});
private void lookForMentor() {
for(Mentor m: mentorListMentor){
if (m.getMentor_id()==mentorId){
String test = m.getMentor_name();
int spinnerSelectionM2 = getIndexM(sp_CourseMentor, test);
sp_CourseMentor.setSelection(spinnerSelectionM2);
}
}
}
Put the for inside the observer, change the Room DAO to return a List and use LiveData on your own viewmodel:
MentorViewModel.java:
MentorViewModel extends ViewModel {
private MutableLiveData<List<Mentor>> _mentorsLiveData = new MutableLiveData<List<Mentor>>();
public LiveData<List<Mentor>> mentorsLiveData = (LiveData) _mentorsLiveData;
void getAllMentors(){
//room db query
_mentorsLiveData.postValue(mentorsList);
}
}
EditActivity.java:
mentorsViewModel.getAllMentors();
mentorViewModel.mentorsLiveData.observe(this, new Observer<List<Mentor>>() {
#Override
public void onChanged(#Nullable List<Mentor> mentorList) {
mentorsListMentor.addAll(mentorList);
sp_CourseMentorAdapter.notifyDataSetChanged();
for(Mentor m: mentorListMentor){
if (m.getMentor_id()==mentorId){
String test = m.getMentor_name();
int spinnerSelectionM2 = getIndexM(sp_CourseMentor, test);
sp_CourseMentor.setSelection(spinnerSelectionM2);
}
}
}
}
});
I want to set data into a variable of a class and want to get data of that variable in another class.
I have tested an example. But i am getting null value.
Example class Data:
public class Data {
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
In the MainActivity class I do the set:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Data data = new Data();
data.setText("HELLO");
System.out.println("GET from Main class: " + data.getText());
// Call the get class method
Test test = new Test();
test.execute();
}
Test class:
public class Test {
public void execute() {
Data data = new Data();
System.out.println(" GET from Test class: " + data.getText());
}
}
Output:
I/System.out: GET from Main class: HELLO
I/System.out: GET from Test class: null
How do I access to the Main class get?
Thanks.
You need to say that the data variable is a static variable, that means it's the same for all instances of Data. Once you've done that, you don't need an instance of Data.
public class Data {
private static String sText;
public static String getText() {
return sText;
}
public static void setText(String text) {
sText = text;
}
}
Then you call data statically. Data.getText() and Data.setText("hello")
edit:
But as Stultuske writes, you should brush up on the basics. My "solution" will fix your test but it's not a good one since making everything global will lead to a mess as soon as you start working on larger applications.
It is null because you create new object from Data!!! If you want get value from Data, you must pass Data object to your Test method.
The way it's done for now, execute() has no way to know the value of the object, because it's a new one for it. You've got two solutions for this :
you can put the text variable in static : this way, the value will be shared by all the instances. Any Data object will be able to change the value of text and you will be able to changed the variable without creating an instance.
you can also pass the Data object inside the parameters of the execute() function. This way, each Data object you will create will have its own text variable.
So I've debugged my program and have found that the part of my program is updating, whilst another isn't.
I have a method:
public void storeApplication(String name, String item){
Application app = new Application(name, item);
peopleAttending.add(app);
}
The debugger reports that an object is contained in the LinkedList (peopleAttending).
In another method:
public void populateListView() {
int noOfPeopleAttending = peopleAttending.size();
String noPeopleAttending = String.valueOf(noOfPeopleAttending);
Toast.makeText(GuestsAttending.this, noPeopleAttending, Toast.LENGTH_SHORT).show();
}
This method can be called after the previous one and states that there isn't an object within the LinkedList.
I've checked the object references just to make sure that they are pointing at the same reference and they are.
Any help would be greatly appreciated.
EDIT: Entire Class:
public class GuestsAttending extends Activity {
private LinkedList<Application> peopleAttending = new LinkedList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guests_attending);
populateListView();
}
public void storeApplication(String name, String item){
Application app = new Application(name, item);
peopleAttending.add(app);
}
public void populateListView() {
// GuestsAdapter adapter = new GuestsAdapter(this, peopleAttending);
// ListView listView = (ListView) findViewById(R.id.listView);
// listView.setAdapter(adapter);
peopleAttending.size();
int noOfPeopleAttending = peopleAttending.size();
String noPeopleAttending = String.valueOf(noOfPeopleAttending);
Toast.makeText(GuestsAttending.this, noPeopleAttending, Toast.LENGTH_SHORT).show();
}
Second Edit:
Java Booking Screen Method:
public void saveBookingInfo(View view) {
GuestsAttending sendApplication = new GuestsAttending();
EditText applicantNameText = (EditText) findViewById(R.id.applicantNameTextField);
EditText itemToBurnText = (EditText) findViewById(R.id.itemToBurnTextField);
String appName = applicantNameText.getText().toString();
String appItemToBurn = itemToBurnText.getText().toString();
if (appItemToBurn.isEmpty() || appName.isEmpty()) {
Toast.makeText(BookingScreen.this, "Please fill in all fields.", Toast.LENGTH_SHORT).show();
}
else {
sendApplication.storeApplication(appName, appItemToBurn);
}
}
GuestsAttending Java Class: -- See Above.
Useful hint: It's really popular to set type of List as a List<> interface from java.util package instead of LinkedList<> itself.
Anyway, i am pretty sure that storeApplication method is not automatically triggered before onCreate method ran by Activity framework. Maybe your debugger is stopoing on it in different order (because of using threads or smth), but you should to log some invoke. Try to find it out.
I've found out what the problem is:
When I submit the booking information, it runs all the necessary methods. However, when the "storeApplication()" method has finished executing, the ArrayList 'empties' all the objects out.
I only noticed this when I used breakpoint and tried running the method twice, on the second time I entered booking details, the ArrayList stated it was empty.
I'm going to see if I can try and store the ArrayList in a more secure place.