I have list view with 78 items, in my activity file I descripe onClick Function realization
lv.setOnItemClickListener(new ListView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Log.d("Click from activity", "win");
if(!checkedContacts.contains(clearContacts.get(arg2))){
checkedContacts.add(clearContacts.get(arg2));
arg1.setBackgroundColor(R.color.selectedItem);
} else {
checkedContacts.remove(clearContacts.get(arg2));
arg1.setBackgroundColor(Color.WHITE);
}
}
});
So when I clicked on item it change color. But if I scroll down my listView, other elements every 10 times will have new color. checkedContacts array have right values but, visualization wrong
The problem is that listviews recycle the views that are out of the screen so this way you are not saving the row status. When you get the row out of the screen it will be deleted and when it comes back again it will be refilled in your adapter again.
You may solve this, for example, using your checkedContacts var and make it accesible from your adapter so, if the position given in getViewfunction of your adapter you can check if the row has been checked and then youn can change the background color. If the user rotate the screen you may save this var in onSaveInstanceState in your activity to recover them later.
Hope it helps :)
Related
my problem:
ring5Views[i]
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,
View arg1, int position, long arg3) {
arg1.setBackgroundColor(Color.parseColor("#444444"));
}
});
if I click on one Item (child) every second item (%2) will change its bg color.
Why ?
The hole thing I want is a list with items and different colors for each but getChild dont solve the prob because NULL every time. And getItem dont help because it only gives back the value (string)
This is how it should work...
for(int i=0; i<oneList;i++)
{
oneList.getChildAt(i).setBackgroundColor(Color.blue);
}
thanks
Extend the ArrayAdapter and override the getView method. And inside the getView method based on the condition you can change the color
you can see this Example for reference
I have a listview and I want to able to select one of the entries here in order to delete it. For example, my entry in listview looks like this:
John Smith
john#hotmail.com
4857394
NewYork
So I want to select this one and delete it. I find this set OnItemClickListener in order to select it. I have delete function. I just need to select name "John Smith" when I click this entry. I don't know what I can write inside function here.
listContent.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id)
{
listContent.getItemAtPosition(position);
}
});
listContent.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
listContent.getItemAtPosition(position);
listContent.removeViewAt(position);
adapter.notifyDatasetChanged();========> your adapter
}
});
You may remove an item as below:
adapter.remove(listContent.getAdapter().getItem(arg2));
Note: Since you are removing directly from adapter object, so you don't need to worry about calling notifyDataSetChanged() method for the changes to take effect.
On ListView you are providing data through some collection of data and index of individual data in list will be same as it is shown in screen. So onItemClick method you are also getting int arg2 , which signifies selected list. Same index you can use to delete element from List and write this deletion operations in runOnUIThread method so that you will not find any glitches.
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);
}
}
I have a ListActivity with a ListView populated onStart with data from a database. Each row is a custom view. After the ListView is populated I want to select a specific row and edit it (for example change the background).
If I try getListView().getChildCount() after it is populated I always get 0. Am I misunderstanding how getChildCount works or have I used it incorrectly?
I'm not exactly sure what you are trying to do, but if you want to select a row (click a row) and change the background color, this could work:
getListView().setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
arg1.setBackgroundColor(Color.parseColor("#444444"));
}});
If you are trying to do programmatically on population, you should override your list adapter and do it in there.
I have an Android ListView created with a SimpleAdapter that has more items in it than fit in the screen. After the list has been scrolled, I need to get the position in the data model of the first visible item in the list.
Basically I want a function like: listView.getChildAt(0).getPositionInDataModel().
Adapter has a few functions in it, like getItemId(position) that looked useful; however, the SimpleAdapter implementation just returns the passed in position, not a row id like I'd hoped.
A brute force solution would be to get the View at index 0, and compare it to the view for each item in the adapter. However, there doesn't seem to be an easy way to get the view for a particular position from the adapter.
Anyone have any thoughts?
It's very easy. Just use ListView.getFirstVisiblePosition() + indexYouWant. For instance, to get the position in the adapter of the 2nd child displayed in the ListView, just use getFirstVisiblePosition() + 1.
No need for all the scary stuff shown in the reply above :)
listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent,View view, int pos, long id)
{
AisleId= parent.getSelectedItemId();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
In this we will get list item Id parent.getSelectedItemId();
Simply use the getPositionForView(View) (see documentation). The main advantage of this method is it also works with descendant Views of an item.