I know how to access a listview item through an onitemclicklistener but how do I do something like change the background color in code.
sudo code:
lv[0].setBackgroundResource(R.color.red); //change the background of the first listview item
Is there a way to access each view of the listview?
#Matt: It's not that it's weak, you just have to understand the way it works.
Yellavon, at any given time, your ListView only contains the visible items, so you can't access and change items directly. As they scroll out of view, the views are recycled, and populated with data by the ListAdapter. It's within this adapter that you need to handle the cases where items should differ. If you've created your own custom ListAdapter (e.g. ArrayAdapter, BaseAdapter), in your getView() method, simply add some logic within to handle the cases in which the background color should change.
Let's say for example, you have a list of integers, and you want any integer >= 50 to show up in red:
if(items.get(position) >= 50) {
myView.setBackgroundColor("#FF0000");
} else {
myView.setBackgroundColor("#000000");
}
(It's important to make sure to handle the else cases, as you may get a recycled view of an item that had been colored red. In this case, you'd have to reset it to whatever default background color you need.)
If you've never built a custom adapter, this excerpt from CommonsWare's book on creating custom ListAdapters is a great resource.
EDIT: Further thought, based on your comment:
In your custom ExpandableListAdapter
private Object lastSelectedObject;
public void setLastSelectedObject(Object obj) {
lastSelectedObject = obj;
}
public Object getLastSelectedObject() {
return lastSelectedObject;
}
Implement onListItemClick(ListView l, View v, int pos, long id) in your ListActivity
#Override
protected void onListItemClick(ListView l, View v, int pos, long id) {
super.onListItemClick(l, v, pos, id);
((CustomAdapter)l.getAdapter()).setLastSelectedObject(items.get(pos));
}
Now, back in getView()
Object obj = getLastSelectedObject();
if(obj != null) {
//handle background switching for your View here
} else {
//reset background to default for recycled views
}
Have a look at this example
int first = view.getFirstVisiblePosition();
int count = view.getChildCount();
for (int i=0; i<count; i++) {
TextView t = (TextView)view.getChildAt(i);
if (t.getTag() != null) {
t.setText(mStrings[first + i]);
t.setTag(null);
}
}
Seems like getChildAt is the method you are looking for.
The ListView class in android is weak.
The short answer to your question is no, not easily.
Someone asked about it at the Google I/O and the answer from the android team was to use a vertically filling LinearLayout and just add a stack of child views into it (which basically gives it the same functionality as a ListView).
You can do getChild(x) to get any of the views
Related
When I'm using parent.getChildAt(position).setBackgroundColor(Color.GRAY); in my public void onItemClick(AdapterView<?> parent, View view, int position, long id) it colorizes, but it works strange.
When I click first or second item it colorizes it... and every item away ~five records. Sometimes I've got NullPointerException. Completely weird, because position is unique and it should recieve me appropriate View, but it doesn't.
I saw solution with overriding getView method, but I'm using this adapter in different places. I just want to color clicked item. How to get reference to selected view?
EDIT:
In my adapter class I created:
public static int selectedItem = -1;
I added this to my overrided getView method:
if(selectedItem == position){
parent.getChildAt(position).setBackgroundColor(Color.GRAY);
}
In my activity I added that:
myAdapter.selectedItem = position;
myAdapter.notifyDataSetChanged();
And It doesn't work. Where I do a mistake?
It's not a bug - it's the way ListView re-uses the views to save resources.
So to avoid this behavior you should on every getView() set all used attributes for all your views.
Updated - to be quite clearIn your particular case it means that you should set color like this:
1) In onItemClick() - in your actitivity - you should remember given position as selected:
myAdapter.selectedItem = position
2) In getView() - in your adapter:
if(selectedItem == position)
parent.getChildAt(position).setBackgroundColor(Color.GRAY);
else
parent.getChildAt(position).setBackgroundColor(0);//or whatever defauld color
Update 2
If you want to select many items you should use some structure (like HashSet) to hold all the selected items:
1) In your activity class add member:
public static HashSet<Integer> mSelectedItems = new HashSet<Integer>();
2) In onItemClick() use following to flip selected state:
if(mSelectedItems.contains(position))
mSelectedItems.remove(position);
else
mSelectedItems.add(position);
3) In getView():
if(MainActivity.mSelectedItems.contains(position))
parent.getChildAt(position).setBackgroundColor(Color.GRAY);
else
parent.getChildAt(position).setBackgroundColor(0);//or whatever defauld color
At first read this article;
Then use ViewHolder pattern;
And try to setBackgroundColor() in onItemClick() like this:
view.setBackgroundColor(0);//or whatever defauld color
I have one listview in my application,it contains two rows one for task and another one for alarm,date,severity. Initially first row of the list item only displayed for all list item and the second one is invisible. When i click the list item the second row displayed for that item as well as click another list item at that time the above list item closed that second row. Its working fine for me...My problem is if i open one list item and then swipe the listview at then i click the another list item at that time the above one cannot be closed because the above list item instance will be chnaged.please any one help me how to solve this problem...
int lastselectedPosition == -1
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id) {
TextView textviewDate=(TextView)view.findViewById(R.id.taskTimeidDaytoDay);
selectedtaskDate=textviewDate.getText().toString().trim();
if (lastselectedPosition == -1) {
Log.i(TAG,"Loopif:"+lastselectedPosition);
TextView twTaskTime = (TextView) view
.findViewById(R.id.taskTimeidDaytoDay);
TextView twSeverity = (TextView) view
.findViewById(R.id.severityidDaytoDay);
TextView twAlarm = (TextView) view
.findViewById(R.id.alarmidDaytoDay);
twAlarm.setVisibility(view.VISIBLE);
twSeverity.setVisibility(view.VISIBLE);
twTaskTime.setVisibility(view.VISIBLE);
lastselectedPosition = position;
lastSelectedItem = arg0.getChildAt(position);
} else {
// Log.i(TAG,"LoopElse:"+lastselectedPosition);
lastSelectedItem.findViewById(R.id.taskTimeidDaytoDay)
.setVisibility(lastSelectedItem.GONE);
lastSelectedItem.findViewById(R.id.severityidDaytoDay)
.setVisibility(lastSelectedItem.GONE);
lastSelectedItem.findViewById(R.id.alarmidDaytoDay).setVisibility(
lastSelectedItem.GONE);
if (lastselectedPosition != position) {
view.findViewById(R.id.taskTimeidDaytoDay).setVisibility(
view.VISIBLE);
view.findViewById(R.id.severityidDaytoDay).setVisibility(
view.VISIBLE);
view.findViewById(R.id.alarmidDaytoDay).setVisibility(
view.VISIBLE);
lastselectedPosition = position;
lastSelectedItem = arg0.getChildAt(position);
} else {
lastselectedPosition = -1;
lastSelectedItem = null;
}
}
GetView():
#Override
public View getView(int position, View view, ViewGroup parent) {
Log.i("XXXX", "Inside getView");
final DaytoDayTaskGetterSetter objDaytoDaygetset=getItem(position);
TextView textviewTask;
TextView txtviewAlarm ,txtviewTaskTime ,txtviewSeverity;
Log.i(TAG,"InsideGetView:"+position);
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if(view==null)
{
view=inflater.inflate(R.layout.daytodaylistlayout,null);
}
Log.i("XXXX", "before first test");
textviewTask=(TextView)view.findViewById(R.id.tasknameidDaytoDay);
txtviewAlarm=(TextView)view.findViewById(R.id.alarmidDaytoDay);
txtviewSeverity=(TextView)view.findViewById(R.id.severityidDaytoDay);
txtviewTaskTime=(TextView)view.findViewById(R.id.taskTimeidDaytoDay);
return view;
}
In first i click the "gdfgdtet" list item it show another row and then i click the second list item "dfgsdgsd" at that time the above list item "gdfgdtet" closed the second row.This is a normal output.Suppose if i open the "gdfgdtet" list item and then swipe the listview at that time both of "gdfgdtet" "dfgsdgsd" will be opened and crashed...because the above one list item reference changed when i am swiping please how to solve this problem...
I'll try to provide you a good answer that explains why you are having this problems, but the general idea is that you have to see this video - http://www.youtube.com/watch?v=wDBM6wVEO70
please take my words kindly - you don't seems to understand what ListView + BaseAdapter recycling mechanism is all about, and I strongly recommend you see the full video I linked you to, and read more about that.
in general, the specific problem in your code is that you are holding reference to listview item (lastSelectedItem), then trying to use it latter assuming it's still representing same list item. that's wrong. in that stage (after scrolling) the view already been recycled to represent another item in the list (based on the adapter implementation).
listView's number of childs is not the size of adapter.getCount()!!!!!!!!
listViews's number of childs = number of visible list items on screen + 1 + headers + footers
let's say you have the 5 first items visible on screen, then you are scrolling down. when you see the 7 item you actually see the same view instance that used to show the first list item and been recycled.
getView will call in this stage with convertView != null and position in the adapter to let you reuse the item by putting new values such different text/image to the same instance
this mechanism provides ability to display list of "infinite" number of items in the list, and holding in memory only a few number of views. imagine that you have list of 5000 items in the list, and each one of them have different view instance - you would get outOfMemory exception in a sec!
complete explanation about that would be hard to write in stackoverflow answer's context.
it just too long trying to explain one of the most important and complex UI components in android, but this links would be a good start:
http://www.youtube.com/watch?v=wDBM6wVEO70
How ListView's recycling mechanism works
http://mobile.cs.fsu.edu/the-nuance-of-android-listview-recycling-for-n00bs/
if you are interstead in "quick" fix for your specific problem, the solution would be:
hold in the data structure represents your list item additional field indicating if it in "close" or "open state. when item been clicked - change the data accordinly and call notifyDatasetChanged(). inside the getView() check if item is open or close and populate it accordinly
by the way - it's not only "quick fix" solution, but also the right thing to do anyway
You should pay attention to Tal Kanel's answer and consider this one to be an extension to it. His advice will help you in the long run.
Add a boolean field to DaytoDayTaskGetterSetter class:
public class DaytoDayTaskGetterSetter {
....
....
boolean open;
public DaytoDayTaskGetterSetter (.., .., boolean o) {
....
....
open = o;
}
....
....
public boolean shouldOpen() {
return open;
}
public void setOpen(boolean o) {
open = o;
}
}
In your getView(), check if the object has its open value set:
DaytoDayTaskGetterSetter obj = (DaytoDayTaskGetterSetter) getItem(position);
if (obj.shouldOpen()) {
// Set visibility to true for the items
} else {
// Set visibility to false for the items
}
On list item click, traverse the list and set open for all list items to false. Use the position to retrieve DaytoDayTaskGetterSetter and set its open to true:
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
for (DaytoDayTaskGetterSetter obj : listContainingObjects) {
obj.setOpen(false);
}
DaytoDayTaskGetterSetter clickedItem = (DaytoDayTaskGetterSetter)
yourAdapter.getItem(position);
clickedItem.setOpen(true);
yourAdapter.notifyDataSetChanged();
}
Edit 1:
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
DaytoDayTaskGetterSetter clickedItem = (DaytoDayTaskGetterSetter)
yourAdapter.getItem(position);
if (clickedItem.shouldOpen()) {
clickedItem.setOpen(false);
} else {
for (DaytoDayTaskGetterSetter obj : listContainingObjects) {
obj.setOpen(false);
}
clickedItem.setOpen(true);
}
yourAdapter.notifyDataSetChanged();
}
getItemIdAtPosition() is a function in android used to get the id of an item in a list view given its position
is there any way of doing the reverse, i.e. getting the position of an item is a list view given its id?
No. You have to do it manually. Create a public method inside the adapter you are using; in that method, loop on the adapter items and check each item id. If it is == to the method param, then return the index.
public int getItemPosition(long id)
{
for (int position=0; position<mList.size(); position++)
if (mList.get(position).getId() == id)
return position;
return 0;
}
Update: You might as well save a HashMap for position/id in your adapter, if lookup performance represents a problem for your use case.
Update: If you want to use this method outside the adapter, then use below:
private int getAdapterItemPosition(long id)
{
for (int position=0; position<mListAdapter.getCount(); position++)
if (mListAdapter.get(position).getId() == id)
return position;
return 0;
}
Not sure if the question is still open. Nevertheless, thought I will share how I did it so that it may help someone who is looking to do the same thing.
You can use the tag feature of a view to store the mapping between that view's id and its position in the list.
The documentation for tags on the android developer site explains it well:
http://developer.android.com/reference/android/view/View.html#Tags
http://developer.android.com/reference/android/view/View.html#setTag(int, java.lang.Object)
http://developer.android.com/reference/android/view/View.html#getTag(int)
Example:
In the getView method of your list adapter, you can set the mapping for that view's id and its position in the list, something like:
public View getView (int position, View convertView, ViewGroup parent)
{
if(convertView == null)
{
// create your view by inflating a xml layout resource or instantiating a
// custom view class
}
else
{
// Do whatever you want with the convertView object like
// finding a child view that you want to set some property of,etc.
}
// Here you set the position of the view in the list as its tag
convertView.setTag(convertView.getId(),position);
return convertView;
}
To retrieve the position of the view, you would do something like:
int getItemPosition(View view)
{
return view.getTag(view.getId());
}
A point to be noted is that you do need to have a reference to the View whose position you want to retrieve. How you get hold of the View's reference is dependent on your specific case.
No. Depending on what adapter you're using to back your ListView the id and position may be the same (I haven't looked at all BaseAdapter subclasses so I cannot say for sure). If you look at the source code for ArrayAdapter you will see that getItemId actually returns the position of the object in the adapter. If the position and id are the same, there is no need to use one to get the other. Otherwise you just need to search the adapter for the object your looking for - using either position or id - and you can find the other value.
If what you're talking about is getting objects using some unique key - i.e. one that you define - that can be done. What you need to do is set up your adapter to take a HashMap instead of an ArrayList or regular List of objects. Then you can create your own method to find by key by simply pulling that value from the HashMap.
it's too late to answer but for benefit...
for example if you have listview and you want to get id with click listener you can get it by >>
cListview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
// for id or any informations
itemID = String.valueOf(catList.get(position).getItemID());
// for name or any informations
itemName = String.valueOf(catList.get(position).getItemName());
I have a custom ListView with two TextViews both containing different values. What I want to be able to do it get the contents from one of these TextViews when an item is clicked.
This is the code I have so far:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String value;
// value = (get value of TextView here)
}
});
I want to be able to assign value to the text of one of the TextView's.
Although #Sam's suggestions will work fine in most scenarios, I actually prefer using the supplied AdapterView in onItemClick(...) for this:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Person person = (Person) parent.getItemAtPosition(position);
// ...
}
I consider this to be a slightly more fool-proof approach, as the AdapterView will take into account any header views that may potentially be added using ListView.addHeaderView(...).
For example, if your ListView contains one header, tapping on the first item supplied by the adapter will result in the position variable having a value of 1 (rather than 0, which is the default case for no headers), since the header occupies position 0. Hence, it's very easy to mistakenly retrieve the wrong data for a position and introduce an ArrayIndexOutOfBoundsException for the last list item. By retrieving the item from the AdapterView, the position is automatically correctly offset. You can of course manually correct it too, but why not use the tools provided? :)
Just FYI and FWIW.
You have a few options. I reference the code from your previous question.
You can access this data from the row's layout view:
ViewHolder holder = (ViewHolder) view.getTag();
// Now use holder.name.getText().toString() and holder.description as you please
You can access the Adapter with position:
Person person = mAdapter.getItem(position);
// Now use person.name and person.description as you please
(By the way in your Person class, name and description are public so you don't need the get methods.)
Override following method in adaterclass.
public String[] getText() {
return text;
}
I have two ListViews (A and B) with items of the same type (a class I created)
When I click on an item from A, it adds this object on B and if I click again it removes it.
Only that when an item is selected, I change its background using view.setBackgroundColor(myColor).
I want to be able to remove the item from list B (it works), but I want also to reset the background color. I can't figure out how to get the view of this item I'm removing.
Any ideas?
There's no guarantee that any specific ListView item will even have a view at any given time. If the item is currently off-screen, then it may not have a view. Since a specific item might not have a view, it might not make any sense to try to get the item's view.
Beyond that, because of the way ListView creates and reuses views, you'll see some odd, undesirable effects if you simply modify the views directly. As the user scrolls through the list, items that become visible will incorrectly end up with the same backgrounds as other items that have fallen outside the visible portion.
I don't know whether what follows is the best way to implement your functionality because I don't know the cost of rebuilding the list after a change. Here's the (probably naive) way I would do this:
Add another boolean member to your data object, something like isInSecondList.
Override getView() in the Adapter. In getView(), set the background to either normal or highlighted depending on the the value of the item's isInSecondList.
When an item is added or removed from the second list, update the data object to reflect the change, then call the Adapter's notifyDataSetChanged().
int position = 0;
listview.setItemChecked(position, true);
View wantedView = adapter.getView(position, null, listview);
Here is what i did
private View oldSelection;
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long arg3) {
highlightSelectdListItem(position);
}
public void highlightSelectdListItem(int position) {
clearPreviousSelection();
View newsItemView = mGridVIew.getChildAt(position);
oldSelection = newsItemView;
newsItemView.setBackgroundColor(Color.GRAY);
}
public void clearPreviousSelection() {
if (oldSelection != null) {
oldSelection.setBackgroundColor(Color.TRANSPARENT);
}
}