How to structure my Android/Sugar app? - android

Could you give me some advice and answers for my questions about my app?
In my application, I have two activities: first contain a ListView. Every item in listview is filling up with Database data. Second activity includes webView. I am going to use a Sugar ORM as a database. Part of the data will show on the first activity (name, image) and part in the second activity (link for YouTube).
For download and caching images from web I will use a Picasso framework.
1) What kind of adapter should I use for my listView?
2) I didn't find in Sugar's documentation examples of how I can separately query data from Database. Like, I need some part to show in first activity and another part of data in second activity, but in official documentation I have only found this example "Load Entity"
Book book = Book.findById(Book.class, 1);

Related

How to go about extracting data from an api website?

I have made an app where it opens up an online api(?) database. I used the ListView widget where each entry (name of something) clicks onto a new activity. I would like to know how to go about extracting data from the database and putting it in my app for example; if I were to get reviews about a shampoo showing on the page and also a picture of the shampoo etc.
Sorry if I make no sense.
It would have helped if you post some of your code.
According to what I understood is that you want to download data from server and show it into your list view.
What you can do is you can hit API in Asynctask. Put each item data like text, image URL etc into one bean class (setter getter) and add that bean into your list attached to your adapter.
Then you can call notifyDataSetChanged() on your adapter.

Session state of ListView

I am new to Android App development, working on an android app which populate a list of numbers, in a listview dynamically, depending on the choice of the user, but, the moment user closes the App, the items in the listview are lost. How can I maintain the state of the listview?
Examples with code would be highly appreciated.
When I open Activity A, it allows users to add friends, and this friend list is shown in the form of items of listview in the same Activity, however, when I move to Activity B, and then come back to Activity A, this friend list disappears. I need to make sure that this friend list should not be lost while moving between activities. Please help.
I think that for your purpose there are 3 main methods, i'll explain them from the easier to the most difficult (in my opinion).
Text File
A way to do this is to create two methods in a class:
one has to create the text file in the storage if it isn't created before and read that, the other has to append a String to a StringBuilder and write it on the previous text file.
For this method you need the uses-permission of reading and writing to storage.
This link can help you: http://developer.android.com/training/basics/data-storage/files.html
JSON (also XML)
With JSON file you can create a list of objects with your data that you can serialize when you update the list and deserialize when you want to read it. For this purpose you have to study JavaScript syntax or, at least, JSON one.
SQLite Database
Android SDK incorporate a class named SQLiteOpenHelper that you can extend to create a database inside your app.
This link can help you: http://developer.android.com/training/basics/data-storage/databases.html
There are also references saving methods but i think that aren't right for your purpose, they work betters to save something like preferences or single data like last login informations.
I went through your comment. I would personally suggest using SQLiteOpenHelper
You might need to understand the use of SQLite, hence the tutorial
Simple Flow. On your Activity 1 where person Add Friends save it to DB
Then refresh the List from the DB. So when you move to Activity 2 and come back again to Activity 1 your List will refresh from DB. Hence no loss of data as you want.
EDIT
As user wanted to know how to use the ListView with DB.
Following are my suggestion
Sai Geetha
Youtube

Dynamic flow in Android application

I am developing an eCommerce where I am fetching the Products and sub-products from database at run time.
Here the depth of the sub-products is not fix, means that I don't know at build time that a sub-product of clothes e.g. jeans, again have a sub-product or not.
I think to create Activities dynamically but as per Android Documentation we can not done it.
We have to specify Activities in Android Manifest.xml at build time.
As per your Question you want to display Products and sub-producs dynamically in your application. for that you should have to use Listview to display your UI part and use AsyncTask to Load data from your database/server.
ListView Tutorial :: http://www.vogella.com/tutorials/AndroidListView/article.html
AsyncTask Tutorial :: http://developer.android.com/reference/android/os/AsyncTask.html
First of all you need to clarify the data structure of the product defined in your application. There should be relationship such as foreign key or other constraints defined on your eCommence backend database.
So, there is no need to create activities at runtime. Instead, try to define a base activity to handle the common product operation and other special activities that extend the base activity to handle the differents between the product and the sub-products.
The logic may like this:
Load product data in common activity. then the app decide whether to load the sub-porduct via creating special activities according to the data flag/key defined in product data retrived from database, and so on.
I think expandable list view is best option for that.Because it clearly shows your main product and sub-products to the user.To create and understand expandable list view please see below link :
http://theopentutorials.com/tutorials/android/listview/android-expandable-list-view-example/

Loading a BIG SQLiteDatabase in a ListActivity

I'm working on an Android project I need to finish very fast.
One of the app's features is loading a SQLite database content and listing it in a ListView inside a ListActivity.
The database contains a few tables, among which 2 are very large.
Each item in the database has many columns, out of which I need to display at least 2 (Name, Price), although preferably is 3.
This might seem a pretty easy task, as all I need to do in this part of the app is read a database and list it. I did this without any problems, testing the app versus a small sample database.
In my FIRST version, I used a Cursor to get the query, then an ArrayAdapter as the list's adapter, and after the query I simply loop the cursor from start to end, and for each position I add the Cursor's content to the adapter.
The onItemClickListener queries the database again versus other parameters (basically I open categories) so it clears the adapter, then loops the Cursor and adds its content to the adapter all over again.
The app worked like a charm, but when I used a real-life, big database (>300MB) I suddenly got my app taking very long to display the contents, and sometimes even blocking.
So I did some research and started using a SimpleCursorAdapter that automatically links the contents of a Cursor to the ListView using the usual parameters (String[] from, int[] to etc., where I used android.R.layout.simple_list_item_2 and android.R.id.text1 and text2).
Problem is, is doesn't change much the time to load.
I've came across some suggested solutions on different web sites and tutorials, most of them using, in one way or another, the AsyncTask class. I tried implementing this manually myself but it's hard to keep track of multiple threads and I failed.
Tutorials keep telling how to do this with content providers, but I found nothing clear bout my specific situation: very big SQLite database -> read to ListView.
Now my head is filled in with notions like LoaderManager, LoaderAdapter etc, all mixed up and confused in my head.
Can anybody please provide me a complete, nice, clean solution to do this "simple" task?
Again: I want to read a BIG SQLiteDatabase and display it in a ListView. I want the app NOT to block.
I need a class that has a member function that takes as parameter a query and the ListActivity's context and takes itself care of displaying the result of the query in the view.
Please don't provide me abstract answers. I'm running out of time and I'm very confused right now and I need a clean complete solution.
You're my only hope.
If you query such large database it will take tym, you need to find a smart way,
Like limit you database query to get first 10 or 30 items and then maintain,once last item is reached query rest 30 items and bind them
Refer this tutorial, it will teach you how to add data dynamically in a list view
http://p-xr.com/android-tutorial-dynamicaly-load-more-items-to-the-listview-never-ending-list/
The above list has expired chk this
http://mobile.dzone.com/news/android-tutorial-dynamicaly
If you query large database it will take time to fetch data and show it on List View. So it is better to populate data at run time. You can use Lazy Adapter concept to load data . This link1 may be useful for You.
Thanks
you can also use :
public class TodosOverviewActivity extends ListActivity implements
LoaderManager.LoaderCallbacks<Cursor>
check this link for more details.

How to edit and refresh JSON data in an Android activity?

My Android application utilizes a backend API highly; basically, all of the information is queried from web server and rendered on Activities. So far, I've designed the application to be "read only".
For example, MainActivity first queries server for JSON data and shows the data in a listview. User can open a specific list item by clicking on it. The JSON data for an item is passed to the new activity (SubActivity) as a String representation of a JSONOBject (data.getObject(listItemPosition).toString()), so that no new query to the server need to be made.
However, now I'm facing a challenge as the second part of the application is under development: how to refresh data after it has been modified by user in an Activity. Lets say for example, MainActivity has a listing of images with image comment, and SubActivity shows single image, comment, and some additional information. Now I want to develop a function where user can edit the image comment in the SubActivity.
How should I deal with refreshing the data in MainActivity & SubActivity? Should I edit JSON objects directly (hard from SubActivity)? Should I somehow notify the MainActivity to reload the data? Any other best practices?
if you are using arraylists and customarrayadapters then yes you can notify the data object (and UI object) to refresh itself and update the view
you can edit JSON objects directly, its not like they are sacred, but JSONArrays can be kind of slow.
you can notify activities by doing startActivityforResult method and onActivityResult methods
do you need to update the server with this information? just make a new api call on the server that accepts this information

Categories

Resources