问题描述
我试图更改特定项目的背景颜色在的ListView
。
I've tried to change the background color of specific items in a ListView
.
第一,从数据库中抓住它:
first, catch it from database:
ListAdapter adapter = new ArrayAdapter(getApplicationContext(),
android.R.layout.simple_list_item_1, db.getAllApps());
final ListView list = (ListView) findViewById(R.id.ListViewApps);
list.setAdapter(adapter);
然后我将在不同颜色的所有应用程序,如果他们有标签的激活
// if app is activated in db --> set another colour in ListView
private void setAppCheck(ListView list) {
List<String> apps = db.getAllApps();
for (int i = 0; i < list.getCount(); i++) {
if (db.appActivated(apps.get(i)).equals("activated")) {
list.setBackgroundColor(0xffaaaaaa); // it changes ALL items...
} else {
// do nothing
}
}
}
和存在的问题,以 list.setItemChecked(我,真)
我可以用一个特定的位置改变它,但我要如何改变特定的背景颜色在 ListView项
And there is Problem, with list.setItemChecked(i, true)
I can change it with a specific position, but how do I change the Background color of the specific Item in the ListView
?
希望你能帮助我。
推荐答案
做你试图做的是写自己的支持两个视图类型:启动应用程序和应用程序关闭。然后在你的 getView
方法,当你膨胀您的看法,您可以相应地设置背景颜色。
The cleanest way to do what you're trying to do is writing your own CursorAdapter supporting two view types: activated apps and deactivated apps. Then in your getView
method, when you're inflating your views, you can set the background color accordingly.
有两个项目类型将自动进行Android框架只传递观点的正确类型的转换为 getView
,所以唯一的时候,你需要检查的类型是中创建。
Having two item types will make the Android framework automatically pass only convert views of the correct type to getView
, so the only time you need to check for the type is during creation.
您可能会发现this回答有帮助的。
You may find this answer helpful.
在Android中,适配器
,则用来翻译(在你的情况下,从SQLite数据库),您的数据转换成查看
可以显示在列表视图,纺纱等(适配器视图
来具体)秒。其中最常用的就是的CursorAdapter
有当相关的数据应该是从游标读取必要的基础设施。
In Android, Adapter
s are used to translate your data (in your case from an SQLite database) into View
s that can be displayed in listviews, spinners, etc. (AdapterView
to be specific). One of the most commonly used ones is the CursorAdapter
which has basic infrastructure necessary when the associated data is supposed to be read from a cursor.
您将主要需要适配器三种方法:结果
- getViewTypeCount
这将告诉框架多少的类型的意见适配器知道。对于您这将是二:启动和关闭应用
You will mainly need three methods in your adapter:
- getViewTypeCount
which will tell the framework how many types of views your adapter knows. For you this will be two: activated and deactivated apps.
-
getItemViewType
,当数据(这里:光标)通过一个特定的位置,能够决定哪些该位置属于这些类型的。对于这一点,你将有可能能够重用你的db.appActivated
code,至少在大的部分。
getItemViewType
which, when passed a specific position in the data (here: the cursor), is able to decide which of those types that position falls into. For this, you will likely be able to reuse yourdb.appActivated
code, at least in large parts.
getView
,其中,当传递的位置,可以将与位置成查看相关的数据
显示。让我们来看看更深入的最后一部分。
getView
, which, when passed a position, can turn the data associated with that position into a View
for display. Let's look at that last part in more depth.
Android的做一些非常漂亮的东西,以确保您的应用程序是快速,光滑和响应。其中的一个事情是,它只会保留周围列表中的所有位置都显示了足够的意见。所以,如果你有一个可以一次显示10个项目清单,但您的数据保存一百万条记录,它仍然只保持周围10次(当然,实际上,从当东西滚动关闭屏幕,但绝对是一个几的不的一百万它需要为每个数据记录)。
Android does some very nifty stuff to make sure your app is fast and slick and responsive. One of those things is, it will only keep enough views around for all positions in the list that are displayed. So, if you have a list that can display 10 items at a time, but your data holds a million records, it will still only keep 10 views around (well, actually, a few more from when stuff is scrolled off screen, but definitely not the one million it would require for every data record).
到时候真正将数据转化为可见光重新presentations - getView
- 它会通过一个老,previously可见,但现在关闭屏幕视图作为你的 convertView
参数,试图适应它来显示一个已经requeusted的数据(循环视图)。这是因为新的充气看法较为不仅仅是考虑现有的和不断变化的文本或图像或者其他更昂贵。你告诉它大概将有助于它的视图类型只能通过视图转换成 getView
的类型适合的被请求的位置。
When the time comes to actually turn data into visible representations - getView
- it will pass an old, previously visible but now off screen view (a recycled view) in as the convertView
parameter for you to try adapting it to display the data that's been requeusted. This is because inflating new views is comparatively more expensive than just taking an existing one and changing its texts or images or whatever. The view types you have told it about will help it to only pass the type of convert view into getView
that is appropriate for the position that's been requested.
这样,你需要的,如果传递的转换的观点是不合适莫名其妙地只吹一个新的观点。和不合适,在这种情况下,通常只意味着,如果它是空。因此,通常情况下,你最终得到的东西非常接近这样的:
This way, you need to only inflate a new view if the passed convert view is inappropriate somehow. And inappropriate, in this case, usually only means "if it is null". So, usually, what you end up with is something very close to this:
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = // inflate a new view
}
// bind the convert view to the data, i.e. set its text views, images, and - in your case - background color
}
的视频说,超过一千字
您可能想要观看了多个COM $的对$ phensive解释这一切是如何联系在了一起。
A video says more than a thousand words
You may want to watch this Google I/O keynote for a more comprehensive explanation of how it all ties together.
这篇关于更改项目的颜色列表视图(不点击)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!