I'm creating a simple file explorer on Android Studio, and I wonder which one of the following implementations is the best:
My solution:
I have an activity called MainActivity that displays my list of files, and each time the user clicks on a directory, it creates a new MainActivity (and the onCreate method gets the new list of files, ...).
Correction:
In a correction from a tutorial, I found that when the user clicks on a directory, instead of starting a new activity, the code keeps the current one and changes everything (clear the list and fill it with new files, change the title...).
Is there a solution that is better than the other ? Is it more efficient to keep always the same activity ?
Thanks for any help.
Keep a single activity no question about it !
When a user clicks an item you build your data-source based on the newly selected path. Make sure to distinct between files and directories. Then simply call notifyDataSetChanged and thats it !
To query the file system there are two ways:
The easy - use Java File.listFiles()
The hard - run shell command Runtime.getRuntime().exec( "ls -la" ) and parse response.
There are many open source projects on github for the subject. Example:
Amaze File Manager
Related
I'm working on a complex Android Studio project, which creates many files and directories.
My question is: starting from a file or directory created, is there a command to search where the file is automatically created in the code?
E.g.: I have a textFiles/text.txt in emulator explorer... I want to find, starting from the file, where is the function createText() in the code that created the specifical file.
Press Ctrl + Shift + F and you will be able to search from whole project. In your case try to search createtext() or text.txt.
Make a complete search of mkdir() in whole solution, you will find the method where the directories are actually created. Then look for that methods usage for where its been called for creation of your specific file.(*Try debugging your code)
Hit the shift key 3 times and begin typing the file name.
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, in a feature file, to call certain previous lines of the same feature file?
Without using some steps of same feature file again you can use combined steps.
Ex :
Think you need to delete an order and go to home screen several times in a same feature file and for that think you need to call this 4 lines
Then I touch order cart delete order button
Then I should see delete item alert in oder cart and select YES option
Then I wait for 1 seconds
Then I go to home screen
So you need to call this 4 lines in 4 times in the same file. Instead of that you can define a new step in a ruby file like this
Then /^I delete order from ordercart$/ do
steps %{
Then I touch order cart delete order button
Then I should see delete item alert in oder cart and select YES option
Then I wait for 1 seconds
Then I go to home screen
}
end
And now you can directly delete an order using single line calling in feature file
Then I delete order from ordercart
Now you need only 4 lines instead of 16 lines and also it is easy to maintain the new changes to the code when you need in the future.
check out the 'step' command in cucumber.
In my experience though, it's a better practice to use conventional ruby method definitions and and call those from your step definitions. Steps calling other steps can get really messy.
I am creating a multiple choice quiz game where there is an option to continue the game from where the user has left. Example, if user stopped the application at Question Number 4, then by pressing the continue option will resume the quiz game from Question Number 4 itself. I am not able to understand how should I move ahead by coding way? What is the right way to continue a multiple choice quiz game? Any help would be appreciated.
As an example, you could use JDOM. Just include the .jar file in your projects build path and then you'll have access to the JDOM library methods.
An example of using the JDOM libraries:
// Create a new XML document in memory
Document doc = new Document();
Element root = new Element("child");
doc.setRootElement(root); // set ^ above element as the root element
root.addContent(new Element( "childname" )); // add a data element to the root
root.getChild("childname").setText("SOME INFO"); // give some data to the element <childname>
// Save the XML file
FileWriter writer = new FileWriter("FILENAME.xml");
XMLOutputter outputter = new XMLOutputter();
outputter.setFormat(Format.getPrettyFormat()); // sets correct tabbing/format in the file
outputter.output(doc, writer);
This would make something like:
<child>
<childname>SOME INFO</childname>
</child>
You can then access the information stored in the XML file by using some JDOM methods like:
root.getChild("SOME CHILD").getText();
Of course JDOM isn't the only option here. It's whatever seems easiest for you, not me.
Where are you getting your questions from? Try to build a sqlite db around your game. Get your questions from there. Then when the player goes out save state of the quiz in a db table, and when he comes back resume the game. Also you can store his previous performances, current performance in seperate tables. Seems like a good way to go about it.
Its a simple solution actually. A sql read will always result in the same sequence of read when you make a select call. So if you have 100 rows, and you select 10 rows of them and choose the first one, it will always be the same. Practically tested and it works. Now, you can create a "played" flag in your table and flag your quiz played. This way you will always get the same question in the same sequence as you reset.
If you want to randomize, I suggest you code that. There is no automatic way to do it.
Now for question, you should use shared preferences to save current question being played. This way you can just read that one row when you restart your application.
This is probably realy noobish question but, i am unable to solve it. I dont know what i am doing wrong or what should i do, i checked some tuts but i wasnt able to solve it. So i created new folder in res called menu and created new file there called xyz.xml. Now i want to call it in activity with following R.menu.item ... But when i just write R. it doesnt show menu as option. I can call any map which are default here like layout etc, but maps which i created i cant call them. What should i do to solve this?
If the menu file you create is called xyz.xml, then in your code you should use it as:
R.menu.xyz
Also, are you referring to the correct R? There's one R class used for the Android framework resources (android.R) and then there's one specific R class for your projects resources. To ensure you're using the right one you can use the fully qualified namespace, e.g. yourprojectsnamespace.R.
See http://developer.android.com/guide/topics/ui/menus.html for more information about Android menus.