首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >动态更改listview项目中的文本视图颜色

动态更改listview项目中的文本视图颜色
EN

Stack Overflow用户
提问于 2013-02-16 20:40:32
回答 2查看 799关注 0票数 0

我使用简单的游标适配器从具有自定义布局的listview中的视图中获取所有记录。我想要显示优先级文本视图,以根据文本更改颜色,例如红色(如果优先级高)、绿色(如果优先级高)和黄色(如果是low.is )。我是android新手。

我的listview代码

代码语言:javascript
运行
复制
myList=(ListView) findViewById(R.id.listView1);
    myAdapter.open();

    Cursor cursor = myAdapter.fetchAllView();

    startManagingCursor(cursor);
String []from=new String[]{ DbAdapter.KEY_DESCRIPTION,DbAdapter.KEY_TITLE,DbAdapter.KEY_TASK_PRIORITY,DbAdapter.KEY_CATEGORY_NAME,DbAdapter.KEY_TIME,DbAdapter.KEY_DATE };
int[] to=new int[] { R.id.txtdescription,R.id.txtitem,R.id.txtPriority,R.id.txtcategory,R.id.txttimeOne,R.id.txtDateOne}; 
 myCursorAdapter = new SimpleCursorAdapter(ListItems.this, R.layout.items, cursor,from, to);
myList.setAdapter(myCursorAdapter);

listview运行良好,但如何编写代码来更改listview项目中的文本视图颜色

提前感谢

EN

Stack Overflow用户

回答已采纳

发布于 2013-02-16 20:54:31

您需要使用您的自定义光标适配器,因为您可以动态地更改内容

这里我给出一个例子,在bindView方法中改变文本的颜色

代码语言:javascript
运行
复制
public class MessageAdapter extends CursorAdapter {
private Cursor mCursor;
private Context mContext;
private final LayoutInflater mInflater;


public MessageAdapter(Context context, Cursor c) {
    super(context, c);
    mInflater=LayoutInflater.from(context);
mContext=context;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {

    TextView mobileNo=(TextView)view.findViewById(R.id.mobileNolistitem);
    mobileNo.setText(cursor.getString(cursor.getColumnIndex(TextMeDBAdapter.KEY_MOBILENO)));

    TextView frequency=(TextView)view.findViewById(R.id.frequencylistitem);
    frequency.setText(cursor.getString(cursor.getColumnIndex(TextMeDBAdapter.KEY_FREQUENCY)));

    TextView rowid=(TextView)view.findViewById(R.id.rowidlistitem);
    rowid.setText(cursor.getString(cursor.getColumnIndex(TextMeDBAdapter.KEY_ID)));

}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    final View view=mInflater.inflate(R.layout.message_list_item,parent,false); 
    return view;
}

}

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14910390

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档