Search code examples
androidandroid-listviewsimplecursoradaptercustom-font

How to customize TextViews inside SimpleCursorAdapter - Android


I was wanted to customize my textviews in SimpleCursorAdapter can someone help me to do that in a easiest way where too many code changes is not required...

Below is my working code where i am fetching strings from a class and inserting in SimpleCursorAdapter

code :

public class MyListActivity extends ListActivity {
    /** Called when the activity is first created. */


    private Cursor mCursor = null;


    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.weeklysettings);
        getListView().addHeaderView(buildHeader(), null , false);
        Cursor mCursor = getCursorDetails();
        startManagingCursor(mCursor);

        ListAdapter adapter = new SimpleCursorAdapter(this, // Context.
                R.layout.listview, // Specify the row template
                                    // to use (here, three
                                    // columns bound to the
                                    // two retrieved cursor
                                    // rows).
                mCursor, // Pass in the cursor to bind to.
                // Array of cursor columns to bind to.
                new String[] { MyClass.EVENT,
                MyClass.CHANNEL_NAME,
                MyClass.PROGRAM_TITLE,
                MyClass.EVENTTIME },

                new int[] { R.id.event, R.id.channelname, R.id.programtitle, R.id.timestamp});
        // Bind to our new adapter.
        setListAdapter(adapter);
        //setListAdapter(new ArrayAdapter<String>(this, R.layout.listview, ynetList));

    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        stopManagingCursor(mCursor);
    }


    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        startManagingCursor(mCursor);
    }
    private ViewGroup buildHeader() {
        LayoutInflater infalter = getLayoutInflater();
        ViewGroup header = (ViewGroup) infalter.inflate(R.layout.listitem_header, getListView(), false);
        header.setEnabled(false);
        return(header);
      }
    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
         Intent intent = new Intent(this, SettingsActivity.class);
         intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
         startActivity(intent);
         finish();
         super.onBackPressed();
    }

    private Cursor getCursorDetails() {


        String sortOrder = DataExchangeFacility.TIMESTAMP
                + " COLLATE LOCALIZED DESC";

        mCursor = getApplicationContext().getContentResolver().query(
                DataExchangeFacility.CONTENT_URI_GRACE, null, null, null, sortOrder);

        return mCursor;

    }
}

Solution

  • Give this a shot:

        ListAdapter adapter = new SimpleCursorAdapter(this, // Context.
                R.layout.listview, // Specify the row template
                // to use (here, three
                // columns bound to the
                // two retrieved cursor
                // rows).
                mCursor, // Pass in the cursor to bind to.
                // Array of cursor columns to bind to.
                new String[] { MyClass.EVENT,
                        MyClass.CHANNEL_NAME,
                        MyClass.PROGRAM_TITLE,
                        MyClass.EVENTTIME },
    
                new int[] { R.id.event, R.id.channelname, R.id.programtitle, R.id.timestamp}) {
            @Override
            public void setViewText(TextView v, String text) {
                // v is your TextView
                v.setTextColor(Color.RED);
                super.setViewText(v, text);
            }
        };