How can I set a long-clicked item as selected?
This does not work (item does not maintain selected when clicked):
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
listView.setSelection(position);
return true;
}
});
If you're trying to use a multi-select list, make sure you flag it as such:
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
If it's a single-select list, you still need to flag it as a "choice mode" list, as opposed to one where clicking just performs an action, not selects the item.
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
And since you've got the view you want to select being passed in as a parameter, you don't need to set by position, you can set by the view instead, like so:
view.setSelected(true);
Try using
view.setSelected(true);
If the background of your listview items has selected state - this will change the background to this selected state.
Related
In my app, I have a listView whose item is choice mode is singlechoice. When I click an item, its background color gets dark. When I click it again, it is still with dark background. I want it to come back to the previous background color after I click again.
Now my solution is to use the myList.setSelector(new ColorDrawable(Color.TRANSPARENT)) function every time I click the item.
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
myList.setSelector(new ColorDrawable(getResources().getColor(R.color.gray_white)));
if (position==prePosition){
if(isSelected==UNSELECTED){
isSelected=SELECTED;
if (position>lock_listItems.size())
floatingActionButton.setVisibility(View.VISIBLE);
}
else{
isSelected=UNSELECTED;
floatingActionButton.setVisibility(View.INVISIBLE);
myList.setItemChecked(position,false);
//view.setBackgroundColor(getResources().getColor(R.color.white));
myList.setSelector(new ColorDrawable(Color.TRANSPARENT));
}
}
else {
isSelected=SELECTED;
if (position>lock_listItems.size())
floatingActionButton.setVisibility(View.VISIBLE);
else
floatingActionButton.setVisibility(View.INVISIBLE);
prePosition=position;
}
myList.forceLayout();
}
I think this solution is not so effective. Any better solution?
Take an arraylist.
First check the clicked position is present in the arraylist or not by using
Collections.contains(position);
If it returns true, don't do anything. Otherwise do your stuff. You can do as below:
ArrayList<Integer> myPositionClickedList = new ArrayList<Integer>(); // this should be as global variable
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(!myPositionClickedList.contains(position)) {
myList.setSelector(new ColorDrawable(getResources().getColor(R.color.gray_white)));
//do your stuff here........
myPositionClickedList.add(position);
}
}
For example I have Custom Adapter List View, and my list item is a layout that contains different elements: textview, imageView and so on. How can I set the color of imageview of selected list item? Suppose I want to add this item in favourite and I want to change the color of star favourite for yellow color. Thank you)
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getActivity(), PlaceName[position] + " in favourite",
Toast.LENGTH_SHORT).show();
//Do smth here, set the color of element on item, add to favourite and something else
return true;
}
});
Well, you have this line:
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
https://developer.android.com/reference/android/widget/AdapterView.OnItemLongClickListener.html
According to the docs, view is the layout that you actually clicked, so you can get the child of that View using something like this:
ImageView favorite = view.findViewById(R.id.yourFavoriteImageView);
Note that if you scroll through your list, the layout might render again, and your change won't be visible again.
view.setBackgroundColor(Color.parseColor("#222222"));
I am trying to make a ListView where you can have no more than 4 items clicked at once.
These items have to be adjacent to each other.
When I click my first item, I want to see if the previous OR the following item is clicked already.
Note: When I click an item, I change the background color. So if I want to tell if an item is clicked or not, I just want to check the background color.
public void OnItemClick(AdapterView<?> parent, View, view, int position, long id){
View currentItem = view;
currentItem.setBackgroundResource(R.drawable.li_gradient);
// How do I get the view in front of and behind currentItem
// to check their current background color? (Assuming they exist)
}
You should do this inside your adapter class, the model item need to have a background resource property.
Just cast the adapter, get the previous or next item, set the new background and notify the listView
public void OnItemClick(AdapterView<?> parent, View, view, int position, long id){
YourAdapterClass adapter = (YourAdapterClass) parent.getAdapter();
// TODO check if is a valid position
YourItem item = (YourItem) adapter.getItem(position-1);
item.setBackgroundResource(R.drawable.li_gradient);
adapter.notifyDataSetChanged();
}
Keep in mind that if the activity is recreated (because of configuration change for example) or if the list view is redrawn (for example when it is notified of a change) it will not automatically know about the changes made to the view in the onClick method and the background changes could be lost.
I would make a custom AdapterView with methods like itemSelected(int i), clearAllSelected(), getSelectedItems(), etc and make the adapterview the single point of responsibility for tracking and maintaining those changes and for rendering the background.
I'm now implementing an application that contains an Activity includes ListView, when the user selects any item in the list view, the background & text color of this view are changed, So, i placed the code of this changes in the onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3) method ..but,this is not the problem.
The problem is, when i open the activity, i need to make an initial selection "before the user selects any thing"..so i made listView.setSelection(index);, but, unfortunately, this code doesn't invoke the onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)..So, the view doesn't changed "background and text color".
Any solution for that..?!!!
Thanks,
try onItemSelected for selection
selecting a iem in list view does not fires onItemClick
Create a method in yout Adapter to set the selection (or do it in the constructor):
public myAdapter (COntext context, int initialSelectedPos){
setSelectedPos(initialSelectedPos);
}
public void setSelectedPos(int pos){
mSelectedPos = pos;
}
Then check in your getView is given pos is the same than mSelectedPos.
#Override
public View getView(..., int pos){
/*convertView stuff*/
if (pos == mSelectedPos){
//Put the background as it is selected
}else{
//...
}
return view;
}
In your OnItemClick method from your OnItemClickListener call the setSelectedPos method of your Adapter.
You will solve the issue you commented and also when your selected view is no longer visible on screen and comes back to screen, will still be marked as selected ( I am pretty sure it was appearing with the original background).
after setting selection
listView.setSelection(index);
and then after call the
listView.getAdapter().notifyDatasetChanged();
i think this will solve your problem
I have an AutoCompleteTextView. I set its adapter by extending a CursorAdapter.
After selecting an option from the dropdown, I like to know the position of the item, or the item, that was selected. At least, I want an id for fetching more data in Sqlite.
How can I do that?
Simply override the AutoCompleteTextView's OnItemClickedListener to find the position of the item in the dropdown list or the SQLite row id:
autoCompleteTextView.setOnItemClickedListener(new OnItemSelectedListener() {
#Override
public void onItemClicked(AdapterView<?> parent, View view, int position, long id) {
// Do whatever you want with the position or id
}
});