I am modifying an adapter view. How do I implement the setSelection() of an AdapterView? What are the steps one must take when setSelection() method of an adapterview is called?
I tried browsing through ListView's source code, but it wasn't of much help.
Save the selected position and pass it to the BaseAdapter class. (You have to implement a custom Base Adapter class).
Then in getView() method, change as per your requirement by checking position==selectedPosition. (Note: you should call notifyDatasetChange() method to invoke getView() method again).
try List view onItemClick()
listView.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v, int position,long id) {
}
});
According to this and this you have to save selected item somewhere, show user that this item is selected(if you are not in touch mode), and you need to scroll view to this item.
Android-HorizontalListView is having HorizontalListView, which is an Android ListView widget which scrolls in a horizontal manner. In this class it's describes how to implement the setSelection() of an AdapterView.
Related
Good day. I have an android application which displays a ListView. Upon clicking the any of the rows, I want all other rows to be grayed out. I am using a custom ListView with my own BaseAdapter and I know I can create an onclick function on the getView function there. However, doing so invalidates the onItemClick function I have in my base activity that's why I want to do this in my base activity as much as possible.
Here is how I made my onItemClick function:
resultListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long id) {
// TODO Auto-generated method stub
resultListView.smoothScrollToPositionFromTop(position, 0);
resultListView.setClickable(false);
resultListView.setScrollContainer(false);
resultListView.setEnabled(false);
};
});
}
Is there a way to gray out all the other items except for the chosen item? Even changing the alpha/lowering it would help. I want to give the user an impression that the other list items are not clickable. I'm currently having a tough time figuring it out. Any help is very much appreciated, thank you.
EDIT
I am able to adjust the alpha by doing:
resultListView.setAlpha((float) 0.5);
However, it affects the whole row and I am unable to adjust the alpha for the selected row.
Create class to hold variables. Pass this class to the adapter instead of string for example. Create a boolean variable isSelected in your class. Set isSelected in onItemClick(), and enable/disable the views from getView() depending on your isSelected variable.
I'm trying to change one TextView in any view in my ListView,
And I already got one Adapter set so I can't use adapters for this.
I tried to do it in a "for" statement, using
mListView.getChildAt(i)
but it didn't worked.
when I tried to do it inside the OnItemClick method, using arg1 instead of
mListView.getChildAt(i), it worked for the item I clicked.
But i need it to happen without clicking on an item,
So my question is: What is the value of Arg1 and Arg2?
I know what they represent, but I want to know what value do they get when I click on an item.
Thanks.
you can fake a click through view.performClick() method if that is what you need
From Android documentation :
public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id)
Callback method to be invoked when an item in this AdapterView has been clicked.
Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item.
Parameters :
parent The AdapterView where the click happened.
view The view within the AdapterView that was clicked (this will be a view provided by the adapter)
position The position of the view in the adapter.
id The row id of the item that was clicked.
I implemented OnScrollListener to paging the listview!
The paging is ok! i scrolled the list and after i selected the item on listview onListItemClick didn't work!
Here is my onListItemClick and i extended ListActivity.
I was just scanning through your code and I can't tell which class you're extending either ListActivity or ListFragment, so you can provide a more complete listing?
In the meantime, if you're extending ListView you should check out this article that shows how to set you onClick method:
ListView with onItemClickListener
Otherwise, if you're extending ListFragment, then you can override this method to provide whatever behavior your looking for when a list item gets clicked:
#Override
public void onListItemClick(ListView listView, View clickedView, int position, long id)
An example of what I'm talking about can be found here:
ListFragment docs
Hope that gets you where you need to be.
David
I am trying to set item selected in OnItemClick event in ListView and it just wouldn't leave item selected. What am I doing wrong?
lView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(#SuppressWarnings("rawtypes") AdapterView parent, View clickedview, int position, long id)
{
clickedview.setSelected(true);
mItemsAdapter.select(position);
}
});
few things:
1. I am trying to implement Multiple Select on the list View.
2. I cannot extend from ListActivity because Activity extends from BaseActivity custom class already.
3. mItemsAdapter is a custom ItemsAdapter adapter that extends BaseAdapter.
4. I don't need a checkbox in there, just to be able to see the row selected is fine.
5. ItemsAdapter overrides getView() and sets the layout of the row by inflating xml
I could manage to get an item of a ListView to be set as selected when long-clicked:
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
parent.requestFocusFromTouch(); // IMPORTANT!
parent.setSelection(position);
return true;
}
It only worked after I called requestFocusFromTouch().
I currently don't have much time. So I'll take a look again later this day.
Anyway take a look at my previous questions, I was struggling with the same:
Change ListView background - strange behaviour
Clickable ListView
In case the solution for you is not in there (I think it's in the first one) we will need more code, in order to help you.
Hope this helps a bit.
I have not had much luck using the built in functionality for selection.
I see you have your own custom adapter, which I am guessing means your inflating custom views as rows. If your row has anything more then a checkedtextview I don't think you will be able to correctly use setSelections.
I solved this problem by using my own models and functions. Each item in the list had data with it to determine it if was selected or not. I could then iterate though that array, toggle selections with and and even update the UI by changing values and calling notifydatasetchanged on the adapter (which used getView and checked against my selection model to draw checks).
I am using a ListView with a SimpleAdapter. Once data is populated into it I would like to set the first item in the list. This can be done by clicking on the list item on the screen. I want to just call the method directly after I populate the list so that when you see the UI its already done. My only problem is getting the View from the ListView. I noticed its children are all null but the SimpleAdapter has items in it. When I try to get those items they are not Views and I am not able to match the method call of
protected void onListItemClick(ListView l, View v, int position, long id)
because I cant get the right View. Any help would be appreciated.
After lots of searching the solution is not to make a manual call but to override the adapter getView method. That way I can change the background color and not have to manually call onListItemClick()