SimpleCursorAdapter的子字符串?

时间:2011-03-29 16:36:36

标签: android listview

这是我尝试将ListView与。

绑定的ListAdapter
ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.my_item,c,new String[] {"title","body"},
new int[] { R.id.TextView_Title,R.id.TextView_Body});
setListAdapter(adapter);

这很有效。但我坚持认为应该很简单。我想要做的是在显示身体的子串(例如,前10/15个字符)时完全显示标题。在这种情况下我该怎么做?我可以直接操作光标,以便它在第一个位置返回子字符串,或者在光标返回值后必须这样做(在这种情况下,如何?)。谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

编辑:好的,对不起,我以为你想要“获取”正文列的子字符串,但你想把它设置为子字符串???

对您的活动实施SimpleCursorAdapter.ViewBinder并覆盖setViewValue()

在您的代码中的某个时刻,您需要使用...

adapter.setViewBinder(this); // Put this on onCreate() perhaps

...代码看起来与此类似......

public class MyActivity extends Activity
    implements SimpleCursorAdapter.ViewBinder {

    @Override
    public boolean setViewValue(View view, Cursor cursor, int column) {
        int bodyColumn = cursor.getColumnIndex("body");
        if (column == bodyColumn) {
            String bodyString = cursor.getString(bodyColumn);
            ((TextView)view).setText(bodyString.subString(0, 10));

            return true; // Return true to show you've handled this column
        }
        return false;
    }
}