1

By default i need to show one item as highlighted in horizontal list view and when the user selected another item in the horizontal list view i want to highlight that item(removing the earlier and highlight the currently selected) for that i'm trying with the following code,in my adapter

Adapter:-

 int selectedIndex;
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
   View v = convertView;
   v = LayoutInflater.from(parent.getContext()).inflate(
        R.layout.hlist_rowitem, null);

  if (position == selectedIndex) {
    v.setBackgroundColor(Color.parseColor("#abcdef"));
 }
}

and after selecting another item from activity in from the list view what to do in activity to change highlighting position of the item.

Activity:-

int sIndex;
sIndex = getIntent().getIntExtra("POSITION", 0);
hlAdapter = new HSelectedAdapter(InsuranceCard.this, rowItems, sIndex);
hListView.setAdapter(hlAdapter);
hListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
 //other code goes here
}
});
Harish
  • 3,122
  • 2
  • 31
  • 46

4 Answers4

2

I'd would use a color state list resource and have the ListView handle the selection with setSelection(position).

The color list would look something like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:color="#aabbcc"/> <!-- pressed -->
    <item android:state_activated="true"
          android:color="#fedcba"/> <!-- selected -->
    <item android:color="#abcdef"/> <!-- default -->
</selector>

and it should be set as background of the R.layout.hlist_rowitem or as listSelector on the listview.

Edit: To change the selection when receiving a click event:

hListView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
        hListView.setSelection(position);
    }
});

The ListView will deselect the old/default item and selects the new item at the specified position.

Edit 2: By default the ListView don't have a choice mode set so make sure you either set it in xml or in code: listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

Delblanco
  • 671
  • 9
  • 19
  • then how to remove earlier selected item? – Harish Jul 08 '14 at 11:49
  • You don't :) The ListView only highlights one selected item so every time you select a new item the old item is deselected. – Delblanco Jul 08 '14 at 11:50
  • If you observe my question in the above i'm setting a default item as selected and now the problem is removing the default selection and applying new selected item. – Harish Jul 08 '14 at 11:54
  • So you want only one item to be highlighted as selected? and when you select a new item you want the old to be deselected? If that's the case you can easily use the selector. Just call listView.setSelection(newPosition) whenever you want to deselect the old item and select the new item. – Delblanco Jul 08 '14 at 12:05
  • then how to pass it to the adapter and how to update it in onclick – Harish Jul 08 '14 at 12:45
  • I edited my answer with an onClick. With my approach I don't have to pass anything to the adapter. – Delblanco Jul 08 '14 at 12:50
  • by default i need to set a selected item then how to do that? – Harish Jul 08 '14 at 13:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/56954/discussion-between-delblanco-and-harish). – Delblanco Jul 08 '14 at 13:00
1

you can achieve this in two ways.

  1. manually clear all item and set selected in onItemClick()

    listview.setOnItemClickListener(new OnItemClickListener() {
    
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
    
            for (int i = 0; i < adapter.getCount(); i++) {
            View item = listview.getChildAt(i);
            if (item != null) {
                item.setBackgroundResource(R.drawable.unselected);
            }
            arg1.setBackgroundResource(R.drawable.selected);
        }
    
        }
    });
    
  2. use selector and let listview do itself.

/drawable/selector_list.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

   <item android:drawable="@drawable/selected" android:state_selected="true"/>
   <item android:drawable="@drawable/selected" android:state_activated="true"/>
   <item android:drawable="@drawable/unselected"/>

</selector>

and add android:listSelector="@drawable/selector_list" to your listview

Ketan Ahir
  • 6,678
  • 1
  • 23
  • 45
  • need to show one item as it is default selection and when the user changes according to that change the selection. – Harish Jul 08 '14 at 11:51
  • use this [How to tap ListView item programmatically](http://stackoverflow.com/a/16278023/1608643) – Ketan Ahir Jul 08 '14 at 12:00
  • hey thank for your answer its work. but when i click on first and scroll then last item also selected. when i second item select then last of two item also selected. – tej shah Jul 31 '15 at 13:18
  • @tejshah I think it happens due to recycling. try viewholder pattern in getView() method of adapter. – Ketan Ahir Aug 01 '15 at 08:48
0

add listitemclick.xml in your drawblw folder this is the code. 2)then in your hlist_rowitem.xml set background="@drawable/listitemclick"

0

follow these steps: 1)declare one boolean array. public static boolean ClickItem[]; 2)inside oncreate ClickItem=new boolean[your array size]; Arrays.fill(ClickItem, false);

in your adapter write this code a)
if ClickItem[pos] { v.setBackgroundColor(Color.parseColor("#abcdef")); }else

a) v.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        Arrays.fill(ClickItem, false);
            ClickItem[pos]=true;

            adapter1.notifyDataSetChanged();


        }
    });
  • 1)it is not multiple selection 2) what is arrays.fill() and how you are getting that into the adapter. – Harish Jul 08 '14 at 11:56