1

我有这个布局,其中有一个列表视图。列表的内容是在另一个布局中定义的。我在这个布局中有文本视图,我想根据某些条件动态更改文本视图的颜色(比如简单的 if else)。请让我知道如何执行此操作。这是我的代码-:

ListAdapter adapter = new SimpleAdapter(this, menuItems,
                R.layout.recentlist,
                new String[] { KEY_CAT, KEY_DATE, KEY_TID, KEY_AMO, KEY_DEB,KEY_CUR,KEY_BAL,KEY_FEES}, new int[] {
                R.id.textView1,R.id.textView2, R.id.textView3, R.id.textView4,R.id.textView5,R.id.textView7,R.id.textView6,R.id.textView8});

        setListAdapter(adapter);

这个特殊的布局是我从另一个 xml 文件中添加的,用于将 listview 的内容放入其中。所以我不能通过使用 textView 来设置TextColor,因为它需要这个类布局的 id 而不是这个布局。请帮助我解决这个问题。

4

3 回答 3

0

AFAIK you cant use a SimpleAdapter to perform this operation as SimpleAdapter only copies data from data set to TextView in your ListView. In order to handle color changes at run-time you should define a CustomArrayAdapter.

于 2013-04-04T12:10:29.180 回答
0

通过将 SimpleAdapter 的 getView 方法扩展到您自己的类来覆盖它。在 getView 方法中,您可以使用 View.findViewById ,然后对您的 TextView 做任何您想做的事情

于 2013-04-04T12:14:34.030 回答
0
TextView tv1 = (TextView) vi.findViewById(R.id.your_textview_id);
// where vi is convertview
tv1.setTextColor(Color.BLUE);
于 2013-04-04T12:16:18.400 回答