I have a GridView that displays several dozen rows worth of a custom layout, each of which consists of an EditText and a TextView object.
If I understand correctly, I should extend the BaseAdapter class to accomplish that. If so, how can I get access to a specific EditText object?
Also, will recycling of views cause me to lose the text that the user has entered if one of the EditText views is no longer visible?
Is there a simpler means to accomplish what I am describing here (perhaps something other than GridView) ?
When you extend BaseAdapter one of the methods you have to override is getView(...). In this method, you create the view that needs to be shown. If you want to persist text that is entered in the EditText, what you need to do is set a a TextWatcher on the EditText, passing in the position of the EditText (parameter in getView(...)), and whenever the text is changed, save the text in an array of sorts. Then, whenever that position comes back through the getView(...) method, grab the text from the array and populate the EditText.
You should use an underlying data structure ( say a List ) which should hold the data for each element of the Grid.
TextView and EditText should be populated from this List.
Related
I have a custom listview with EditText, my problem is when one edittext value is alter then i need to update another edittext value in inside listview.
For Example:
if edittext position1 value is '5',and edittext position2 value is '2' means i need to update particular possition value is '7' (5+2=7).
how this possible without refresh the view every time when another edittext value changed?
thanks in advance...
I'm going to give a generalised solution instead of the positions for 0/1 etc..
So for performing such operations do the following steps:
1.Change from ListView To RecyclerView.
2.RecyclerView Forces developer to follow view holder pattern (I assume you know what view holder pattern is if not you can google it)
So in your recyclerview whenever user clicked on a row you will have the clicked position.
use the following method to get the view for particular position
myRecyclerView.findViewHolderForAdapterPosition(pos);
here pos is the recyclerview row where you want to edit the edittext value.
Here is the trickiest part.
As recyclerview uses viewholder pattern, the above method may return null if is not in the viewport(i:e; If it is not seen in the screen)
if myRecyclerView.findViewHolderForAdapterPosition(pos); this method returns a view well and good (view is currently in pool and is accessible)
If not it returns null, so what you need to do is remember the event and when user scrolls you will get the currently shown position if the currently shown position matches the view to be updated then update your edittext.
Actually once i faced the same scenario and this is how i solved it.
Comment below if you have any doubts.
I case if you have only two rows in a list . myRecyclerView.findViewHolderForAdapterPosition(pos); this method will never return false unless your each row is of screen size.
you can use edittext text changed listener for such kind of implementations like updating text as of when editing... for how to implement check this link android on Text Change Listener
I followed almost all the threads about this and didn't find an answer (if there is one..).
I have app which is table of values and to each value i have spinner for the user to choose.
I implemented it by listview (can also with gridview with 1 column), with custom layout (linearLayout with 2 textViews for the ID and VALUE_TEXT and Spinner).
the Spinner is with custom layout also - value Id in the spinner and the text.
the both IDs (ListView value ID and Spinner value ID is hidden from the user and it is for my use).
i have few questions about my implementation :
1) is that the best way to implement rows in UI (ListView and Spinners) when you need to save its ID? i am getting the data from Json, which gets me the ListView items (ID and TEXT), and for each spinner i am getting (after POSTing the value ID to the server) the values for the spinner and their ID. i am communicating with the server only with the IDs, so i have to save them somewhere..
2) is there a way to get the ListView row (for getting its ID) from the Spinner OnItemSelected method? i have only the parent parameter, which is AdapterView.
3) Now i am extending BaseAdapter (one for the ListView and one for the Spinner) in order to be able to populate the data in the UI. i am using its getView method to inflate the custom layout and putting the data to it? isn't there a simpler way with ArrayAdapter to perform such action?
Thank you very much.
You can use ListActivity since it gives you a lot of shortcut methods to make things easier and keep your code more readable.
For example, you get the onListItemClick() method which is called whenever you click a item which saves you from creating a separate listener.
If you want to change the layout of a ListActivity you still can with setContentView() method from Activity. As long as there is a ListView called #android:id/list somewhere in your View the ListActivity will still work.
I am developing an application for Android, and for that I am trying to make a ListView act in such a way, that when a user presses an empty entry, he can start typing text directly into that empty entry, and that when the user touches any other part of the screen, it is saved. Is there a way to do this? I was thinking of using onClick somehow, but I have no concrete approach.
Here is one basic, general approach:
For each row of the ListView, create a layout that has a visible TextView and a EditText with the visibility set to gone.
Use an onClickListener for each row to swap the visibilities of the TextView and EditText (respectively, gone and visible) when the row is selected.
Track the active row for clicks to another row or background.
When the active row changes, set the value of the EditText to the TextView and return them to their original visible states.
Other approach that could be taken is using the View.OnFocusChangeListener and the TextWatcher you can get more detail about them here http://developer.android.com/reference/packages.html
I have a ListView that can be in one of two states - viewing and editing.
In viewing state, the ListView should use one view to render rows - one with non-editable fields.
In editing state, it should use another view - one with editable fields.
When the ListView switches between states, all its rows should use the appropriate view for that state.
If the user has scrolled down the list, he/she should not lose their position when the ListView transitons between viewing and editing.
One of the solutions I attempted involved 2 adapters. But that was no good because it made the ListView pop back up to the first row whenever either adapter got set on the ListView.
Thanks much.
I've done something like this before. I had a set of complex views in a GalleryAdapter, one of which was a ViewFlipper that contained an EditText (the editable bit) and a TextView (the static part). There was a button to the right of the ViewFlipper, and when the user pressed that button I called showNext()/showPrevious() as appropriate to switch between the editable and visible views.
To clarify, I had one adapter that extended BaseAdapter, and in my getView() method I assigned onClickListeners to the buttons that would modify the objects in the view I was about to return.
I have listview of edittext boxes. I also have a button to add a new row into the list, thus, the list length is dynamic.
However, when I entered text into the edittext and press the add-button, my input disappears. I think my input is not captured when I added a new row into the list. How can I save my input so when I change the amount of rows, my text remains?
Your idea is fine, but you need to persist the edittext's data. Try adding textwatcher to each edittext and save the text for each edittext in a array or something
A ListView recycles its row views anyway, so this would still be a problem without changing the list length. Rows that scroll off the visible portion of the ListView would lose their contents. To fix the problem, text entered into the EditText needs to be stored in the underlying data model, not just in the view.
You'll probably need to add a TextWatcher (with addTextChangedListener()) to each EditText. When one of the TextWatcher callbacks is called, get the text from the EditText and save it into the data model.
To keep track of which data element to save to during the TextWatcher callback, you might use a technique similar to the one used with the RatingBar in this CommonsWare sample.