guys what's up?
I'm still new to android and creating some testing app.
I have two Lists in my view. (Left and Right).
List Filled with values.
I trying to add selected list item value to other lists.
For example, If I clicked on an item in a left list. I wanna add selected item to Right list,
implemented OnItemClickListener into my code and OnItemClickListener onItemClick method is firing for both lists.
Any way to identify clicked list in an onItemClick method?
any suggestions are greatly appreciated.
After reading official API. Found a solution.
We can get the ID on the selected list.
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if(adapterView.getId() == R.id.leftList){
// It's a left list
}else{
// It's a Right list
}
}
I'm not sure if I understand your question, but if you are trying to identify which list was clicked, you can call getId on the view in onItemCLick:
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
switch (parent.getId()){
case R.id.left_list:
...
break;
case R.id.right_list:
...
break;
}
}
Related
I have a ListView where I store a list of order.
When clicking an item, it should expand to let the user see the detail.
The problem is that the item is actually expanding, but so is another item randomly in the list.
If I scroll the list while one item is open, the first item collapse and another take his place. The second expanded item is NEVER in sight, I have to scroll to see it.
I don't understand anything to this bug, can you help me?
Here is the listener that interract with the listview
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
View v = CommandesSalarieActivity.this.list.getChildAt(position);
LinearLayout ll = view.findViewById(R.id.commande_salarie_list_item_layout_main_layout);
ImageView iv = view.findViewById(R.id.commande_salarie_list_item_layout_dropdown);
if (ll.getVisibility() == View.VISIBLE) {
ll.setVisibility(View.GONE);
iv.setImageResource(R.drawable.ic_dropdown_ressource);
} else {
ll.setVisibility(View.VISIBLE);
iv.setImageResource(R.drawable.ic_dropup_ressource);
}
}
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);
}
}
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
}
});