Can't highlight selected item in a ListView
NickName:Jerry Agin Ask DateTime:2015-06-11T04:22:57

Can't highlight selected item in a ListView

I'm having trouble highlighting the selected entry in a ListView. I've followed the procedures in http://www.michenux.net/android-listview-highlight-selected-item-387.html. I've also tried the suggestions in Android: setselector color for listview not working to no avail. I've tried working with a ListView in a DialogFragment as well as in an Activity.

What I've noticed is that (1) I can set the Selection, but I can't query the SelectedItemPosition, (2) onItemSelected never gets called, and (3) I can't get the child View.

Here's some code:

public class ChooseDirActivity extends Activity
{
  private ListView listView = null;

  @Override
  protected void onCreate( Bundle savedInstanceState )
  {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.choose_dir_layout );
    listView = (ListView)findViewById( R.id.directory_list );
    StateListDrawable selector = new StateListDrawable();
    selector.addState(new int[]{android.R.attr.state_selected}, new ColorDrawable( 0x808080 ));
    selector.addState( new int[]{ -android.R.attr.state_selected }, new ColorDrawable( 0xFFFFFF ) );
    listView.setSelector( selector );

    listView.setOnItemSelectedListener( new AdapterView.OnItemSelectedListener()
    {
      @Override
      public void onItemSelected( AdapterView<?> adapterView, View view, int i, long l )
      {
        Log.i( "myApp",  "onItemSelected" );  // Never called
        view.setBackgroundColor( 0x808080 );
      }

      @Override public void onNothingSelected( AdapterView<?> adapterView ) { } } ); listView.setChoiceMode( ListView.CHOICE_MODE_SINGLE ); }

  @Override
  protected void onResume()
  {
    super.onResume();
    currentDirectory = Intonia.getStorageDirectory();
    ArrayList<String> dirList = getDirectoryList();
    int selected = 11;
    listView.setAdapter( new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, dirList ) );
    Log.i( "myApp",  "setting selection to " + selected );
    listView.setSelection( selected );
    listView.setItemChecked( selected, true );
    Log.i( "myApp",  "selected item position is " + listView.getSelectedItemPosition() );
    View childView = listView.getChildAt( selected );
    if ( childView == null )
    {
      Log.i( "myApp",  "view at " + selected + " is null" );
    }
    else
      childView.setBackgroundColor( 0x808080  );
    Object so = listView.getItemAtPosition( selected );
    Log.i( "myApp",  "item at " + selected + " is " + ( so == null ? "null" : so.getClass().toString() ) );
  }

}

Setting the selection does move the selected item to the top of the screen, but there doesn't appear to be any way to follow up on it.

This produces the following log output

myApp: setting selection to 11
myApp: selected item position is -1
myApp: view at 11 is null
myApp: item at 11 is class java.lang.String

Can anyone help me?

Copyright Notice:Content Author:「Jerry Agin」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30766584/cant-highlight-selected-item-in-a-listview

More about “Can't highlight selected item in a ListView” related questions

Can Can for Rails Association

I used can-can gem for managing the roles in my application. The models are as follows class User &lt; ActiveRecord::Base has_many :projects has_and_belongs_to_many :teams end class Pr...

Show Detail

Can can with devise, admin and user

I use cancan and devise, I can update delete and show but I can't create profile. why I can't create new profile ("ActiveModel::ForbiddenAttributesError") class Ability include CanCan::Ability ...

Show Detail

rails 3 can can ability

I am using can can gem to restriction user based on role there is no model for product class ProductsController &lt; ApplicationController authorize_resource :class =&gt; false def index ...

Show Detail

GET request can be bookmarked and POST can not . Can anybody explain on this?

I am studying the HTTP methods. I read that GET request can be bookmarked and POST request can not be bookmarked. Can anybody explain this with an example? Thanks

Show Detail

Can not tnsping but can sqlplus connect

I have a question hopefully someone could explain it to me. I have an Oracle 11g installed properly on the server. From a workstation, I have installed the oracle client which tnsname.ora pointed t...

Show Detail

Can Can with Role Model restriction

I'm Trying to achieve that the user can see only HIS applications, and not the "Application Index" containing all the applications from all Users. My Header: &lt;%= link_to "My Application ",

Show Detail

CAN busoff with CAN_High and CAN_Low shorted

It is known fact that short circuiting CAN_High and CAN_Low on a CAN bus leads to a bus off condition. With respect to the physical layer, how does this condition lead to bus off condition?

Show Detail

What is CAN Active, CAN passive and Sleep state in CAN Network manager?

I am trying to understand the CAN network management in vehicle. During my research, I got to know that CAN network management(CANNM) will make some Mode state to decide the CAN transmission. Those...

Show Detail

Where is CAN ID in CAN Message Frame

I am new to the CAN-BUS protocol. So was going through the CAN Bus Specifications and related documents. I have always used the CAN ID and Frame at the application level. CANID like 0x1a1 CAN Fram...

Show Detail

Service can do ,but Asynctask can not?

I have read about difference between Service,Intent Service &amp; Asynctask ,but I have not figured out any situation where only service works but Asynctask fails. I have noticed some points: Asy...

Show Detail