Binding a class to a view/viewgroup in Android? - android

In Android I have a class contains 2 properties. I've created many objects from it and put them into an ArrayList, then using ArrayAdapter to bind the List to a TableView as you see. Once the list is changed, the view will be updated.
The question is, how can I bind my properties with these 2 TextViews in a table-row (a so-called ViewGroup in Android)...? I've tried to take data from the list into 2 String-array: dates and values, but that's odd...

In the getView() method of your adapter, use setTag(), passing in the object you want to attach. You can then, for example, retrieve the object in onListItemClick() (assuming you're using a ListView) by calling Log logItem = (Log) view.getTag().

Related

Display two Reclerview in single activty with same recyclerview adapter

I have two List coming from webservice Like past and future.Sometimes both List have data and sometime only one list have data. And how set the differt RecyclerView for pastList and FuturList with same adapter.
As long as your model objects are the same, it sounds like you need to have two versions of the adapter, where you can assign each list of data to each different adapter.
So if your adapter was MyObjectRecyclerViewAdapter, you could make two instances - pastObjectsAdapter = MyObjectRecyclerViewAdapter();
futureObjectsAdapter = MyOjbectRecyclerViewAdapter();
Then assign one to each of your RecyclerViews and update the list on each with your lists from your webservice.

FirebaseListAdapter, How to update individual list views after populateView

I have implemented a firebase list adapter to load a list of items (List A). My data structure is setup such that within List A, each item also contains a reference id to some information located somewhere else (isNice) in the database. Likewise:
ListA
- ObjA
- title : "hi"
- id : "ObjAid"
isNice
- ObjAid : "true"
I'm currently using another database operation to look up the id in the "isNice" child, passing the "ObjAid", the list position, and then a resultReceiver to get the result back. My problem is, when the resultReceiver get a resultData (the value "true"), I have no idea how modify the data within firebase list at the specific position.
My past experience is to use load my data into my own ArrayList and create a custom adapter for the listView, in that case, I could easily update the populated view as extra information is loaded. I would like to avoid setting up my own ArrayList to store the data for the time being, favoring the simplicity of the FirebaseListAdapter. All tips are appreciated, thx :)
After some trial and error, I've instead gone with the RecyclerView + FirebaseRecyclerViewAdapter approach. Using recycler is better for the long run too, in my use case the list could get to 3K+. Using RecyclerView.findViewHolderForAdapterPosition() I can reference the specific itemView after I get the data from the result receiver likewise:
// Reference the Item's view at the specific position
myViewHolder itemViewHolder = recyclerView.findViewHolderForAdapterPosition(position);
// Reference and set the views and here......

Best strategy to create a ListView of forms in Android?

I need to create a dynamic list of forms. The user would be able to input data into each element of the list (which would contain an editable text box and other elements).
What is the best approach? How would I access the inputted information in each list element? Would each list element have to be its own Fragment?
1) Decide what elements you need in each list item
2) Create a data class with the member variables to how your data. one data object will be initialized per listview item //note this is not required, but it can help.
3) Create a layout (list_view_item_layout.xml) for your listview items
4) Declare your ListView in your layout.xml, ensure that you give it an android:id.
5) Declare and initialize an array of your data objects (or just an array of data) that holds all the data objects that you want displayed in your list
5) Create your MyListViewAdapter.class that extends BaseAdapter. this class will inflate your list_view_item_layout and populate the views with the data that is in your data array
6) call ListView#setAdapter(); passing in myListViewAdfapter, context, R.id.document_list_view //I htink that is correct
Thats about all there is to it.
Look for tutorials for custom list view adapters, there are a billion + tutorials

How to add/remove data from a list view (Android)?

I'm using a list view to displace names. The user needs to be able to add a name to the bottom of the list view and to be able to delete names within. The names are saved using SharedPreferences and loaded into an original string array which I then load to an ArrayAdapter. The problem with this method is that, unless I fill up the entire string array, I get a NPE for the ArrayAdapter. So my solution was to set the string array to only be as big as the number of names. This means, however, that I cannot add a name to the list, as the list is only so long.
What's the best way to be able to add and remove strings from a list view and still be able to tell what they are?
After changing values of listview data please put code
notifyDataSetChanged();
Use the List<T> overload rather than the static array for the ArrayAdapter constructor. Then you can add or remove items to your List<T> and call notifyDataSetChanged() once you are done. Using a dynamic data structure like a List<T> will avoid any NPE.

Extending BaseAdapter, passing variables

I'm not understanding how you pass variables to these adapter classes. Most of the examples I've seen, you pass a context (Activity, usually). How exactly do you pass the variables, the data that you want your view to display?
BaseAdapter is used to connect a data source to view. Each item in the row needs to be connected to the property of the object. Take a look # this blog post - http://dup2.in/2011/08/08/android-custom-listview-baseadapter-tutorial/
The simple listview example is having only one textview in each row which corresponds to a single String element in arraylist.

Categories

Resources