Highlight current touched item as selected item - android

I have a android file explorer application and I need to highlight the selected item. I could select touched items using v.setBackgroundColor() method inside onListItemClick(). But when I touch another file/folder still previous one highlighted. I need only current touched item display as selected. How to do this?

not perfect but works good
loop through all items of list and try something like this
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
for (int i = 0; i < arg0.getCount(); i++) {
View view = arg0.getChildAt(i);
if (i == arg2) {
view.setBackgroundColor(Color.RED);
} else {
view.setBackgroundColor(Color.GREEN);
}
}
}
});

Related

Filtered listview highlighting incorrect item

I have a list view where I am filtering items through a SearchView. On activating the state for an item, it is not getting the correct item instead getting it from the position. To make it more clear, please refer to the below screenshots:
Search for keyword com and selected the filtered item (i.e activated_state)
On clearing the filter, when the position of the items changes it does not keep track of the selected item i.e com.android.gesture.builder:
I want the selection to be correct regardless of the position change.
My code in MainActivity for this section:
apps.setChoiceMode(apps.CHOICE_MODE_MULTIPLE_MODAL);
apps.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
if (apps.isItemChecked(position)) {
Adapter.getItem(position);
Toast.makeText(getApplicationContext(), "CHECKED", Toast.LENGTH_LONG).show();
count = count + 1;
mode.setTitle(count + "items selected");
list_apps.add(Adapter.filteredData.get(position).packageName);
list_apps.trimToSize();
}
else {
Toast.makeText(getApplicationContext(), "UNCHECKED" , Toast.LENGTH_LONG).show();
count--;
mode.setTitle(count + "items selected");
list_apps.remove(Adapter.filteredData.get(position).packageName);
list_apps.trimToSize();
}
I am using an extended baseAdapter, please let me know if you need to see that code as well.
Update:
I am having OnItemClick listener in the code:
apps.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
/*
for (int i = 0; i < packageList.size(); i++) {
TextView txtview = ((TextView) arg1.findViewById(R.id.textView1));
product = txtview.getText().toString();
list_apps.add(Adapter.filteredData.get(arg2).packageName);
//Toast.makeText(MainActivity.this, product,
// Toast.LENGTH_SHORT).show();
//for
}
*/
String selection;
selection = Adapter.filteredData.get(arg2).packageName;
string = (String) Adapter.getItem(arg2);
//list_apps.trimToSize();
Toast.makeText(MainActivity.this, selection,
Toast.LENGTH_SHORT).show();
I am using activated_state for the item selected on filtering and maintaining that selection.
If i am not getting wrong, then you need to use OnItemClick lisenter, and get item like this.
Search View Change position of list item, but when we fetch our item from adapter, it return currect item.
lv.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
SString itemName = (String) adapter.getAdapter().getItem(position);
// assuming string and if you want to get the value on click of list item
// do what you intend to do on click of listview row
}
});
You can make a function getVisibleArray() in your adapter, and call it from your onItemClickListener.
in setOnClickListener:
People personInFocus = (People) adapter.getVisibleArray().get(position);
And in adapter:
public ArrayList<People> getVisibleArray() {
return mDisplayedValues;
}
Which is your filtered array.
Ive tested it and it works.

Knowing how many times an item is clicked in listview in android

Is there any way to find whether an item in a ListView is clicked for the first time in Android?
Initialize an arraylist with false for how many items in your listView, then try the following
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(arrayList.get(position) == false){ //Make sure you initialize your arraylist with false values (same amount as listView size)
arrayList.set(position, true); //set true to an arrayList in the same index of the listView that was pressed
}else{
//Has been clicked before, do something
}
}
});
If you need to, you could include this info to the model wrapper, for example if you display list of Items, so you will have wrapper:
class ItemWrapper {
Item mItem;
boolean mIsClicked;
}
In this case when you click on your item you could get info about if this item was already clicked.
If you need for example list of Checkable items (list of checkboxes) it's the right way to do, because this item is a part of your logic, you could definitely answer if you item is checked.
This will help you.
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
int count = 0;
try {
count = map.get(your_listview_value);
} catch (Exception e) {
e.printStackTrace();
}
map.put(your_listview_value, (count + 1));
Toast.makeText(getBaseContext(),
String.valueOf(count), Toast.LENGTH_LONG).show();
}
To know about the basic functionality of list view , Check this

Dont want to select Listview's First position By default in Custom Adapert

In my Code,I am using Custom Adapter to display Listview Items,,But Listview's First position is selected by default ,But I don't want like that.After My Selection It should high light.I posted my code below.Can any one help me ?
//To Call SelectionIndex Method
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
customAdapetr's Object.SelectionIndex(arg2);
}
//To get Selected Position:
public void SelectionIndex(int selectedposition){
this.selectedposition = selectedposition;
notifyDataSetChanged();
}
--
getView(){
if (position == selectedposition) {
convertView.setBackgroundColor(selectedColor);
}else {
convertView.setBackgroundResource(R.drawable.bg_palewhite_grad);
}
}
When the activity/adapter is getting initialized, selectedposition has a value of '0' (zero) which is causing the first item to be selected.
Try declaring the selectedposition to be -1 (any negative value)

List view select items in custom layout

I have a listview, in each row have five button, but I do not know as get focus over each button, someone know ?
You can do like this
Add tag value to that five button in its Xml file like 1,2,3,4,5
int rowNo;
int buttonTag;
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position,
long id) {
rowNo = position; // which row is selected...
buttonTag = v.getTag(); // which button is selected... if 1 then first if 2 then second
}
});
I hope this is what you want..

change background color on list item selection android

hello frnds i want to change background color (white on selection) of list on selection of list in listview and if i select any other position then first selected row comes to its previous state and currently selected rows background become white.so how to do this
public void onListItemClick(ListView parent, View v, int position, long id) {
super.onListItemClick(parent, v, position, id);
//do some stuff here
Toast.makeText(getApplicationContext(), "You have selected"+(position+1)+"th item", Toast.LENGTH_SHORT).show();
}
I wouldn't do that in code, since you later on might want to change colors, and you shouldn't have "layout/styling" code hardcoded.
Do instead create a style, and apply that to the ListView in your xml. You can read about how you do that in this thread:
ListSelector applies to the entire list
Your list view click listener:
yourlistView.setOnItemClickListener(new OnItemClickListener() {
#Override public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
yourAdapter.toggleSelected(position);
yourAdapter.notifyDataSetChanged();
}
});
Then make an ArrayList in your adapter and initialize it to store all the positions of your list view items:
public ArrayList<Integer> selectedIds = new ArrayList<Integer>();
int length = yourmainarraylist.size();
for(int i = 0; i < length; i++){
selectedIds.add(0);
}
then put a check in getView to toggle the background:
if (selectedIds.get(position)==1)
convertView.setBackgroundResource(R.drawable.list_row_selected);
else
convertView.setBackgroundResource(R.drawable.list_row);
and put this method in your adapter
public void toggleSelected(int position) {
selectedIds.set(position, (selectedIds.get(position) == 0));
}

Categories

Resources