Android most efficient Table Layout - android

So I have a few arrays of data I would like to display in an activity without having like 15 text views with unique ids. Is there a code efficient way to make a Table layout or something like it where I could feed in data and it would automatically place it in there respective text views? Thanks!

I think you can achieve that by using RecyclerView (with a GridLayoutManager). Have a look at this answer.
If there are only TextViews and you don't want a specific layout you can use SimpleAdapter, if you want to modify the layout you have to extend RecycleView.Adapter (there is an example in the answer above).
You can add/remove items into/from a List and use DiffUtil that
can calculate the difference between two lists and output a list of update operations that converts the first list into the second one.
There are a lot of tutorials about using this class. Have a look here or here.
Or you can use the notifyItemChanged() method:
If the list needs an update, call a notification method on the
RecyclerView.Adapter object, such as notifyItemChanged(). The layout
manager then rebinds any affected view holders, allowing their data to
be updated.
LE: There are some libraries available. Here is a list:
https://github.com/evrencoskun/TableView
https://github.com/HYY-yu/TableRecyclerView
https://github.com/Cleveroad/AdaptiveTableLayout
https://github.com/celerysoft/TableFixHeaders

Related

How to implement Android Multi-item listview like iOS UITableView

Now I am implementing chat screen in an Android App.
the use of the adapter pattern of ListView seems to be the best one cell.
You know you need a cell of a different type of user messages, photos, videos, dates, announcements, etc in chat screen.
Conventional methods that I know of is two things.
In getView() method, create each time a new type of cell.
First of all, put all the item and ,in getView() method, adjust the UI layout by VISIBLE/GONE options.
The second method is better i thought, but this method is still a waste of memory and complexity to implement.
For UITableView of iOS generate multiple queue by the number of type of cell. It is efficiently in Multi-Item ListView.
there any easy way to implement as iOS?
Use RecyclerView instead of ListView.
Create xml file for each item view. Create ViewHolder for each file xml.
Inside Adapter of RecyclerView override getItemViewType() return view type correspond with it's position. Override onCreateViewHodlerwhich return ViewHolder correspond with view type. Finally, override onBindViewHolder bind your data to your layout.
Consider using this library JSQMessagesViewController its very flexible and scalable.

Android display multiple lists values

I am doing android application. In That I want to display a List of podcast urls like shown in the Image. In this I also share this url into FB, twitter and etc and also the user clicks the arrow symbol I want to forward to that podcast url.
I am having those values in separate arraylist (i.e. "4353,3424" as a arraylist and "567567, 234234" as a another arraylist likewise). So how can I display these values like shown in the attached image. Can anyone help me how to proceed to display like this?
I'd recommend you had a look at some of the tutorials on how to implement your own custom ListView.
An example can be found here: http://www.thepcwizard.in/2012/09/android-creating-custom-listview-for.html
Also I'd recommend you create a custom class for holding the different informations in every row of the ListView. When doing it like this you can have one single List<MyCustomObject> holding all informations and then when a row is clicked, you simple get the item from the List<MyCustomObject> and get the specific property of the custom object and act accordingly.
EDIT: Small example of how to add onClickListener to a sub-view of the row:
Inside the getView method of the custom Adapter you can use setOnClickListener to the views, you'd like to respond to clicks. For instance:
myImageView.setOnClickListener(this);
Then let your custom Adapter implement the interface OnClickListener and act accordingly to the clicks.
Another way would also be to add a Share Intent to the different images, like in this example:
http://sudarmuthu.com/blog/sharing-content-in-android-using-action_send-intent
You would need to create a custom ArrayAdapter to populate a ListView from this objects the way you want.
The advantage of this technic is that you gain a Views recycle mechanism that will recycle the Views inside you ListView in order to spend less memory.
In Short you would have to:
1. Create an object that represents your data for a single row.
2. Create an ArrayList of those objects.
3. Create a layout that contains a ListView or add a ListView to you main layout using code.
4. Create a layout of a single row.
5. Create a ViewHolder that will represent the visual aspect of you data row from the stand point of Views.
6. Create a custom ArrayAdapter that will populate the rows according to you needs.
7. Finally assign this ArrayAdapter to your ListView in onCreate.
You can get an Idea of how to implement this by reading this blog post I wrote:
Create a Custom ArrayAdapter

Loading sub-records for each item in listview

I'm making an app in which the user can log simple timesheet-like transactions into a list. I have done this with a listview which is connected to a sqlite query with a custom CursorAdapter descendent. So far it is working well.
What I am trying to do next is let the user add any number of "tags" to an individual transcation (think labels against emails in gmail). I have a many-many-relationship table to link tags to transactions, but i'm unsure how to modify my existing listview and cursor arrangement so that each individual record can load and display its tags.
One thing that comes to mind is to load all the data into a two-tiered object structure, then use that as the datasource for the listview. I don't like this idea though because it will certainly cause a delay in loading, whereas using a CursorAdapter seems to be better for performance.
The way I was intending to display the tags within each listview item was just as plain text, linearly along the bottom of the row with different colours using Html.fromHtml(). I already have this part working so i was hoping to stick with the existing listview (if possible). My problem is purely about finding the best and most efficient way of getting the tags out of the DB and making them available to each row in the listview.
I'm very new to android - is there a clever way to do this or is it going to be a manual exercise?
There's no shortcut for this. You're going to have to create a View by subclassing LinearLayout, RelativeLayout or whatever you want. In your View class expose a public method like 'setTag(tag)' which takes an argument as to what tag to set the corresponding data to. In your View you'll have to have some field which maintains a reference to that View. In other words, somewhere before you return your view in getView in your Adapter, you can bind the data to the View and then alter its state from within the View.

How would I use a different row layout in custom CursorAdapter based on Cursor data?

Background: I'm trying to implement a messenging system in my app, and I'm writing a custom CursorAdapter to display the messages in a ListView in the chat window. I want to use a different row layout for incoming and outgoing messages (information that is saved in the SQLite row in the cursor). Each row has the same elements in it with the same IDs, but they are arranged differently.
The Problem: Currently, I have overridden newView() and bindView(). When the ListView is first populated, it creates all of the Views perfectly, checking each row to see if it's either incoming or outgoing, and inflating the proper XML file. However, when I scroll or a new message is added to the window, the adapter recycles Views for the wrong rows. I would override getView(), but it is not passed the Cursor as a parameter, so I have no way of knowing whether the row should be incoming or outgoing.
I'm not looking for code, but rather, some suggestion for an elegant implementation. Thanks in advance!
Here are two possible solutions:
(1) Use a single layout for all items, which you can adjust when binding to show as desired. The most straight-forward way would just to have the root view be a FrameLayout which contains N children for each of the different states, and you make one of them visible and all others gone when binding. Of course you want to take care to not let this cause your items to explode in the number of views they contain.
(2) Implement Adapter.getItemViewType() http://developer.android.com/reference/android/widget/Adapter.html#getItemViewType(int) to tell the list view about the different types of items you have so it will recycle the correct one.
Just a few thoughts, I personnaly find the whole ListView and CursorAdapter combination to be a little... err... is bloated the right word? Would it just be simpler to have a ScrollView/ LinearLayout combination that will just add the appropriate TextView as requested?
But as for your solution, since the user seems to be unable to change the order of the messages as they are added, you could add a ArrayList<String> field to your custom CursorAdapter that will keep track of whether the messages are incoming or outgoing. Something like:
private ArrayList<String> cursorMonitor; //"incoming" and "outgoing" as your options.
...and then inside wherever the ListView gets populated just use
cursorMonitor.add(my_cursor.getString("outgoing_or_incoming"));
And then in the getView() you can override it and use the cursorMonitor to determine which layout you need to inflate.

Android Complex View: manually create or using layout xml file?

I need to create an activity(view) that shows several lists, for example:
(text)Fruits:
(item)apple
(item)orange
(item)pear
(text)Veges:
(item)Cabbage
(item)Carrot
all data are read from a database, so I cannot determine them and create layout file(xml) for them, what's is the best way to create this? Also, list items are clickable which directs users to another screen.
Thanks!
ListViews are filled with data using Adapters. So, you can use a single layout with a ListView and change adapters depending on what you want to display.
Have a look at Adapter interface.
And if you want to have several ListViews, then in your case I'd use a single ExpandableListView widget with differet groups like Fruits, Veges and so on.
If you have created a database using sqlite already, U can use cursor adapter to fetch the data from your database. Its really simple. try to find out how to fetch data from database and how to insert values into listview using cursor adapter.

Categories

Resources