如何更改使用SimpleCursorAdapter特定的行背景 [英] How to change backgrounds for specific rows using SimpleCursorAdapter

查看:282
本文介绍了如何更改使用SimpleCursorAdapter特定的行背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个从信息产生了我的SQLite数据库的列表视图。我现在有code设置只是相应地显示每一行和填充3 textviews数据。我希望能够使用某种如果语句来检查,看看是否值之一是室1,然后设置该行的背景有一定的颜色。

此方式列表中的每个行将会基于什么房间有不同的背景数据是用于

我曾尝试延长SimpleCursorAdapter,但我有点迷失如何得到我想要出来的东西。

下面是我目前的SimpleCursorAdapter:

 光标notesCursor = myDbHelper.fetchDayPanelNotes(类型,日);
startManagingCursor(notesCursor);
的String [] =由新的String [] {DataBaseHelper.KEY_NAME,DataBaseHelper.KEY_ROOM,DataBaseHelper.KEY_TIME};INT []为= INT新[] {R.id.text1,R.id.text2,R.id.text3};SimpleCursorAdapter指出=新SimpleCursorAdapter(这一点,R.layout.main_row,notesCursor,从,到);
setListAdapter(注);


解决方案

我会建议延长 BaseAdapter ,手动填充您的列表数据,并保持数据的本地副本您的适配器类。然后,当你实施 getView(...)方法,你可以使用查看的位置或ID 你再生成,以获取相应的数据对证室1,并修改查看返回基于比较。

由rekaszeru的回答完全正确的想法,但它听起来像是你将需要比的位置和标识,这就是为什么我推荐一个较低的水平和执行更多信息 BaseAdapter 直接

I'm trying to create a listview generated from information out of my SQLite DB. I currently have the code set up to just display each row accordingly and populate 3 textviews with data. I would like to be able to use some sort of If statement to check to see if one of values is "Room1" and then set the background of that row to a certain color.

This way each row in the list will have a different background based on what "room" the data is for.

I have tried extending SimpleCursorAdapter but I am a bit lost on how to get what I want out of it.

Here is what I have currently for the SimpleCursorAdapter:

Cursor notesCursor = myDbHelper.fetchDayPanelNotes(type, day);
startManagingCursor(notesCursor);
String[] from = new String[]{DataBaseHelper.KEY_NAME, DataBaseHelper.KEY_ROOM, DataBaseHelper.KEY_TIME};

int[] to = new int[]{R.id.text1, R.id.text2, R.id.text3};

SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.main_row, notesCursor, from, to);
setListAdapter(notes);

解决方案

I would recommend extending BaseAdapter, populating your list data manually and keeping a local copy of that data in your adapter class. Then when you implement the getView(...) method you can use the position or ID of the View you're generating to fetch the corresponding data to check against "Room1," and alter the View you return based on that comparison.

The answer by rekaszeru has exactly the right idea, but it sounds like you'll need more information than the position and ID, which is why I recommend going a level lower and implementing BaseAdapter directly.

这篇关于如何更改使用SimpleCursorAdapter特定的行背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆