from this original post :
http://androidforums.com/application-development/770370-listview-onitemlongclick-getview-solved.html
I had help from alostpacket to get the longclick to highlight the row on the simplecursor adapter.
Now as further progress to this, I would like the user to be able to unhighlight the row also using a long click. This I thought should be simple but after hours of trying to get this to work and also getting so close, I have run into a problem.
The problem occurs when the following pattern happens :
User long clicks on e.g. row 1 . This is then highlighted. User then long clicks on row 1 again, this is then unhighlighted and at this point I added a Toast message to confirm this part works. So far so good.
User then long clicks on e.g. row 3 - this is highlighted but if the user either long clicks on row 1 afterwards or long clicks on row 3 to unhighlight (that works) but then long clicks on row 1, it does not highlight BUT it does not bring up the Toast message so I know it has not reached the method to unhighlight it.
Sorry if the above sounds confusing it is very hard to explain in a post .
Also found that due to multiple return true statements in this SetValueView method this makes the method get read for every return true that there is. I am wondering if this has some affect on what I am trying to do
I am still using the viewbinder as per the post link above but I have added logic ( all be it upside down ) to both the long click method and the viewbinder as well.
Long Click Code :
[HIGH]
if (longrowclick1==arg2) {
islongclickbefore = true;
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "YOU HAVE CLICKED HERE BEFORE!!!!", Toast.LENGTH_LONG).show();
// longrowclick1 = -1;
return true;
} else {
isclick = false;
islongclick = true;
longrowclick1 = arg2;
Log.d("ONITEMLONGCLICK","View Clicked = "+arg1);
Log.d("ONITEMLONGCLICK","Position Clicked = "+arg2);
islongclickbefore = false;
adapter.notifyDataSetChanged();
// datalist.smoothScrollToPosition(longrowclick1);
return false;
}
[/HIGH]
ViewBinder / SetViewValue Code part 1 of 3 :
[HIGH]
if (cursor.getPosition()==longrowclick1 && !islongclickbefore ) {
islongclicked = true;
// sets up parent view of object
myviewp = (View) view.getParent();
Log.d("HERE", "Set to Blue not long clicked");
// checks if parent view is valid (there are a lot here)
if (((View)view.getParent()).getId()==R.id.Table1 || ((View)view.getParent()).getId()==R.id.tableRow1 || ((View)view.getParent()).getId()==R.id.tableRow2 || ((View)view.getParent()).getId()==R.id.tableRow3 || ((View)view.getParent()).getId()==R.id.Table2 || ((View)view.getParent()).getId()==R.id.Table3 ) {
//Toast.makeText(getApplication(), "Long Row Click1 = "+Integer.toString(longrowclick1) +" and Prev Long Click = "+Integer.toString(prevlongclick) , Toast.LENGTH_LONG).show();
Log.d("PARENTCHECK", "Set to Blue not long clicked");
myviewp.setBackgroundResource(R.drawable.darkslateblue);
if (view.getId()!=R.id.seper) {
view.setBackgroundResource(R.drawable.darkslateblue);
// ... set them to the dark slate blue ...
// view.setBackgroundResource(R.drawable.darkslateblue);
} else { // else if the child object is the date separator ..
// .. keep it as dark gray
view.setBackgroundColor(Color.DKGRAY);
}
}
[/HIGH]
ViewBinder / SetViewValue Code part 2 of 3 :
[HIGH]
if (cursor.getPosition()!=longrowclick1 && !islongclickbefore ) {// if the cursor position does not match the Long Click Position then ..
// .. get the parent of the current child object ...
islongclicked = false;
Log.d("HERE", "Set to black not long clicked : longrowclick1 row no= "+longrowclick1 +"Cursor POS="+cursor.getPosition());
//islongclickbefore = false;
// colorcounter = -1;
myviewp = (View) view.getParent();
// .. set the background of the parent to black ..
myviewp.setBackgroundColor(Color.BLACK);
// .. check to make sure that the child object is not the date separator ...
if (view.getId()!=R.id.seper) {
// .. and then set the child object background to black ..
view.setBackgroundColor(Color.BLACK);
} else { // .. else if it the child object is the date separator then ..
// .. set it to the dark gray background .
view.setBackgroundColor(Color.DKGRAY);
}
ColorDrawable vbk1 = (ColorDrawable) myviewp.getBackground();
Log.d("BACKGRD", "Back Ground Color of Main Views int value when not selected (BLACK) = "+vbk1.getColor());
}
[/HIGH]
ViewBinder / SetViewValue Code part 3 of 3 :
[HIGH]
if (cursor.getPosition()==longrowclick1 && islongclickbefore ) {
Log.d("HERE", "Set to black - Long Clicked!!!");
myviewp = (View) view.getParent();
if (((View)view.getParent()).getId()==R.id.Table1 || ((View)view.getParent()).getId()==R.id.tableRow1 || ((View)view.getParent()).getId()==R.id.tableRow2 || ((View)view.getParent()).getId()==R.id.tableRow3 || ((View)view.getParent()).getId()==R.id.Table2 || ((View)view.getParent()).getId()==R.id.Table3 ) {
// .. set the background of the parent to black ..
myviewp.setBackgroundColor(Color.BLACK);
// .. check to make sure that the child object is not the date separator ...
if (view.getId()!=R.id.seper) {
// .. and then set the child object background to black ..
view.setBackgroundColor(Color.BLACK);
} else { // .. else if it the child object is the date separator then ..
// .. set it to the dark gray background .
view.setBackgroundColor(Color.DKGRAY);
}
}
//longrowclick1 = -1;
//islongclickbefore = false;
return true;
}[/HIGH]
Thanks
TimCS
http://androidforums.com/application-development/770370-listview-onitemlongclick-getview-solved.html
I had help from alostpacket to get the longclick to highlight the row on the simplecursor adapter.
Now as further progress to this, I would like the user to be able to unhighlight the row also using a long click. This I thought should be simple but after hours of trying to get this to work and also getting so close, I have run into a problem.
The problem occurs when the following pattern happens :
User long clicks on e.g. row 1 . This is then highlighted. User then long clicks on row 1 again, this is then unhighlighted and at this point I added a Toast message to confirm this part works. So far so good.
User then long clicks on e.g. row 3 - this is highlighted but if the user either long clicks on row 1 afterwards or long clicks on row 3 to unhighlight (that works) but then long clicks on row 1, it does not highlight BUT it does not bring up the Toast message so I know it has not reached the method to unhighlight it.
Sorry if the above sounds confusing it is very hard to explain in a post .
Also found that due to multiple return true statements in this SetValueView method this makes the method get read for every return true that there is. I am wondering if this has some affect on what I am trying to do
I am still using the viewbinder as per the post link above but I have added logic ( all be it upside down ) to both the long click method and the viewbinder as well.
Long Click Code :
[HIGH]
if (longrowclick1==arg2) {
islongclickbefore = true;
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "YOU HAVE CLICKED HERE BEFORE!!!!", Toast.LENGTH_LONG).show();
// longrowclick1 = -1;
return true;
} else {
isclick = false;
islongclick = true;
longrowclick1 = arg2;
Log.d("ONITEMLONGCLICK","View Clicked = "+arg1);
Log.d("ONITEMLONGCLICK","Position Clicked = "+arg2);
islongclickbefore = false;
adapter.notifyDataSetChanged();
// datalist.smoothScrollToPosition(longrowclick1);
return false;
}
[/HIGH]
ViewBinder / SetViewValue Code part 1 of 3 :
[HIGH]
if (cursor.getPosition()==longrowclick1 && !islongclickbefore ) {
islongclicked = true;
// sets up parent view of object
myviewp = (View) view.getParent();
Log.d("HERE", "Set to Blue not long clicked");
// checks if parent view is valid (there are a lot here)
if (((View)view.getParent()).getId()==R.id.Table1 || ((View)view.getParent()).getId()==R.id.tableRow1 || ((View)view.getParent()).getId()==R.id.tableRow2 || ((View)view.getParent()).getId()==R.id.tableRow3 || ((View)view.getParent()).getId()==R.id.Table2 || ((View)view.getParent()).getId()==R.id.Table3 ) {
//Toast.makeText(getApplication(), "Long Row Click1 = "+Integer.toString(longrowclick1) +" and Prev Long Click = "+Integer.toString(prevlongclick) , Toast.LENGTH_LONG).show();
Log.d("PARENTCHECK", "Set to Blue not long clicked");
myviewp.setBackgroundResource(R.drawable.darkslateblue);
if (view.getId()!=R.id.seper) {
view.setBackgroundResource(R.drawable.darkslateblue);
// ... set them to the dark slate blue ...
// view.setBackgroundResource(R.drawable.darkslateblue);
} else { // else if the child object is the date separator ..
// .. keep it as dark gray
view.setBackgroundColor(Color.DKGRAY);
}
}
[/HIGH]
ViewBinder / SetViewValue Code part 2 of 3 :
[HIGH]
if (cursor.getPosition()!=longrowclick1 && !islongclickbefore ) {// if the cursor position does not match the Long Click Position then ..
// .. get the parent of the current child object ...
islongclicked = false;
Log.d("HERE", "Set to black not long clicked : longrowclick1 row no= "+longrowclick1 +"Cursor POS="+cursor.getPosition());
//islongclickbefore = false;
// colorcounter = -1;
myviewp = (View) view.getParent();
// .. set the background of the parent to black ..
myviewp.setBackgroundColor(Color.BLACK);
// .. check to make sure that the child object is not the date separator ...
if (view.getId()!=R.id.seper) {
// .. and then set the child object background to black ..
view.setBackgroundColor(Color.BLACK);
} else { // .. else if it the child object is the date separator then ..
// .. set it to the dark gray background .
view.setBackgroundColor(Color.DKGRAY);
}
ColorDrawable vbk1 = (ColorDrawable) myviewp.getBackground();
Log.d("BACKGRD", "Back Ground Color of Main Views int value when not selected (BLACK) = "+vbk1.getColor());
}
[/HIGH]
ViewBinder / SetViewValue Code part 3 of 3 :
[HIGH]
if (cursor.getPosition()==longrowclick1 && islongclickbefore ) {
Log.d("HERE", "Set to black - Long Clicked!!!");
myviewp = (View) view.getParent();
if (((View)view.getParent()).getId()==R.id.Table1 || ((View)view.getParent()).getId()==R.id.tableRow1 || ((View)view.getParent()).getId()==R.id.tableRow2 || ((View)view.getParent()).getId()==R.id.tableRow3 || ((View)view.getParent()).getId()==R.id.Table2 || ((View)view.getParent()).getId()==R.id.Table3 ) {
// .. set the background of the parent to black ..
myviewp.setBackgroundColor(Color.BLACK);
// .. check to make sure that the child object is not the date separator ...
if (view.getId()!=R.id.seper) {
// .. and then set the child object background to black ..
view.setBackgroundColor(Color.BLACK);
} else { // .. else if it the child object is the date separator then ..
// .. set it to the dark gray background .
view.setBackgroundColor(Color.DKGRAY);
}
}
//longrowclick1 = -1;
//islongclickbefore = false;
return true;
}[/HIGH]
Thanks
TimCS