0

As I am working on a project where it needs to work with API 8 I found a lot of examples on how to highlight a row would not work as they need minimum API 10 upwards.

So I have looked around and got the highlighter to work via OnListItemClick however my problem now is , when there is more than one item in the list, it allows for multiple items to be selected which is not what I want

I used the following to highlight and un highlight the row:

if (selectedrow == 0) {
   ((TextView)v).setBackgroundColor(Color.rgb(0, 153, 204)); 
   selectedrow = 1;
   oki.setEnabled(true);
   currpos = position;
   File fileselected = new File(pathi.get(currpos));

 } else {
  ((TextView)v).setBackgroundColor(Color.TRANSPARENT); 
  selectedrow = 0;
  oki.setEnabled(false);
   currpos = -1;

}

When I noticed it was highlighting more than one row I then tried the following before the above code:

if (currpos != -1 ) {
   ((ListView)l).smoothScrollToPosition(currpos);
   ((TextView)v).setBackgroundColor(Color.TRANSPARENT); 
   ((ListView)l).smoothScrollToPosition(position);
   selectedrow = 0;
 }

I have also tried the above as follows:

if (currpos != -1 ) {
  ((ListView)l).setSelection(currpos);
  ((TextView)v).setBackgroundColor(Color.TRANSPARENT); 
  ((ListView)l).setSelection(position);
  selectedrow = 0;
 }

EDIT*

The custom adapter is now working and is as follows but the highlighter is still an issue I hope posting the code will shed some light on this:

public class FileListAdapter extends ArrayAdapter<String>  {

     private List<String> itemsll; 

        public FileListAdapter(Context context, int row,
                List<String> iteml) {
            super(context,row,iteml);
            // TODO Auto-generated constructor stub
            this.itemsll = iteml;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View viewnull = convertView;
            if (viewnull == null) {
                LayoutInflater vrow;
                vrow = LayoutInflater.from(getContext());

                viewnull = vrow.inflate(R.layout.row, null);
            }
            String currow = itemsll.get(position);
            TextView rowtext = (TextView)viewnull.findViewById(R.id.rowtext);
            rowtext.setText(currow);

            if (currpos == position) {
                rowtext.setBackgroundColor(Color.BLUE);
            }

            return viewnull;
        }


        public void setSelectedPosition( int pos )
        {
            currpos = pos; // selectedPos is global variable to handle clicked position
            // inform the view of this change
            notifyDataSetChanged();
        }
     }

and then I added the onItemClick as follows:

public void onItemClick( AdapterView<?> arg0,View arg1, int arg2, Long arg3) {

         int pos = arg2;
         fl.setSelectedPosition(pos);
     }

Solved.
Soon registered that I could not use onListItemClick and onItemClick and also as the class is a ListActivity I had to use Listview lw = getListView() to then use the onItemClick method

Cœur
  • 37,241
  • 25
  • 195
  • 267
TimCS
  • 363
  • 1
  • 7
  • 17
  • Worked out that the error is to do with the getview method as, when I comment the getview method out of the custom adapter, the Listactivity class which then calls the adapter does not crash with the above error. I suspect it is to do with the return statement so I need to work out how to prevent a null return now. – TimCS Jul 28 '13 at 11:11

2 Answers2

0

Your approach is wrong there is some thing called selector which works on all the api version almost and I have did it on API level 8 as well there is the example try this

Here's Adapter Layout

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:background="@drawable/rowselector" >

   . . . . . . 

</RelativeLayout>

you can see I am setting background which actually is a selector

My rowselector (keeping this in drawable)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_enabled="true" 
     android:state_pressed="true" android:drawable="@drawable/left_navbg_hover" />
    <item android:state_enabled="true"
     android:state_focused="true" android:drawable="@drawable/left_navbg_hover" />
    <item android:state_enabled="true"
     android:state_selected="true" android:drawable="@drawable/left_navbg_hover" />
    <item
     android:drawable="@drawable/left_navbg" />
</selector>

where left_navbg_hover and left_navbg is an Image I have used .

Try that , all the best !

  • Thanks Shashank I will give this ago as well - hopefully it will sort it :), I have a list layout and a row layout which would this be added to? – TimCS Jul 26 '13 at 09:25
  • Also how to I detect whether the row is selected in code with this method? – TimCS Jul 26 '13 at 12:28
  • tried this but the only background that appears is the default non selected background. Might try using colours instead – TimCS Jul 26 '13 at 12:53
  • Used your code for the selector but I am still unable to keep the row selected - I have even tried to use the ChoiceMode="singleChoice" in the listview but this has made no difference, I have removed the code from the onlistclickitem that I used to highlight it as well. Any thoughts on what to try now? – TimCS Jul 26 '13 at 22:07
0

if you are using custom list adaptor than you can do one thing:

create one public method in your adaptor class like :

public void setSelectedPosition( int pos )
    {
        selectedPos = pos; // selectedPos is global variable to handle clicked position
        // inform the view of this change
        notifyDataSetChanged();
    }

In getView method change your backgroundcolor:

 if ( selectedPos == position )
    {
            ((TextView)v).setBackgroundColor(Color.rgb(0, 153, 204)); 
    }

onItemClick method call adaptor method:

@Override
    public void onItemClick( AdapterView<?> arg0, View arg1, int arg2, long arg3 )
    {
        int position = arg2;

        youradapter.setSelectedPosition( position );

    }
KDeogharkar
  • 10,939
  • 7
  • 51
  • 95
  • The activity is a ListActivity and the list is based on folders and files so I am trying to only highlight one file at a time I will look into trying your example- thanks for this – TimCS Jul 26 '13 at 09:24
  • can I use your code if the adapter is not a separate class? currently the adapter is defined in the method which creates the list of files and folders , I used this example to create the files and folders list : [file explorer](http://android-er.blogspot.co.uk/2012/07/example-of-file-explorer-in-android.html) – TimCS Jul 26 '13 at 12:31
  • are you using the default adaptor or you create your own? – KDeogharkar Jul 26 '13 at 12:37
  • I am doing the following within a getdir method as per the link above : fileListi = new ArrayAdapter(this, R.layout.row, itemi); setListAdapter(fileListi); – TimCS Jul 26 '13 at 12:52
  • thats default . no sorry – KDeogharkar Jul 26 '13 at 12:57
  • Could I change it to work the way you have stated? obviously the current Class pretty much as the link I posted in the other comment. If this can be changed to make it work as you have put that would be great - Thanks TimCS – TimCS Jul 26 '13 at 16:21
  • How do I go about this? – TimCS Jul 27 '13 at 07:04
  • Thanks for these I will study them and hopefully get this working – TimCS Jul 27 '13 at 07:19
  • Added more info to post - having problems with custom adapter – TimCS Jul 27 '13 at 22:39
  • Got the custom adapter to work see [here] (http://stackoverflow.com/questions/17908827/custom-arraylist-string-adapter-coming-up-empty-solved) . However after doing that I have put your code in place and the row does not get highlighted – TimCS Jul 28 '13 at 21:30
  • I think this is down to the OnItemClick as the adapter does not have this as a method but the listview does. Is this the method from the listview that I am using ? – TimCS Jul 29 '13 at 15:23
  • I have tried to use the onListItemClick method as part of the listview to get this to work but when I choose one row and then choose another the previous row stays selected. I do not seem to be able to use OnItemClick as you have stated as my Listview is not an object defined as the activity is a ListActivity. Any thoughts on how to sort this please @BaZinga – TimCS Jul 29 '13 at 19:54