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