spinner inside listview custom row layout and Adapters - android

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.

Related

Android ExpandableListView save input in each item's edit text on button click

I'm new to Android and can't quite figure out the right approach for the problem I'm trying to solve. I have an ExpandableListView with several items. Each item has an EditText, except the last item has a button. The contents of the EditTexts are to be loaded from the database. When the button is clicked or when the activity is navigated away from, I want to save the contents of each EditText to my database.
I'm not sure what to call from the activity's class, what to call from my adapter, and how exactly to access each item appropriately. Code is welcome but not necessary, I'm just looking for guidance on the general approach. Thanks.
I'd recommend putting a method in your adapter called saveAllValues. It iterates through the list of objects in the adapter and saves them to the database. Call this when your button is clicked and in your activity's onStop() method (which is called when the activity is no longer visible).
You should have your activity fetch the values for the item IDs in an AsyncTask in its onCreate method. Then pass the list of id/value pairs to the adapter in its constructor. It should maintain this list so it can go back through it and save the IDs and values to the DB.
Hope my answers in these links help:
Values of counter changes after scrolling ExpendableListView shows how to maintain the list inside the adapter and how to get list from the activity.
Remove the divider view in the expandablelistview last item shows how to make the last child different from the others.

Handling events on individual textviews in a listview extended from list activity

I have an activity which extends a list activity. The activity extracts data in the form of json array from the web server. The json array which is returned contains data in groups of 3 name/value pairs. I display the data in 3 text views one in each views in the listview from the simple adapter. The onclickitem event is working wenever i click on the listview item but wat i want is to be able to define different events for clicking on different text views which are a part of the list view.
Add your onClickListener on the individual textviews, not on your whole View bound by the adapter.
textview.setOnClickListener(new View.OnClickListener{...})
You have two options
Create custom adapter for this and handle the desired action in getView() method.
Get the current selected item view using getChildAt(); using this view you can get the 3 text views and perform an action.
But as per my experience and for the sake of more flexibility first option is much better. I have answered a question here which has a sample custom adapter, if you want you can have a look.
Android Java: Unable to selectively highlight rows in a ListView via onScroll listener

design for updating information in a view of listview

i have an app that has a listview. each view inside of the listview has custom xml and has buttons checkboxes and textviews. i have to get the previous saved settings for the checkboxes from an sqlite database and set them. and if the user changes the checkboxes or radio buttons i have to save those settings in the database.
tried several different ways to do this and most resulted in error and would not display the correct setting for checkboxes when coming back to the page.
what finally worked is the design shown below. where i put any database calls as close as possible to the actual view. however it looks like if there is many rows in the listview it would make many calls to the database and it could get slow.
is my design correct or is there a better way
psudocode
onCreate
instantiate arraylist adapter and set adapter to view
end onCreate
arrayadapter class
getView(){
call databsase and get previous settings for checkboxes in this view and
set the checkboxes to show checked or not depending on database saved settings
onClicklistener or oncheckedchanged listener for getting checkbox positions if changed
call database and set method to store updated positions of checkbox, radiobutton or spinner
}
end arrayadapter
Your approach is correct and your concern is warranted. I can suggest you the following method, but its performance will have to be tested:
onCreate
instantiate arraylist adapter and set adapter to view
end onCreate
arrayadapter class
getView(){
call databsase and get previous settings for checkboxes in this view and
set the checkboxes to show checked or not depending on database saved settings
----> Create two ArrayLists: ArrayList<Boolean> for CheckBoxes
----> and ArrayList<Integer> for RadioButtons.
----> Fill these lists using the values from the databse
onClicklistener or oncheckedchanged listener for getting checkbox positions if changed
call database and set method to store updated positions of checkbox, radiobutton or spinner
----> Here, update the lists, not the database.
----> Execute a AsyncTask to update the database whenever the lists change.
}
end arrayadapter
Another think you can do is to load data from database in Activity.onStart() and save it back in Activity onPause(). All other time use ArrayList to track changes in UI.
In that case you will have this heavy db operations much rarely during the Activity life cycle.

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.

Preselect items in listView Android

All i need is an idea or path to continue searching, i have a list of items that i add to a listview adapter. And i am trying to make some items in the list to have a specific background color (as an example) when the activity loads. Something like having those rows "preselected". The method public ListView getListView() does not help me much.
Thank you either way.
Need to set Multiple Choice Mode via setChoiceMode(), and then you can call setItemChecked() for each of the items you want checked. Then you probably have to make a custom adapter that overrides getView(), cast the parent to ListView and ask if the View (a list item) you are getting at the position given is in fact checked by calling something like getCheckedItems() on the parent. Then you can do what ever you want with that view, like set its background color etc.

Categories

Resources