I have a ListActivity which then manages a simplecursoradapter to display text and images plus a separator for each day of the month. This separator and the information that is fed into the adapter is done by using a viewbinder. As I need this App to work from API 8 some of the answer I have found appear not to work for that API version.
So far I have tried to use the OnItemClickListener to get the row selected, I then have stored the position of this row and used this with the getView method to try and set the background resource but all it appears to change is the colour of the separator and not just the current row selected but others too.
The OnItemClickListener has been altered a few times so the current code is my last and failed attempt:
datalist.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
mC.moveToPosition(arg2); // The Cursor
rowchecked = arg2; // Global variable to store the position
arg1.setBackgroundResource(android.R.color.holo_red_dark); // this is a recent attempt but I knew it would not work
Toast.makeText(getApplicationContext(), "Row "+Integer.toString(arg2) + " Clicked", Toast.LENGTH_LONG).show();
}});
The datalist is assigned as follows:
ListView datalist = getListView();
datalist.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
The getView is as follows:
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.viewdata, mC, fields, viewfields)
{
@Override
public View getView(int position,View convertView, ViewGroup parent) {
final View row = super.getView(position, convertView, parent);
if (position == rowchecked) {
Toast.makeText(getApplicationContext(), "Detected row selected , row is :"+Integer.toString(rowchecked), Toast.LENGTH_LONG).show();
//row.setBackgroundResource(Color.BLUE);
row.setBackgroundColor(Color.BLUE);
mC.moveToPosition(position);
row.setSelected(true);
}
return row;
}
};
if the viewbinder code is required then I will post when requested - there is a lot there so I do not what to post it all at this point.
*** EDIT ****
Really struggling to get the OnitemLongClick part of this post to work the OnitemClick seems to use/call the getView method of the simplecursoradapter but the OnItemLongClick Listener doesn't.
Reading other posts including this one -> how to implement a long click listener on a listview
I have tried the setonlongclick(true) against the listview object in the code and I have tried assigning this to the listview in the XML layout plus I have created the OnItemLongLick Listener with the AdapterView.OnItemLongClick Listener.