I want to coding a app with SQLite.There are two java file, one is Main.java, the other one is MyDB.java.
All method about DB ,I want to put on MyDB.java
I don't know how to use method in MyDB.java.
Any suggestion?
I think you have to read this tutorial.
http://www.roseindia.net/java/javascript-array/call-method-another-class.shtml
or this
Android: How to use classes in a different file
Read about Java classes and objects.
Basically you could set the MyDB as an attribute for the Main class (if it it's an object class also), and initialize it within Main. Then you can use the MyDb-object's methods.
Or just create an object out of MyDb in your Main-function (MyDB db = new MyDB(...)) and start calling its methods (e.g. db.getConnection())
Related
so I'll try to explain my problem this time without pasting you 400 lines of code.
I have a custom view that im using in my main activity (com.company.app.common.ui.view1), in my main activity it's imported properly as follow
import com.company.app.common.ui.view1
Inside view1 i have a function defined in the constructor
fun updateItems(items: List<Item>){}
Item is an object I have modeled properly and the function has no errors.
But when I try to call it in my main activity using
view1.updateItems(items) I get an unresolved reference on updateItems.
the function is supposedly public by default if im not wrong, can anyone help? Thanks
It looks like you are trying to call the method on class rather than on instance (one way to get an instance is to create it via constructor myView1 = view1(...) and then you can call the method on it myView1.updateItems(items)
Two more points:
No need to post all code, just post snippets (only the code relevant to the issue), replace the non-relevant code with ... and keep the parenthesis
In my answer I assumed (based on what you wrote) that view1 is supposed to be a class/object, it's good practice to capitalize it's name so it does not get confused with instances
I need to store a few values that are going to be use in several activities. I know could easily create a Constants class , a Interface or extend the Application class and put those constants there.
But not wanting to reinvent the wheel, I want to ask you if Android have something like appSettings in Asp.net or the Application Descriptor that we had in the old Java Me.
Thanks in advance.
Since you are using constants, there are two solutions:
Create an interface or class with public static final fields.
Create a XML resource file. See Resource Types for details about the resource types.
Personally I prefer 1 for constants that are used in my Java code because 2 requires a Context object and calls to getResources(). This just makes for more code than is really necessary.
Note that I don't give SharedPreference as a possible solution. This is because SharedPreferences should be used to store calculated or user data rather than constant values.
You could use SharedPreferences
Complete Docs
I am working on my second Android Application, first being, hello world. The application code is quite crazy looking because I love to test new libraries and ideas in it. I have been working on this application for well over 3 months and one of my activities is getting way to large and difficult to work with. I find myself getting lost in the code and it is taking longer to do simple things. There might be simple solutions to solving this issue. I really want to split my activity into two and reference each other if possible. Is there are any suggestions to simplifying and organizing code that would be greatly helpful. Even example will help me very much.
Part of my activity is adding a ton of data into a database and the other part is a long equation with multiple values. Another part is implementing the HoloGraphLibrary (Which I love). It is also implementing a listView with custom adapter. It also has a custom dialog............ I can go on and on. I hope you get my point.
EDIT
Going to work with this.
HoloGraphHelper holoGraph = new HoloGraphHelper();
holoGraph.initialize();
Try creating classes for each responsibility.
A Database Helper that has functions to insert data too:
DatabaseHelper database = new DatabaseHelper();
database .insertData(whatever);
A HoloGraphHelper that initializes the HoloGraph
HoloGraphHelper holoGraph = new HoloGraphHelper();
holoGraph.initialize();
And so on.
Break into multiple files. First classes defined in the Activity like the adapter. Change anonymous classes to classes defined in their own file. Look for ways to break out other related code into a class.
Right click on src folder of your Project and select new - class to create a new class. You can use a class for storing methods but you won't be able to display anything on screen.
To display contents to user, you can create a new Activity bu pressing Ctrl + N and selecting Android - Android Activity.
The best way is modularise your code.
I.e split your code into various related modules, for example a separate class for each part that your testing. So you could have a database entry class, a class for Gui testing, i.e. for your custom dialog. That class does all the work for that test, into various functions, I always try to keep functions as small as possible as they are easy to read.
As an example for your database entry, you could have a function which checks the database if the record already exists and then insert it. But a better way would be your insert function only performs the insert code and instead within this function it calls CheckIfDatAlreadyExists function which can return a bool so you know whether you should go ahead and insert the record. This would keep the code tidy and clean to manage.
Then from your main activity all would need to do is instantiate the relative class and call the relevant method.
Is it possible to change at runtime the behaviour of a method from a class already loaded using Dexmaker, by proxing or generating code?
UPDATE
Just a remark: I want to add a method/modify existing one from my own application, not from the android framework.
No. It is not possible.
You could create a new class that extends the original, or possibly even make a copy of the class, with a new name and a tweaked implementation. But you can't replace an existing class.
Your best bet is probably to extract out the code that you might want to modify into a separate class, and then pass in an instance of that class to whatever uses it. And then, if you need to create a new implementation, you can subclass it and pass in the subclass instead.
I've recently been making a very data driven application in which I use a lot of arrays. I've tried searching for a solution, but the problem is quite difficult to word concisely so I haven't been able to find an answer. If there's an obvious one I apologize beforehand.
Currently I load a set of 30 arrays from multiple pre-made databases during an initial class, and I use intents to move this set of arrays back and forth between my classes. The problem is, this results in very long extra sequences of code in every single one of my classes. To give an example, after the initial screen I have to enter the code in every class:
Intent intent = new Intent (getApplicationContext(), NextScreen.class);
intent.putExtra("array1", Array1);
// ... 30 more arrays
and then
Bunble b = getIntent().getExtras();
Array1 = b.getStringArray("array1");
// ... 30 more arrays
I was hoping maybe there would be a way to store all the arrays in some resource or class to just reference later.
I suggest your create a Class that holds all your information , then make this class Parcelable so it can move throught activities : Parcelable example .
Instead of using arrays, create a class with static vector or arraylist.
create setter and getter methods which will update your arrays.
and directly call these methods from multiple classes,you don't need to pass values. just store them in one class, and use the same class, to read write from multiple places
Try using a singleton class. This will be like a "Model" class in MVC patter.
However, exercise caution when using this, as per this discussion:
Singletons vs. Application Context in Android?