0

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
TimCS
  • 363
  • 1
  • 7
  • 17
  • Wondering now if I need to create a custom adapter extending a simplecursoradapter - is this the route to take to make this work? – TimCS Sep 10 '13 at 16:24
  • My other problems seems to be with the row layout I think as it has at least three table layouts under a linear layout. So each of the table layouts have their own background colour to match that of the main layout and these will need changing when the row is "touched" – TimCS Sep 11 '13 at 12:37
  • Well the latest is that I can get the row to highlight but when I click another row, the first stays highlighted and other rows then become highlighted! – TimCS Sep 12 '13 at 15:38

1 Answers1

0

So far I have got the onItemLongClickListener to call the getView using the notifydatasetchange from the adapter. My problem is now that when the row is selected, it always highlights the first row in the list.

Sorted this now by using the setviewbinder and setviewvalue methods .. here is the code:

// is the current cursor position the same that has been Long Clicked             
if (cursor.getPosition()==longrowclick1) {  

    // sets up parent view of object  
     myviewp = (View) view.getParent();  
     // 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 ) {  

        // If the parent views are valid set the background to dark slate blue  
        myviewp.setBackgroundResource(R.drawable.darkslateblue);  
        // if one of the child views are not the date separator then ..  
        if (view.getId()!=R.id.seper) {  
            // ... 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);  
        }  



    }  




} else {// if the cursor position does not match the Long Click Position then ..  
    // .. get the parent of the current child object ...  
     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);  
     }  


}  
TimCS
  • 363
  • 1
  • 7
  • 17