android - 如何在 SimpleCursorAdapter 中自定义 TextView - Android

标签 android android-listview simplecursoradapter custom-font

我想在 SimpleCursorAdapter 中自定义我的 TextView ,有人可以帮助我以最简单的方式做到这一点,不需要太多代码更改...

下面是我的工作代码,我从类中获取字符串并插入 SimpleCursorAdapter

代码:

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;

    }
}

最佳答案

试一下:

    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);
        }
    };

关于android - 如何在 SimpleCursorAdapter 中自定义 TextView - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25465450/

相关文章:

android - 如何从 href =""在移动设备上打开 facebook native 应用程序

javascript - 按回主屏幕时如何从 Webview 获得暂停/停止声音?

android - 从内部存储加载图像时 ListView 滚动缓慢

android - Android 中带有搜索栏的 ListView 中的音频播放器

java - 多选 ListView 和 SharedPreferences

java - 如何在 Android Pie 上共享 CSV 文件?

android.content.ActivityNotFoundException : No Activity found to handle Intent { act=android. intent.action.VIEW

android - 如何使用 simplecursoradapter 按 id 在特定项目上定位(选择)微调器?

java - 通过自定义适配器将已添加书签的项目的状态保存到 SQLite 表

android - 在堆栈跟踪中没有应用程序代码的 ListView 中滚动时的 IndexOutOfBounds