I have an android fragment that looks something like this:
Every time the user clicks add/remove a new row is added/removed. When they click okay, then I need to return a string for all the views above, for example "TextATextBTextCTextD".
What would be a good way to go about this? I thought about adding tags for each new view, then doing a for loop through each view. But because I have spinners and edit views, I wasn't sure how to get the view for each then get their text.
Any help would be appreciated. Thanks.
Well, your tag option isn't bad... everytime you change one of the view's values, you could set the tag of the view to the string value, then iterate over the views and just collect the tags.
Another way to do it, would be to create your own custom spinner / edittext & make them all implement an interface which has a method called String getDisplayText()
Then, whenever you create a view, place it in an array of that interface type, then iterate over all your objects and call getDisplayText() on all of them.
Hope this helps :)
Related
I have an Activity in my App that adds more EditText fields to my view when a button is clicked.
Now I want to create a new ID for every created EditText-Field. Then I want to use
editText.setId(createdID);
And then I want to add this editText to an ArrayList (thats why I need the ID!)
editTextList.add((EditText) findViewById(createdID));
Any ideas? Thanks!
You can set it with TextView.setId(int id).
Try to see this question for more details.
Android: View.setID(int id) programmatically - how to avoid ID conflicts?
Rather confusing idea. But if you really need - you can set tags to yours EditTexts
i need to get some json data from a server and then create elements to show that data. due the data is into an array of objects, i need to put this code into a loop.
example:
name - address
save button
name - address
save button
name - address
save button
i am thinking to create an array of linearlayouts with the needed widgets inside and repeat this process n times.
am i going right? it would be great if you can help me with a point of view.
kind regards and thanks for advance.
I would create a ViewGroup, then add each child view to that. The reason for this is it is more flexible (as the LinearLayout inherits from the ViewGroup). Then you can loop through each view later to get their values if need be
no, you should use a ListView and put your data in an ArrayAdapter. There are lots of examples and tutorials for that.
I try to put something like menu picker in ListView with only one Activity.
Only one XML file, one Activity one ListAdapter but few lists with items to fill it.
I am trying to do it like this:
on OnItemClickListener, i get item position, save it and open another list , according to our choose.
it's work, but only on first click. When i try choose 1st option 2 times then OnClick method jump from first list to last. When i try pick 1st option and then any other it's work's perfect
But it will be nice to have all options available for all the options on the list :)
any other way to make something like this? or to fix this? of course i can create xml file and activity for every single list, but when you have like 20 different combinations it's not good option :D
I'm thoroughly confused about the life cycle of list view. More specifically, what does the list adapter do exactly? Does it just provide data to the given view? And when/where does the getView() function gets called? And what purpose does this getView() function provide? From just looking at the code, it looks like getView() is "assigning" data to the view to be displayed. I'd like to be able to use list views without having to memorize, do this and then this in order for it to work. I'd much rather understand it so I can use it properly. Someone please help me understand all of this.
Also, if someone can explain to me.. what's the difference between BaseAdapter and ArrayAdapter? and any other kind of adapters that comes with Android.
What I have understood is your adapter constructor instantiated by activity and then on activity launch the getView() method is called. the {#param position, view, viewGroup}
position: it refers to the position of the view as given by adapter. Please Note it is different from the position in {OnItemClick(AdapterView adapter, View v, int position,long id)} here position is the list item position. The {position} in {getView()} changes after particular object in the list are displayed again for eg. when you scroll.
view: the view here is the view you want to be presented through getView(). It can be a particular XML layout for each row. So this states clearly that getView is called to plot every row. this view needs to be valid one or another layout (LinearLayout by default) will be selected to maintain uniqueness.
viewgroup: as you might know and as name says will be the container of your #param:view
any other point is appreciated.
getView() fills in the data into the item's view with the given index. The view which is given as a parameter may be a pre-inflated view. If it is not, you have to infalte it yourself.
An ArrayAdapter simply calls setText on the given view with the result of toString() of the object with the respective index from the array. If you override it, you can do more complex stuff, like assigning a picture or filling in more TextViews.
I recommend the following tutorial: http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
Hi list adaper provides view for listview.
when user scrolls listview at that time getview is called.
getview is used to populate your view with data hence the name adapter.
The Adapter does all the "rember to do this" for you. If you change a list view's backing data structure through the adapter's methods (e.g. "add()") it will fire all the datachanged and update events you'll need for the list view to show the new state of the data.
Basically what I want to do in my Android app is use TextView to display two different pieces of text at once. So in code, I want to be able to do something like this:
LinearLayout ll = new LinearLayout(this);
TextView text = new TextView(this);
text.setTextColor(0xFF000000);
text.setGravity(Gravity.CENTER_HORIZONTAL);
text.setTextSize(20f);
text.setText("Text1");
text.setTextSize(14f);
text.setColor(0xFF0000FF);
text.setText("\nText2");
ll.addView(text);
To clarify, I am trying to display a black "Text1" and a blue "Text2" at once using only a single TextView. Obviously this doesn't work out using the code above. I've considered using a second TextView but that seems like a waste of effort and memory to me. I'm sure the brilliant minds here can come up with the best solution to this.
Thank you very much in advance for your time and your assistance.
There are two options for you.
One is
Spannable
and other is
fromHtml (String source)
So that you can get your desired output.
I think with the current version of the code, you can see only the latest text (Text2).
If you want to have multiple look and feel for two texts, I would suggest use 2 separate TextViews. It would add more flexibility.
If you are not going to change this UI code later, then you can consider Html.toHtml() in setText() call.
It seems the problem is with:
LinearLayout.addView(text);
You are trying to add a view to a LinearLayout, but the layout doesn't exist (in the current activity). You need to add the TextView to a Layout defined in the .xml you are using. Suppose you have a LinearLayout with id "linearlayout01" in the xml file "activity1.xml", you would do something like:
setContentView(R.layout.activity1);
// Create and adjust TextView text
...
LinearLayout layout = (LinearLayout) findViewById(R.id.linearlayout01);
layout.addView(text);
Once a View is added to a ViewGroup of which LinearLayout is a descendant you do not need to readd it to update its display. If you preform any changes on a view that requires it to change its display it will handle all the proper notifications about required redraws or relayouts by calling View#invalidate and View#requestLayout where appropriate.
In addition, because all UI changes are handled on the same thread you do not need to worry about calling multiple methods that will update the UI. This is because of two reasons, first, the execution of the redraws will not occur until your code is finished, second, android has optimizations built in that combines multiple invalidate calls into one.
So, the only thing you need to worry about is getting a proper reference to your TextView instance and then you can call all the methods on it that you need to make it display what you wish.
Since you are creating your Views manually and not from xml you need to add your root ViewGroup to the Activity by calling Activity#setContentView.
Edit:
Then you're going to need to learn about SpannableString and SpannableStringBuilder. There is some very brief documentation here: Selecting, Highlighting, or Styling Portions of Text
when do you plan to update the textview ? If it is on click of a button then get a reference to the textview and in the onClickListener() update the text, color, etc whatever you want to do.
After seeing your other comments, I think SpannableString is what you are looking for