【问题标题】:How to customize TextViews inside SimpleCursorAdapter - Android如何在 SimpleCursorAdapter - Android 中自定义 TextViews
【发布时间】:2014-08-23 19:06:54
【问题描述】:

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

下面是我的工作代码,我从一个类中获取字符串并插入到 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;

    }
}

【问题讨论】:

    标签: android android-listview simplecursoradapter custom-font


    【解决方案1】:

    试一试:

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

    【讨论】:

    • 它说“ListActivity 类型的方法 setViewText(TextView, String) 未定义”
    • @user45678 确保您正确复制了代码,{ 后面有一个大括号 {
    • 这是一个非常快速的选择。这节省了我实现自定义适配器的时间。
    • 这太棒了!谢谢!我正在努力使用findViewById() 从我的 ListView 中获取 TextView(我想是因为我使用的是加载器?)但这完美无瑕。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多