I had a requirement in android spinner where I need to display the selected item as first element (which is element in zeroth position ) irrespective of item selected.Suppose I selected element in first position , then also zeroth position element only should be selected.How to make zeroth element to be selected irrespective of any item selected in spinner.
In onItemselected() method I have the item selected as zero , but still the clicked element is selected for as fraction of second.
Can anyone help me in sorting out this issue.
ok, then set it 0 at onItemSelected()...
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
mSpinner.setSelection(0);
}
Related
I am populating a listview with strings from an ArrayList.
When a listview item is clicked i would like to change the background colour to green. <- Issue number one because I cannot change the item which is clicked.
After an item is clicked, I am adding its index to the list of items the user has selected, when the listview is first loaded I need it to set the background colour of all the listview items which have been selected to green too. <- Issue number 2 - I have been trying to do this with a for loop but do not know how to refer to a specific item in the listview to set the background colour!
Essentially, i think if someone can help me in how to change the colour of a selected listview item, i should be able to do the same thing but in a loop for all the userFoodPref which are saved?
animalsNameList = new ArrayList<String>();
userFoodPref = new ArrayList<Integer>();
getUserSelection();
getAnimalNames();
// Create The Adapter with passing ArrayList as 3rd parameter
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, animalsNameList);
// Set The Adapter
animalList.setAdapter(arrayAdapter);
// register onClickListener to handle click events on each item
animalList.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
// argument position gives the index of item which is clicked
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if(userFoodPref.contains(i)
){
userFoodPref.remove(i);}
else {
userFoodPref.add(i);
View item = animalList.getSelectedView();
item.setBackgroundColor(0x0000FF00);
}
String selectedAnimal=animalsNameList.get(i);
Toast.makeText(getApplicationContext(), "Animal Selected : "+selectedAnimal, Toast.LENGTH_LONG).show();
}
});
}
If I understand well, the problem is that when you set the backround of the item and then by example scroll the list and comeback to the previous position, it doens't remember that the backround is green for this specifix item.
I have faced this problem and to solve it easily :
Create a list a string for your name and a boolean (true = green, false = not green) and create an adapter for it and simply add
if (list.get(position).getBoolean) {
Currentitem.setBackgroundColor(0x0000FF00)}
And when you click on a item simply set the boolean of the item position to true and call notifydatasetchanged()
I am working in android to develop an app which contains a list view in which the background colour of the particular item is changed when the item is clicked ( which is already performed using "ListSelector" in .xml file). Now I need to deselect the item which I done through " mListview.clearChoices()". But background colour of the previously selected item is not changed.
Can you guys give some solution ?
Here is my xml code:
<ListView
android:id="#+id/listView"
android:layout_width="#dimen/dp_130"
android:layout_height="wrap_content"
android:background="#color/malibu"
android:gravity="center"
android:minHeight="#dimen/dp_50"
android:listSelector="#e60b62da"/>
Here is the code in Activity file:
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
check = 1;
item_position=position;
Toast.makeText(MacroActivity.this, "Item clicked_ macro" + position, Toast.LENGTH_SHORT).show();
item_value = (String) mListView.getItemAtPosition(position);
Toast.makeText(MacroActivity.this, "Item clicked_ macro_value" + item_value, Toast.LENGTH_SHORT).show();
mListView.clearChoices();
}
});
When you select a list item view (into onItemClick method) you can set a variable in your adapter to keep selected list item index. Then you can call notifyDatasetChange(). In your adapter where you create and set your list item views properties you can check if selected index correspond with current selected view. In that case you can set a colour, otherwise you can set another one.
Hope that helps
This is one of the feasible solution which can be used to solve the above question. I used the listview.setSelector() in which the background colour assigned for the item in the listview can be nullified, yet this function does not allow to set background colour for another item in the listview if needed so.
mListView.setSelector(new ColorDrawable(0));
I have list of items in gridview. On selecting item, position of the selected item should change to 0. Every time i choose item , it should move to first position.
How can i do this?
got it
temp = mFilteredBrandsList.get(position);
mFilteredBrandsList.remove(position);
mFilteredBrandsList.add(0, temp);
notifyDataSetChanged();
Try this inside your onItemClickListener.
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//assuming your grid is a list of string elements
String item= (String)parent.getItemAtPosition(position);
mList.remove(position);
mList.add(0,item);
notifyDataSetChanged();
}
Assuming mList is the main list consisting of elements inflated in grid's row.
If this isn't clear enough. SHare your code will modify the same.
In my android application i'm having a list view and some items in that,now my requirement is when I select a particular item from that list the list item should be highlighted with a red color, and that selection should not be disappear because in my application i should choose an item from the list view and i should click a button(submit).
So in order to know to the user that he has selected an item from the list view it should be highlighted until he clicks submit button.
I have used choice Mode attribute and set the value to single choice and i have changed the color for highlighting everything works fine but i need it not to be disappeared until user clicks the submit button
The variable val consists of some names which i retrieved from database.
Please help me to solve this Thanks in advance.
ArrayAdapter<String> adptr= new ArrayAdapter<String>(this,R.layout.row,R.id.member_name,array);
lv.setAdapter(adptr);
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v, int pos,
long id) {
// TODO Auto-generated method stub
val=lv.getItemAtPosition(pos).toString();
//Toast.makeText(getApplicationContext(), val, 5000).show();
}
});
use OnItemClickListener
ListView lview = getListView();
int pos =0;
lview.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
// set the background of v here
if (position == pos)
// reset the background color
// and pos = position;
else
get the previous listitem view and reset it's background to original
}
});
Actually I had the same problem sometime back. I was working on non touch andorid ui application for google tv.
The solution is like this.
1) create a custom ArrayAdapter (extending ArrayAdapter)
2) onItemClick get the position of the item.
3) send that position to your adapter by some public method say setCurrentPosition(int pos)
4) in getView() of the adapter check for the position and set the background to red for that view.
Hope this will work as it worked for me.
I have a custom listview. I have to select some items from this listview and display it in the next layout listview when a button click event is clicked.
lv5=(ListView)findViewById(R.id.ListView05);
lv5.setAdapter(new ArrayAdapter<String>(this,R.layout.productselecttext,R.id.pstext,arr));
lv5.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(view.findViewById(R.id.oi).getVisibility()==ImageView.VISIBLE){
ImageView icon = (ImageView)view.findViewById(R.id.oi);
icon.setImageResource(R.drawable.vi);
}
}
});
In this way I have selected using an imageview. How can I get only the selected items and display in another listview?
It all depends on what you call a selected item. We need some more code to understand how you "selected" items in the first listview.
But generally speaking, all you have to do is to build a new Adapter for the second list view that just lists the selected item. You could even recylcle the first adapter and the first view, just eliminating the items that are not selected from the adapter's list and calling
notifyDatasetChanged()
on your first adapter to update your first list.
Regards,
stéphane