Dynamically add view to android list? - android

Hi I have run into a design issue and can't figure out the best approach to take. Here is a sample of what I am trying to achieve:
When the user clicks on the Plus icon another row will be added below the current condition. Currently the condition row is in a listview and is setup using a custom adapter. The left col is filled with the options that the user has chosen from the select columns view. All that is working but I can't seem to figure out how to add a new row. So is there a way to dynamically add a new row to the list and fill it with the same data? OR is there a better alternate way to set this up?

So your custom adapter is starting off with three rows? Then when the user clicks on the plus icon, the onClick listener should call a method on your adapter that sets some kind of indication that there are now four rows (after which it calls notifyDataSetChanged()). And your getView() method should look at that indicator and if position == 2 (third row) and the indicator is not set, it returns an "Order By" view. If the indicator is set, then it should return a "Condition View" and only return an "Order By" view when position == 3 (fourth row). Does that make sense?
Your adapter reflects the state of your list model at any moment in time. Any event that occurs where you want to change the ListView should update some kind of state in the adapter. Then getView() should always check that current state when returning a view. So always think in two phases:
Your event (like onClick) updates some model data in the adapter somewhere.
Your adapter's getView() method always checks the current model data in the adapter to figure out how to set up the view for the given position.

Related

Mark all the previously selected items from a ListView in an other Color when returning from activity

I am using a ListView in combination with a HashList to show data from an SQL Database through JSON. Everything works fine. But now I want to have a special feature.
In the ListView are round about 400 Items. So if i click one of the item to view its content and switch via Backbutton back to the Listview, i want to have the viewed Item from the Listview being marked in another color so that the user sees directly which oh the items he already has clicked.
I painted an little grafik for you, for better understanding
On the left side, the original View. After i View the 1st,5th and 8th Item of the list, the list should look like at the right side.
Is this possible?
You can implement your own adapter, then have an boolean array to store the current status of every row, and in the getView function assign a different color to the background of the selected rows based on your boolean array.
For this you will need also to set an onClick event in your rows to open the previsualization view and there also check the boolean array as "visited".
For more info about how to make custom efficient adapters, please read this
To simply let the user choice more than one row you can use
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
in your list

does ListView re-draws itself every time the adapter changed?

I want to make a list of SMSs in my own application..
my question is about the steps makes by the adapter (automatic)
-when new message arrived I adds it to list object (not the ListView).
-then I passes the list to the adapter of the listView.
the adapter GetView() method run for every Item in the list
-I notify the listview about the change.
- the listview re-draws all its existing rows and then draw the new row.
My question: this behavior (re-draw and redraw, it mean every row will be drown times equal to the total rows) affect the performance?
*if the question is not clear I say: does the ListView Draw all the Raws just to add new row? *
The ListView doesn't redraw every single item in order to add one. It will only be drawn when you scroll to it. And yes you have to notify ListView that the change had happened.
I guess you want to add the item at the top position. So all other item's position and index will change . Adapter will redraw the whole list(only elements which are visible on the screen) .
For performance you can use viewHolder pattern.
see this link
[Making ListView Scrolling Smooth][1]http://developer.android.com/training/improving-layouts/smooth-scrolling.html

get text from specific position in listview android

I have a ListView with custom_row , every row has a textView1 and a textView2 , the list has now 2 records , and i have a button that is not on the list.
When i click the button i want to get the text from textView2 of the 2 records.
Is it possible?
I would take a shortcut, you ListView is being populated by an Adapter that uses a dataset. This dataset can be almost any datastructure such as Array, ArrayList, etc.
The layout you define, such as custom_row in you case only defines the structure of your view i.e. "where" items will show within on item on the list.
On the other hand, it is still your responsibility to tell the ListView "what" to show within the textView1 and textView2. You do this using the Adapter which connects the ListView to the dataset. More often than not, the ListView is a one-to-one mapping of the dataset i.e. the first item on the list is the first item in you dataset (I don't know what you are using for only two items, might be an array).
The ListView calls getCount() on the Adapter to find out how many total views there will be. It then call getView() for each view to be shown on the screen. It is in this method that you define what will actually show in a single view on the list (your custom_row).
Now you would know which entry of the dataset is supposed to populate which view in the ListView so you can just read it off there. For example, if your getView() does:
textView2.setText(getItem(position).getSomeTextField());
And the original dataset is an ArrayList named listDataSet
You could just do listDataSet.get(2).getSomeTextField()
NOTE: You will have to manage the scope of the dataset so that it's visible from wherever you are calling.
Get back your ListView (maybe already stored in an object thanks to findViewById, or by calling getListView() on your ListActivity).
Then call getItemAtPosition() on your list view, with the position you want.

Android CustomHow to get Child Views from Custom ListView enclosed within linearlayout without clicking listview

I have a custom listview with two buttons and two textViews enclosed within linearlayout. I want to iterate through the listView without clicking the listView rows. When i do
listView.getChildAt(position);
position can be any integer from 0 onwards it gives null output, what is the other way to get views from listView. Please help.
There isn't really an interface exposed for getting the view objects from a ListView. These are constantly being switched out and stored for recycling as rows scroll in and out of the visible area.
However, you can do:
listView.getItemAtPosition(position);
to get the model (item) at a given position in the list. If you need to operate on information that the user changes in your list, then you would want to first save that information back to the underlying model individually when each component changes and then later aggregate over your underlying data objects to do your calculation.

Editable ListView - Update Adapter with UI Values

I would like to create a ListView in Android where I have the ability to add a new blank row, and have the controls in the new row be editable. Then on some event (either the user clicks add again, selects another row, or some other trigger I haven't determined yet), I want to update the Adapter with whatever values the user entered into the editable row.
I currently have editable controls within each row and the ability to add a blank row via a menu item. I cannot figure out how to sync the user entered data with the Adapter.
I originally thought that Adapters are two way data binds, but that doesn't seem to be the case. From my research and experimenting, if I change an Adapter value and call notifyDataSetChanged(), then the UI gets updated. Is there a reverse operation?
I was able to accomplish the two-way data binding by adding a KeyListener and OnFocusChangeListener to each of the controls on my row's View. Both of these events will call into a method I created on my row's View to loop through all the controls on the view and update my adapter's data with the current values. I had to make sure to not call notifyDataSetChanged(). This method is necessary only for programmatically changing the data source object and having the UI reflect the changes.
Not the most efficient way ever, but it works decently well.
Another thing to note, adding and deleting rows I needed to set both control and view level squelching of updating of my adapter view. For deletions, what I did was add a long click event on my row's View to have a menu with a delete option. Then I started squelching updates on a View level, because I programmatically edit my data source object to remove the given row data and call notifyDataSetChanged() (necessary otherwise OS will throw an exception). Squelching here makes sure I don't hit my events and get into an infinite loop and that my data is properly synced. Then on the deleted row View I set all my controls to squelch their event's update adapter. This is because the deleted row View will still have focus, and I want to make sure I don't update my data source object with values not on the UI. This flag gets flipped once I get the row's View back from the ListView recycling process in getView() of my adapter.
Adding a new row I also need to squelch on just the row's View level. This is because I programmatically change my data source with a new empty row of data, and call notifyDataSetChanged. Reasons are exactly the same as the delete.
your problem is hat you, for example may hav 300 items on the list (that are repesented by EditItems) but only 12-20 EditItems in reality that are recycled.
i guess your only way to know that use has finished with his row is FocsedChangedListener on each of your views.
Once focus is off use:
in your adapter's getView use: if v is your View then do v.setTag(position)
in the OnFocusChangedListener once focus is off use: int pos = (Integer)v.getTag(); mAdapter.updatePosition(text,pos)
make sure your adapter has an updat mthod that will update the object in position pos with the String 'text'
To refine C Nick's solution a bit, you can use EditText.addTextChangedListener.

Categories

Resources