Hidden Field in ListView Android? - android

I'm wondering if there is a way to hide a field in Android.
I tried with setting the value in a TextEdit and then making the TextEdit invisible, but the result is that the value is invisible, but the control takes space.
In my case, I want to store an extra value in a row of a ListView.
Is there another solution besides using hidden fields?

Use View's public static final int GONE field.
In your case textEdit.setVisibility(View.GONE), or in xml android:visibility="gone"
Setting the view to INVISIBLE does not take layout into consideration, but GONE does.

View has methods setTag() and getTag() that you can use to associate some extra data with row of ListView. For example I'm using CursorAdapter class and in newView() and bindView() methods I call view.setTag(). Then in OnItemClickListener I call view.getTag().

I bet the only invisible data contained by rows of ListView is id (long). I was also trying to find a way to pass some data (like uuid) to the row click handler, however seems a "GONE" TextView is the best solution for now...

Related

Updating android listView adapter's items' view

I'd like to change my listView items dynamically. The algorithm goes like this:
1.I create the default view for my listView using adapter and show it to user. The list item contains Imageview, textview and another imageview which is invisible.
2.The data is beeing dowloaded in the meantime.
3. after my data is downloaded, I'd like to check whether my listview contains any of downloaded items. If yes, I want to make visible the previously invisible ImageView for this item.
Should I add some kind of method to my adapter, call it and then call invalidateViews(), notifyDataSetChanged(), or notifyDataSetInvalidated()? Or maybe there is some kind of standard way to find my adapter's item by Id or sth and then make visible the imageview for this item?
This list update operation is the only one left to implement for me.
Should I add some kind of method to my adapter, call it and then call invalidateViews(), notifyDataSetChanged(), or notifyDataSetInvalidated()?
Yes, exactly.
maybe there is some kind of standard way to find my adapter's item by Id or sth and then make visible the imageview for this item?
Above mentioned way is enough. There is no such standard or special way to do it AFAIK.
Read the Displaying Bitmaps Efficiently introduction and in particular the part about Handle Concurrency. This will give you all the information you need.

Individual preference for each listview item example

What's the easiest way to create separate preferences for each item in an Android listview? I found this question, but it's kind of general. Could anyone be more specific, possibly show a quick example?
If you want to do this, you should write some code into your adapter getView method.
You can check the position (with an if-clause) in the method and add a custom desgin/layout depending on the position.
If you use a LinearLayout or RelativeLayout in your ListView item XML you can fill this programmatically in the getView method with all the widgets (edittext, checkbox, etc) you need.

getting more data from a listview row

I have a ListView that contain several TextViews. Once a TextView is pressed I get the onClick called with the view. What is the best practice to get the other text views on that row? IDs of the text views are similar along the rows so I need to keep on the context of the given view. I would guess I need to get the Parent of the given view and then grab the rest of the text views from it.
Thanks.
Yeah, you would need to get the parent of the current textview, then get all the children contained within that parent.
Depending on what you need to do with the textviews you could either call FindViewById on the parent view to get each TextView by ID. Or you can iterate through the children as suggested in this question/answer:
Android - get children inside a View?
I know people mention that calling FindViewByID is a taxing process, so consider storing the references to the textview's in some sort of object so you can quickly get the references to the other textview's within the row without having to look them up all the time.
ViewHolder's work pretty well, as they store the references to the textview's within an object (which you only need to fill once during creation) but it requires setting up your own customized adapter.

How would I use a different row layout in custom CursorAdapter based on Cursor data?

Background: I'm trying to implement a messenging system in my app, and I'm writing a custom CursorAdapter to display the messages in a ListView in the chat window. I want to use a different row layout for incoming and outgoing messages (information that is saved in the SQLite row in the cursor). Each row has the same elements in it with the same IDs, but they are arranged differently.
The Problem: Currently, I have overridden newView() and bindView(). When the ListView is first populated, it creates all of the Views perfectly, checking each row to see if it's either incoming or outgoing, and inflating the proper XML file. However, when I scroll or a new message is added to the window, the adapter recycles Views for the wrong rows. I would override getView(), but it is not passed the Cursor as a parameter, so I have no way of knowing whether the row should be incoming or outgoing.
I'm not looking for code, but rather, some suggestion for an elegant implementation. Thanks in advance!
Here are two possible solutions:
(1) Use a single layout for all items, which you can adjust when binding to show as desired. The most straight-forward way would just to have the root view be a FrameLayout which contains N children for each of the different states, and you make one of them visible and all others gone when binding. Of course you want to take care to not let this cause your items to explode in the number of views they contain.
(2) Implement Adapter.getItemViewType() http://developer.android.com/reference/android/widget/Adapter.html#getItemViewType(int) to tell the list view about the different types of items you have so it will recycle the correct one.
Just a few thoughts, I personnaly find the whole ListView and CursorAdapter combination to be a little... err... is bloated the right word? Would it just be simpler to have a ScrollView/ LinearLayout combination that will just add the appropriate TextView as requested?
But as for your solution, since the user seems to be unable to change the order of the messages as they are added, you could add a ArrayList<String> field to your custom CursorAdapter that will keep track of whether the messages are incoming or outgoing. Something like:
private ArrayList<String> cursorMonitor; //"incoming" and "outgoing" as your options.
...and then inside wherever the ListView gets populated just use
cursorMonitor.add(my_cursor.getString("outgoing_or_incoming"));
And then in the getView() you can override it and use the cursorMonitor to determine which layout you need to inflate.

android access listview row from another row

Suppose I have a listview with 3 rows. If the user clicks a button in row 1, it expands a menu (it's just a linear layout that's shown/hidden). If they then click on an item in row 2, I'd like to be able to collapse the menu in row 1. Is this possible? If so, how?
Save a reference to the View you would like to manipulate later. Probably wrap in a final variable, then you will be easy to change.
You can always use getContext() on the View to get a reference to the containing Activity, which you can cast to a ListActivity (assuming you're using that) and call the usual methods to get the data at that position in the ListAdapter:
((ListActivity) v.getContext()).getListAdapter().getItem(1)
Then you can manipulate your data however you want to and call notifyDataSetChanged() on your ListAdapter.
But it'd probably be a lot easier for you and your users to just use ExpandableListView, which gives you expandable lists with predictable interactions your users already know. You can check the history activity in the Browser source code in AOSP for a real life example. And if it doesn't exactly meet your needs, you can always yank the code for ExpandableListView itself from the Android source.

Categories

Resources