Change displayed text in SimpleCursorTreeAdapter

Put your problem here if it does not fit any of the other categories.

Change displayed text in SimpleCursorTreeAdapter

Postby toxeo » Wed Jun 30, 2010 8:53 pm

Hello,

I'm using a "SimpleCursorTreeAdapter" and my problem is, that I want to change the way the data of the child-entries is displayed (in this case the date). The actual status of the view:

CategoryName
- Name | Date

I overrided the function "bindChildView" and tried to change the value there. But now I have some problems:

- I don't know, how to get the ViewID, so that I know if I'm in the view of the "Date" (I tried "view.getId()" but it's always "-1").
- I can't cast the passed "View" to a "TextView" so that I can write the formatted date to the view (with "view.setText()")

Do you have any ideas that could help me?

Code: Select all
        @Override
        protected void bindChildView(View view, Context context, Cursor cursor, boolean isLastChild){
           super.bindChildView(view, context, cursor, isLastChild);
           
           int noteDateColumnIndex = cursor.getColumnIndexOrThrow(DatabaseNote.COL_DATE);
          
           long milliseconds = cursor.getLong(noteDateColumnIndex);
          
           Date date = new Date(milliseconds);
           String formattedDate = DateUtils.formatDate(date, "dd.MM.yy");
          
          TextView textView = (TextView) view;
          textView.setText(formattedDate);
        }
toxeo
Freshman
Freshman
 
Posts: 2
Joined: Wed Jun 30, 2010 8:48 pm

Top

Re: Change displayed text in SimpleCursorTreeAdapter

Postby padde » Wed Jun 30, 2010 9:21 pm

Why dont you just use a ViewBinder.. you can there define what is bind to the view and how.. you get the view
the cursor and the column index.. so you can easy differentiate.
http://developer.android.com/reference/ ... inder.html


-- Edit --
Sample:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class MyViewBinder implements ViewBinder {
  2.     public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
  3.         if(view instanceof TextView) {
  4.             TextView tv = (TextView) view;
  5.             int noteDateColumnIndex = cursor.getColumnIndex(DatabaseNote.COL_DATE);
  6.             if(noteDateColumnIndex == -1) return false;
  7.             if(columnIndex == noteDateColumnIndex) {
  8.                 long milliseconds = cursor.getLong(noteDateColumnIndex);
  9.                 Date date = new Date(milliseconds);
  10.                 String formattedDate = DateUtils.formatDate(date, "dd.MM.yy");
  11.                 tv.setText(formattedDate);
  12.                 return true;
  13.             }
  14.         }
  15.         return false;
  16.     }
  17. }
  18.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


This viewbinder checks if the view data should get bound to is a textview and if its the correct one
(the one the date gets bound to)... if its not the right one the default build-in textview binding method
is used.

To use the ViewBinder just add this line where you define your SimpleCursorTreeAdapter:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     ...
  2.     SimpleCursorTreeAdapter adapter = new SimpleCursorTreeAdapter(...);
  3.     adapter.setViewBinder(new MyViewBinder());
  4.     ...
  5.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Re: Change displayed text in SimpleCursorTreeAdapter

Postby anarche » Thu Jul 01, 2010 12:21 am

If View.getId() is returning -1 then you have not set the ID.
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Re: Change displayed text in SimpleCursorTreeAdapter

Postby toxeo » Thu Jul 01, 2010 7:53 am

thank you padde!!!!! you exactly got my problem :) the columnIndex ist the additional information I missed and tried to get it over the View.

The code you posted will exactly do what I tried to figure out for hours/days....

I also tried the solution you gave me, but it I didn't work and so I searched on... I was just to stupid to recognize, that I need the API-Level 5 for this, but I will now update my application....
toxeo
Freshman
Freshman
 
Posts: 2
Joined: Wed Jun 30, 2010 8:48 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 15 guests