Handling a large number of items in a listview - android

I am creating a directory of schools in android application and want to open their details on new page by clicking on it from list view. how should i handle it?

I would recommend to use a RecyclerView.
You can find a simple example here.

make Pojo of the student info and pass it to the next activity using parcelable object

Related

How to put data to spinner on another layout?

How can I to put data to spinner which is on another layout?
I am newby on android, and now I have problem with spinners.
I have data at first class with his layout. And I want to put that data to other spinner which is on another layout. What I need to do? Create new Class or What?
Please help! :)
Data is- Array
You should put your data where it is accessible from everywhere.
so please put your data in database using SQLite or ORMs that help you work with Native database like SugarORM, ORMLite and Realm.
another way is to apply singleton pattern to store your data in a different class.
You said you have an Array
Then make Array static
eg:- public static ArrayList<> new_array = new ArrayList<>;
now call this array any where in the activity
you will get data
there is many to manage array data.
- create arraylist and use that data anywhere in the activity.
- using database also you can user.
- using Read & writing arrays of Parcelable objects
- using global application class ....etc

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

Search implementation Questions on android

I am a newbie here and hope you can help me out to clear some of my understanding (or misunderstanding). I have been reading for days and trying out based on Android guide to implement search function but it really didn't work out well. Perhaps I've got the whole concept wrong so do help me out here if you can be kind.
I have an app which data is from remote xml and I want to implement search function. I have 1 main activity (under my SRC -> my package)that list all the data retrieved from the xml. My questions:
1) In the guide, it saws we must define a .Searchable Activity. I supposed this is to handle the search query and store the result? If this is a case, do I need to create a new activity or I can use my main activity as .Searchable Activity?
2) After the above item is done, I supposed I need to create an activity to receive the result and do something. I take it as I need to create "public class SearchableActivity extends ListActivity" in the activity I define in item 1? The problem is that I don't know what to do with this as the guide are incomplete.
3) Presumingly I did it successfully and upon search, there are some results return, how do I show (or filter to show the search result) in my main activity? I do not want to have another UI to show the result as I prefer it just get filter to show the matched records
4) Lastly, since my data is from remote xml, do I need to "search" based on remote xml or it will search based on cache info on the app?
I am sorry for being so ignorant but I am totally new here and hope you can help me out. So far I have no problem, getting the dialog shown on my action bar. The only problem is that the search didn't work at all :( upon search, there is just nothing happened.
Thanks very much in advance. Cheers!
For searching in a list you can use built-in functions of android, but if you want a good result you can use your own function. The simplest function is:
for(int i = 0 ; i < arraylisy.size() ; i++)
{
if(arraylist.get(i) == youobject)
return true;
}
return false;
but there are better functions like binary search that you can use.
Now, how to create search activity?
First: create XML file an add a listview in it.
Second: create java file and link xml file to it.
Now use above code to search an object on your list. If in every search you find several items then add them to new listview.
Now create an adapter and send your search list to that.
Finally set your adapter to listview.

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/

Parse JSON file and display an ImageButton and TextView for each entry

I have a JSON file that I want to parse. For each entry in the JSON file, I want to display an ImageButton (this will be an icon) and a TextView (a title). I'm just wondering what the best way of doing this is..
I DO NOT want to make a listview.. it is more of a grid view. I want 2-3 icons per row (so 2-3 columns) kind of like apple's bookshelf
I've started off with..
json = JSONfunctions.getJSONfromSDCard("/sdcard link to the file") //this gets the json file
JSONArray entries = json.getJSONArray("entries");
ArrayList<String> alTitle = new ArrayList<String>();
for(int i=0;i<entries.length();i++){
JSONObject e = entries.getJSONObject(i);
..//create arraylist to store entries?
//alTitle.add(e.getString("title"));
}
//create ImageButton and TextView?
You can use a listview inside this activity or a ListActivity. Then use a list adapter to create a layout for each list item.
There are samples supplied in the SDK for this to get you started. You will find these in the android-sdk folder wherever you have installed this on your PC.
There are also many tutorials on the internet so I cannont list them all, but here are a few to get you started. These are the top google searches.
Android Series: Custom ListView items and adapters
Android: Dealing with ListActivities, customized ListAdapters and custom-designed items
UPDATE
Sorry I missed the specification of using a GridView. The officaial GridView documentation includes a full sample The same idea applies using an adapter you can compare the different type of view in the Hello Views section of the official documentation
Here is a full example, you will need to add any additional controll into your adapter. Again, there are samples in the sdk itself and these are invaluable to Android developers.
I recommend looking into the GSON library for Android. It's supposedly a lot faster and easier to use than org.json in the stock SDK. I've also ran into bugs with the stock json library.
Basically it adds an interface similar to Java's serializables.
http://code.google.com/p/google-gson/
User Guide: https://sites.google.com/site/gson/gson-user-guide
You can parse the JSON, create an ArrayList then pass that ArrayList to an ArrayAdapter.
Use a custom implementation of ArrayAdapter as mentioned here
android, how to add ListView items from XML?
Create a layout XML with an icon and a TextView and deflate it in the getView method of your ArraAdapter implementation. Populate the values and you are done.

Categories

Resources