How to make a Android contact alike listView? - android

Hi, I have to make a listview like this image.
These letter A,B are auto scrolled by the listview.
The Letter A is appear until there is no Item start with A in the list view.
I dont have any problem with the list view in the right. My difficulty is showing letter abc along with the listview.
Can anyone help me about this?
Thanks.

I'm assuming that you already know how to make a list view to display data and/or images as the image above illustrates, and that your primary difficulty is to show the "A", "B", "C", etc.
If that is the case, then a good approach would be to specify a list of models (data), each containing a field of "name", "imageUrl" & an optional "firstLetter" which can be null.
When you sort these models by the "name" field, you go over the list once, and on each iteration you check if the 1st letter of the "name" field is different than the previous one (always true for the 1st element). If this is the case, the "firstLetter" field is set to the upper-case first letter of the "name" field. Otherwise, it will be null.
Then in your adapter, if the "firstLetter" field is not null, then make the TextView which displays this letter visible (otherwise, make it GONE), and assign the text to the value of "firstLetter".
EDIT:
The above was written before you asked for the "A", "B", etc to be "sticky" and remain until there are no more items beginning with that letter.
To implement the "sticky" behaviour, similar to the WhatsApp app which makes the dates of the texts sticky, you can create an additional view which floats over the list (you'll need to make sure that the paddings, styles and fonts match so that everything looks good).
Then, in your adapter, you'll need to check the following:
Am I already displaying this "hovering" letter for the letter that my cell wants to show? if that's the case, don't show it. This will prevent showing two "A"'s when the "A" is partially outside the screen.
Is the position of my cell which is showing the "A" or "B" slightly outside of the screen? If so, make sure to show the "hovering" letter and give it the correct text.

I think you have to use 2 listviews on a relativeLayout. First with the Capitals of the names and second, the list you want to show.
For make your listview you must create your itemList.xml, with name and photo like you want for create your listview.xml. You will need a "people.java" class with atributes, for example, name and photo. Also, you need one adapter for create the relation between your list.xml and your mainclass.java.
This page is in spanish but the code could be usable for you:
http://www.oneoctopus.es/desarrollo-android/listviews-personalizadas-en-android/

When you are getting the data in adapter class then check for each object and its first word extract their first word in a char and compare that first word with other objects if they are same ignore and if different then save the previous char to temp char and assign the new one to previous char and so on.

For the sticky letters index, you can do the following:
On the leftmost side of your RecyclerView, create a TextView that will hold the letter index;
On the top of the Recycler view (in the layout that wrappes it) place a TextView in order to cover the one you created in step 1, this will be the sticky one;
Add a OnScrollListener in your RecyclerView. On method onScrolled (), set the TextView created in step 2 for the reference text taken from firstVisibleRow. Until here you shall have a stiky index, without the effects of transition;
To add the fade in/out transition effect, develop a logic that checks if the item previous of the currentFirstVisibleItem is the last of the previous letter list, or if the secondVisibleItem is the first one of the new letter. Based on these information make the sticky index visible/invisible and the row index the opposed, adding in this last the alpha effect.
I've answered about the sticky-index in this question and also provided a library that does this behaviour. Hope someone still needs it: https://stackoverflow.com/a/30679418/2068693

Related

Best way to make a layout with many buttons on Android

First I have build a simple android application which had only five optios to select. For this purpose I used five Buttons on main Activity. Now I have more than twenty buttons in a ScrollView to select. What is the best way to represent this kind of application (using buttons in a ScrollView? using TabHost? or with some other widget?)
The app look like this now:
Grid View or List View or Recycler View
the Adapter automatically will add buttons with the names you want, something that I did for my upcoming app.
I made a java class called data which has 'data` for my app.
it has an array of images for my GridView.
SO:
Make a class called data
Add a public final static String[] myArray array of your names, or data
Now, whenever you want to access them, use data.myArray
If you want to access one item ,use data.myArray[itemIndex]
Don't forget, indexes are zero based, not 1
Put your button inside a viewHolder class
find the id of the button in the getView if convertView is null & set the holder as a tag
NOTE : after finding the ID of the button, just leave it don't do anything or edit the text, continue reading please.
Use that array with your custom adapter
as
gridView.setAdapter(new myCustomAdapter(parameter1, parameter2,data.myArray);
use this , I just made it yesterday, added array of buttons feature now. You can just learn it or use it or commit changes.
NOTE :
You can make an array of listeners just like any primitive data type, View.OnClickListener[] and name it, initialize it.
Use grid view. it will be easy to show multiple buttons on screen using grid view.

Array Adapter for an AdapterView that only shows data one way, (Android)

I get data from my server returned as an array of objects. Each object is itself an array of strings that describe the object.
For example let's use cars as the object. In this case, the array of strings are descriptors like 'year built', 'horsepower', 'automatic or manual', 'color', etc etc.
What I'd like to do is display only 1 car at a time for the user. The user can choose whether he likes or dislikes the car. Either way, a choice is final, and the next car will show up. Also, the user should not be able to go back to the previous car (not with a swipe or a clicking of aback button). In other words, he can never see his choice on the previous car again.
If I make a call to my server on every single 'like/dislike', this will be a very slow app. If I inflate a bunch of data into many views that are out of sight, it will also be a very slow app.
Most Array Adapter examples I see online illustrate how to show data bi-directionally. They are viewpagers or listviews that you can swipe left (or up) to view data that has been previously already viewed. This is not what I'm looking for.
Is there a proper way to implement what I'm trying to do? Any help will be appreciated as I'm just hoping to get some direction and can implement on my own. So far I'm thinking about possibly a viewpager that deletes items as you view them, but I have a feeling this will be really hard to manage the position of the views...
Use an array to hold the data you get from the server. You can request that the server send you 10 or 25 or 50 at a time.
Since you only want to show the user 1 at a time, and he can't scroll or swipe through the list, you only need a single set of views (enough to show all attributes of a single entry). You don't need an array adpater for this. Whenever you step from one entry to the next you just need to adjust the index into your array and then copy all the attributes of the new item into the individual views with setText() or setImageBitmap() or whatever.

Which is the better method in Android for creating a dynamic list?

If you are creating a very dynamic list, say, where every row can have a different set of input types plus optional buttons, and the list length is based on another dynamic value, is it better to do this in a list adapter or creating a custom view in a scroll window?
After struggling with list adapters for quite a while now something finally occurred to me- this seems dumb. It seems like I am going through a lot of work keeping track of what spinner is set to what value, which row was clicked and so forth.
For example, say you are showing something like a contacts screen with various details that can be entered about a contact. Some rows will have text inputs (name, address etc), some will have spinners (ie. state, group), some will have checkboxes (like 'favorite' or something). Also, there is an 'add' button that allows you to add another field to edit. Is it worth making this in a list adapter or is it better to populate a custom view, and if the "add" button is clicked, we re-create the custom view, adding a view of the type they want to add?
I hope this is clear.
ListViews (and List Adapters) are meant for data that is to be displayed in mainly similar views. For your example, it is much easier and more natural to have a predefined layout file with the screen and use view visibility so select which views are to be shown. If you need to add views to the screen you can do this dynamically by using findViewById on the layout and then using it's addView method.
Let me know if you need more clarification or sample code...

Read, display and update Android table values?

This is more of a question about getting my head around the process I need to follow really.
I short:
I need to read the exisiting table (a rugby squad) and display it on screen.
The user has the option of tapping any entry and updating it - and repeating as necessary.
The user can then tap "Accept" or "Cancel"
If "Accept" is tapped I obviously want to update the table with the new values.
The squad can be from 10 to 22 players, so I need the display to be fairly dynamic and ideally I need a shirt number (static), a name (text, updateable) and a "starting" checkbox (updateable)
I've kind of got my head around each on the indiviual components, but when I try and tie them together it all goes pear-shaped!
Can anyone point me in the right direction, please?
It sounds to me like a simple datbabase backing a ListView would be the way to go. That way you can offer different options for sorting the list, etc.
EDIT
In your main xml have the listview and two buttons at the bottom (accept and cancel).
Create a Listview rowlayout.xml that contains a textview, an edittext and a checkbox.
Populate them like normal (you can use setText() on an EditText just like a TextView). Put the shirt number in the textview and the player name in the edittext and the starting state to set the checkbox.
Then you simply wire up the accept button to cycle through the list and commit any changes you made and the cancel button to reset them all to the last saved values.

Android store listview views into ArrayList

I have the problem of the ListView re-rendering and changing the values within my list view views. Each cell has interactive elements with numerical values that are set, unfortunately once off screen these numbers are re-rendered seemingly at random!
I've since discovered that Listview needs to have an extended view class that allows the values at that position to be stored.
I need to pass several values, so far all the examples I have seen only show one method with one values per cell (referring to http://commonsware.com/Android/excerpt.pdf), I am seeking assistance on how to store the values for my application.
I have a "total" count , and a separate count that I can increase and decrease the value of, via buttons within the listview cell. I need to store these individual totals and values within that position in the View. I supposed I would have a method/constructor that simply includes those values, but in this syntax is seems the super. will not allow this override.
Insight Appreciated. If this wasn't clear without code, sure just let me know. But if you are already familiar with this problem and the most generic way to tackle it, that would be great too.
Listview recycles its views, so if you don't store the values you are displaying explicitly using a custom adapter fit for your purposes, that is probably were it goes wrong. Whenever you scroll a view in the listview out of the screen, it throws away all the values in there, and pastes it on top with new data.
So what you probably need is a custom adapter storing the numbers you want to display; here is a good place to start off from in understanding it better: http://developer.android.com/videos/index.html#v=wDBM6wVEO70

Categories

Resources