ListView Clicked Item Highlight not working properly - android

I have a ListView and have a onClickListener for this ListView.
ListView is using the SimpleAdapter, and populating the Data via a map.
Whenever Item is clicked, that List Item is getting highlighted.
But the problem is, Suppose I click the 4th Item on the List and scroll the list, then every 4th element is highlighted. I'm using a Selector xml to implement the highlight functionality
I know the problem is due to the screen refresh.
But how can I avoid this problem?
Searching for the answers from about a month now. I need a solution now.
Thanks in advance.
Selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/drawer_icon_normal" android:state_selected="false" android:state_pressed="false"/>
<item android:drawable="#drawable/drawer_icon_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/drawer_icon_activated" android:state_selected="true"/>
</selector>
Below is the code for populating the listView and onclick functionality.
//Note this code is not using the selector, how with this code also, functionality is same.
public void populateListView() {
//Populate the List View Here
//Set the adapter
SimpleAdapter flvadapter = new SimpleAdapter(getActivity(), finalSongList, android.R.layout.simple_list_item_1, new String[]{"filename"}, new int[]{android.R.id.text1});
fileListView.setAdapter(flvadapter);
fileListView.setBackgroundColor(Color.WHITE);
fileListView.setTextFilterEnabled(true);
fileListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
//Registering the OnItemClicklistener
fileListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
//TODO Auto-generated method stub
//fileListView.getItemAtPosition(position);
updateView(position);
index = position;
#SuppressWarnings("unchecked")
HashMap<String, String> hm = (HashMap<String, String>) fileListView.getItemAtPosition(position);
if (hm.get("fileuri") == null) {
AlertDialog.Builder adb = new AlertDialog.Builder(getActivity());
adb.setTitle("ListView OnClick");
adb.setMessage("Selected Item is = " + hm.get("fileuri"));
adb.setPositiveButton("Ok", null);
adb.show();
} else {
//Processing the Selected File
}
}
});
}
private void updateView(int index){
//View v=fileListView.getChildAt(index-fileListView.getFirstVisiblePosition());
selectedListViewItem=fileListView.getChildAt(index);
fileListView.setBackgroundColor(Color.WHITE);
if (selectedListViewItem != null) {
selectedListViewItem.setBackgroundColor(Color.CYAN);
}
}

In your updateView() function, you are changing the selectedListViewItem's background color. up to this point everything works like you would assume however when you scroll, every so ofter another row will already be "selected" even though you didn't do it yourself.
That is caused by view recycling, a function of Listview adapters that allow them to load faster by taking a row that has been scrolled out of the screen, fill it with new data and present it as is (without needing to inflate it from XML again.
Basically it will take your old blue row that is not on the screen anymore, put a new string in and display it as the new row.
The way you fix it is by implementing your own Adapter and overriding the getView method. there are a ton of resources that will show you how to do that. Here is one
One thing to keep in mind, once you implement your custom Adapter is that you will have to keep track of which items have been clicked so you can un-highlight and re-highlight items correctly.

you are changing the background color of the selected item but not reset it in the adapter
try to post your adapter code
so check in adapter getView method
if item is selected
setBackgroudColor for selected row
else
setBackgroundColor for normal row

Related

Changing background colour of listview item on click - and remembering it

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()

change the background color of a listview item when clicked

my app contain a ListView and when a user click on one of the items, the selected item color change to a different color. i acomplished this by adding this android:listSelector="#android:color/darker_gray" in my ListView located in my activity_main.xml file. This is precisely what i am looking for, but im having issues with it because the background color also changes when the user hold the item but dont click it. so i was wondering if the was a very extremely simple way i could put this code:android:listSelector="#android:color/darker_gray" inside my OnItemClick method. that way when an item from my listview is clicked, i want the selected item background to change. also i do NOT want to rearrange my listview and put the selected items on top, i just want all the items that the user have clicked to have a different background color which is why i want to put the coding inside onItemClick. Currently i am using the default sample_list_item_1 for my ListView.
Additional Info about App: This is kinda like a tutorial app where the user is presented with a list of tutorial names. when the user clicks on a names it loads a stream-able link so the user can just stream it inside the app (this is already done). i have also added a toast message that shows when the user clicks on the stream-able links (this is kinda like what i want eccept instead of a toast message, it changes the background color of the selected item to darker_grey). i was able to do all this inside if-statements. for example:
Inside OnCreate()
String[] Tutorial = getResources().getStringArray(R.array.Tutorial);
spinner1 = (ListView) findViewById(R.id.spinner1);
spinner1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Tutorial));
spinner1.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int pos, long id)
{
if(pos==0)
{
// Do nothing
}
else if(pos==1)
{
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.parse("https://download846.mediafire.com/8cfxi4026iog/x4ywqe8dc8u2r4f/Hetalia+Season+1+Episode+1.mp4"); /* this is a random link to show you that my programing so far works(Enter link to the selected tutorial here)*/
i.setDataAndType(uri, "video/*");
try {
startActivity(i);
} catch (ActivityNotFoundException e) {
// Handle exception
} catch (Exception e) {
// Handle exception
}
Toast.makeText(parent.getContext(), parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();
// android:listSelector="#android:color/darker_gray"
}
}
}
if you still dont understand what i meant, there is an app in the googlePlay store called Anime Plus TV that does something similar to what i meant. in the app, when you click on the episode of a serie (example: naruto) it loads the episode's stream-able link, and also change the text color of that selected episode so that when the user finishes watching the episode, he wont be confused about which episode he was watching since some stream-able links doesnt realy give you the real title of the video you where watching. but unlike them i just want the background of the selected Tutorial to change
You have two options doing that .
1.progamatically:
In this case when you set the OnItemClickListener for your ListView , get the child and setBackground for it .
String[] inputs = {"test1","test2","test3"};
final ListView l = (ListView)findViewById(R.id.listview);
l.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 ,inputs));
l.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id) {
l.getChildAt(position).setBackgroundColor(android.R.color.black);
}
});
You can define colors in color.xml file under folder res/values ( create it if you dont have it ) and use them like R.color.gray (example) instead of android.R.color.black .
2.Using Custom listSelector
under folder drawable create the listselector.xml . In listselector you can handle different states just like this :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selector style for listrow -->
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="#drawable/test1" />
<item android:state_pressed="true"
android:drawable="#drawable/test2" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/test3" />
</selector>
for your case i think using first way is the best . ask if you had any other questions :)

ListView elements are not visible by default

Below is my code and image. I am populating the listview with the arraylist in runOnUIThread() in onpostexecute() which has values gotten from the remote server in the doInBackground(). But the thing is elements are visible only when the focus is on particular item. I have been trying with different things to set the elements visible, but all have gone vain. Can someone please suggest me how to get the items visible. Note: I can't extend the ListActivity, as I have another class that needs to be extended which is the subclass of an activity.
runOnUiThread(new Runnable() {
public void run() {
//Updating parsed json data to Listview
ListView listView = (ListView)findViewById(R.id.listsubcategory);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1, subCategoryList);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String selectedSubcategory = subCategoryList.get(position);
Toast.makeText(getApplicationContext(), "You clicked on "+selectedSubcategory, Toast.LENGTH_SHORT).show();
}
});
}
});
The problem is the styling of the list items. In normal state your items have white background and white text color, therefore you can't see them. When the state changes to focused the colors change. You can easily solve the problem by using your custom list item instead of the system's android.R.layout.simple_list_item_1.
Define a layout for the item, it can be something like this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/my_item_background"
android:textColor="#color/my_text_color"
/>
Now, if the item's layout is res/layout/my_list_item.xml, create the adapter this way:
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getContext(),
R.layout.my_list_item, subCategoryList);

Android/ListView : Select first item after loading datas

EDIT
I must tell you that my ListView is populate by an AsyncTask.
The code below works fine when I do in onPostExecute method :
synchronized (mListView) {
if(mFeeds==null || mFeeds.size()==0){
Utils.Log("mFeeds empty");
_this.setListShown(false);
}else{
Utils.Log("mFeeds Full");
_this.setListShown(true);
mListView.setAdapter(new ListFeedsAdapter(mActivity,mFeeds));
mListView.notifyAll();
NewsFeedsDetailViewPagerFragment fragment = (NewsFeedsDetailViewPagerFragment) getFragmentManager()
.findFragmentById(R.id.feeddetailViewPagerFragment);
if(fragment!=null){
mListView.performItemClick(null, 0, mListView.getFirstVisiblePosition());
}
}
}
The item is clicked and my detail view is populate...
I try to change my Fragment to a ListFragment but nothing changed...
EDIT END
For my application, I created an UI (for tablet) with ListView at left screen and a detail view at the right.
I would like to automatically select the first item after loading datas, and view the detail.
I am able to do this by calling mListView.performItemClick(null, 0, mListView.getFirstVisiblePosition()); and
mListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
NewsFeedsDetailViewPagerFragment fragment = (NewsFeedsDetailViewPagerFragment)
getFragmentManager().findFragmentById(R.id.feeddetailViewPagerFragment);
if(fragment!=null){
fragment.setFeedDetail(mFeeds, arg2);
}
});
Now, what I want to do is to highlight the first item like Gmail application for tablet.
I use a selector background on each row like :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/selected_item_background" android:state_activated="true"/>
<item android:drawable="#drawable/pressed_item_background" android:state_activated="false" android:state_pressed="true"/>
<item android:drawable="#drawable/pressed_item_background" android:state_focused="true"/>
<item android:drawable="#drawable/unselected_item_background"/>
</selector>
When I use mListView.getChildAt(arg2).setActivated(true); in the onItemClick listener, I have a NullPointerException because mListView.getChildAt(arg2) is null, only if I keep the perforItemClick. If I just comment this line, and click on a row, this row is highlight as in Gmail application.
Can you help me and tell me what I'm doing wrong ?
Thank in advance
I found a solution to my problem and I post here the solution if there is a developer that is in the same situation.
I created a method in my adapter that flag wich position must be activated and in my getView method, I just compare the current position to the flag and activate or not the row :
((ListFeedsAdapter) mListView.getAdapter()).setItemSelected(mOldPosition);
And in the adapter :
private int mItemSelected = -1 ;
public void setItemSelected(int position){
mItemSelected=position;
}
public View getView(int position, View convertView, ViewGroup parent){
//some code ....
if(mItemSelected==position){
convertView.setActivated(true);
}else{
convertView.setActivated(false);
}
}
Thank you !
I think it's because you passed null in the performItemClick() method:
mListView.performItemClick(null, 0, mListView.getFirstVisiblePosition());
Try something like:
mListView.performItemClick(mListView.getChildAt(0), 0, mListView.getFirstVisiblePosition());
did you tried setSelection()?
mListView.setSelection(0)
I know answer is accepted, but if this helps someone.
If you want to select/play the very first view of list view automatically as list loads, then you should visit this link. I was looking for the same thing and found post() method which waits till your list is filled.
Here is my question link :-Accessing list's first item when list view is loaded in android
Here is its solution link :-smoothScrollToPosition after notifyDataSetChanged not working in android
However writing any code in getView() will cost you as getView is called repeatedly.
Thanks.

How to highlight row in ListView in Android?

I need to highlight a row in a ListView that was selected (to show the user what he chose), so, it's not the one that is going to be chosen, it's the one he chose before.
I already have the location by:
ListView.setSelection(position);
And now what I want is to select this specific row and to highlight it.
The code of the onCreate() in the activity that contains the ListView:
public class CountryView extends Activity
{
protected static final String LOG_TAG = null;
/** Called when the activity is first created. */
String[] lv_arr = {};
ListAdapter adapter;
TextView t;
private ListView lvUsers;
private ArrayList<Coun> mListUsers;
String responce=null;
public int d;
int selectedListItem = -1;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.country);
Intent data =getIntent();
mListUsers = getCoun();
lvUsers = (ListView) findViewById(R.id.counlistView);
lvUsers.setAdapter(new ListAdapter(this, R.id.counlistView, mListUsers));
selectedListItem=data.getExtras().getInt("PositionInList");
lvUsers.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lvUsers.setOnItemClickListener(new OnItemClickListener()
{
int positionItem;
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
Intent pongIntent = new Intent(getApplicationContext(),Trav.class);
int counId=mListUsers.get(position).id;
pongIntent.putExtra("response",mListUsers.get(position).p);
pongIntent.putExtra("responseCounID",counId);
//Put the position of the choose list inside extra
positionItem=position;
pongIntent.putExtra("PositionInListSet",positionItem);
setResult(Activity.RESULT_OK,pongIntent);
Log.i("CounID *******************************"," "+counId);
finish();
}
});
}
}
Since by default ListViews are set to a selection mode of NONE, in touch mode the setSelection method won't have visual effect.
For keeping the previous selection / visually display an explicit selection, first you must set your listview's choice mode appropriately:
listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
It's useful to read the API Docs of these methods:
setSelection
void android.widget.AdapterView.setSelection(int position)
Sets the currently selected item. To
support accessibility subclasses that
override this method must invoke the
overriden super method first.
Parameters:
position Index (starting at 0) of the data item to be selected.
setChoiceMode
void android.widget.ListView.setChoiceMode(int choiceMode)
Defines the choice behavior for the
List. By default, Lists do not have
any choice behavior
(CHOICE_MODE_NONE). By setting the
choiceMode to CHOICE_MODE_SINGLE, the
List allows up to one item to be in a
chosen state. By setting the
choiceMode to CHOICE_MODE_MULTIPLE,
the list allows any number of items to
be chosen.
Parameters: choiceMode One of CHOICE_MODE_NONE,
CHOICE_MODE_SINGLE, or CHOICE_MODE_MULTIPLE
In case this is not enough (say you'd like to always show the last selection differently beside the current selection), you should store your last selected item (a data which populates the ListAdapter) as lastSelectedItem, and in your adapter's getView method assign a different background resource to the renderer if it equals this lastSelectedItem.
If your last selection wouldn't refresh on selection change, you should explicitly call the notifyDataSetChanged method on your adapter instance.
Update
Since your activity containing the ListView is a child of an activity which waits for this one's result (based on the setResult(Activity.RESULT_OK,pongIntent); part), the initial idea is correct, the last position should be passed through the intent when starting the activity:
selectedListItem = getIntent().getIntExtra("PositionInList", -1);
lvUsers.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lvUsers.setSelection(selectedListItem);
The ListView.CHOICE_MODE_SINGLE solution would work if you remain in the same activity, but you are finishing it on every itemClick (selection change), that's why the extra data should be passed to the starting Intent.
You can also set the previously selected item's background from your adapter -as mentioned above-, overriding its getView method:
lvUsers.setAdapter(new ArrayAdapter(this, R.id.counlistView, groups)
{
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
final View renderer = super.getView(position, convertView, parent);
if (position == selectedListItem)
{
//TODO: set the proper selection color here:
renderer.setBackgroundResource(android.R.color.darker_gray);
}
return renderer;
}
});
Just:
Set the correct choice mode in your list view. setChoiceMode
Set a background to support the selection state in you item layout, like:
android:background="?android:attr/activatedBackgroundIndicator"
FYI:
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
http://android-developers.blogspot.mx/2008/12/touch-mode.html
It's much easier to implement this in your layout files and let Android handle the rest...
1) Make sure you have android:choiceMode="" set on your ListView layout (singleChoice, multipleChoice, etc). By default it is set to none.
<ListView
android:id="#+id/invite_friends_fragment_contacts_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="multipleChoice"/> <!-- THIS LINE -->
2) Create a state selector XML file and save it in your drawables folder. In this example, we'll name it state_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:color/blue" android:state_selected="true"/>
<item android:drawable="#android:color/blue" android:state_activated="true"/>
<item android:drawable="#android:color/transparent"/>
</selector>
3) In your list item layout, add the state_selector.xml file as the background:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/state_selector"> <!-- THIS LINE -->
<!-- ALL OF YOUR ELEMENTS WILL GO HERE (TextViews, ImageViews, etc) -->
</RelativeLayout>
If you are using multipleChoice, you can override onItemClick()and set/unset selected items accordingly. Android will change the background color as specified in your state_selector.xml file.
I had a problem finding an easy solution to this because so many tutorials and answers contained information on single select via radio buttons (Which relied heavily on RadioGroup's).
My problem was that I could set the item to my "Highlighted" state but then couldn't reset the list when the next item was selected. Here is what I came up with:
listView.setOnItemClickListener(new ItemHighlighterListener ());
With this class:
private class ItemHighlighterListener implements OnItemClickListener{
private View lastSelectedView = null;
public void clearSelection()
{
if(lastSelectedView != null) lastSelectedView.setBackgroundColor(android.R.color.transparent);
}
#Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) {
clearSelection();
lastSelectedView = view;
view.setBackgroundDrawable(view.getContext().getResources().getDrawable(R.drawable.shape_selected_menu_item));
}
}
I solve this problem in the following way:
1. set a lastClickId, when click the item in listView, update the lastClickId to position value, then update the view's background.
After this, when we click one item, this item will be highlighted, but when we scroll the listView(make the item which we selected out of the screen) and scroll back, the highlight is gone, because the method getView() rerun in your adapter, so, we have to do the next thing.
2. in your adapter, change the background in the method getView(), here is the code:
private static int lastClickId = -1;
private OnItemClickListener listener = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if ((lastClickId != -1) && (lastClickId != position)) {
parent.getChildAt(lastClickId).setBackgroundResource(
R.color.grey);
view.setBackgroundResource(R.color.blue);
}
if (lastClickId == -1) {
view.setBackgroundResource(R.color.blue);
}
lastClickId = position;
}
};
public static int getCurrentSelectedItemId() {
return lastClickId;
}
Adapter:
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = mInflater.inflate(R.layout.tweet_list_layout, null);
// set selected item's background to blue
if (position == MainUserFeedFragment.getCurrentSelectedItemId()) {
view.setBackgroundResource(R.color.blue);
}
}
Why dont you store the selections in an array, then pass that array in the constructor of the ListView Array Adapter, something like myArrayAdapter(context,layoutID,dataArray,selectionArray)
then in your getView method for the arrayadapter, just do a check. For example in pseudocode
if row was previously selected
change background color
You can use transition. Follow my code because i have achieved highlighting of specific listview item by choice
Lets say you want to highlight first item for 5 seconds.
if(position == 0){
viewHolderThubnail.relImage.setBackgroundResource(R.drawable.translate);
TransitionDrawable transition = (TransitionDrawable) viewHolderThubnail.relImage.getBackground();
transition.startTransition(1000);
}else{
viewHolderThubnail.relImage.setBackgroundResource(R.color.white);
}
translate.xml
<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<!-- The drawables used here can be solid colors, gradients, shapes, images, etc. -->
<item android:drawable="#drawable/new_state" />
<item android:drawable="#drawable/original_state" />
</transition>
new_state.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#92BCE7"/>
</shape>
original_state.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FFFFFF"/>
</shape>
If you understood this code, which is very simple i must say, then the listview item at zero'th position will highlight in blue color for 5 seconds and then it will slow fade to white color.
The SIMPLEST of all
View updatedview=null;
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//these two lines of code means that only one item can be selected at a time
if(updatedview != null)
updatedview.setBackgroundColor(Color.TRANSPARENT);
updatedview=view;
Toast.makeText(getApplicationContext(), " " + str[position],Toast.LENGTH_LONG).show();
view.setBackgroundColor(Color.CYAN);
}

Categories

Resources