[英]listview each item dynamically change background color
I have more specific case than just change background color. 我有更具体的案例,而不仅仅是更改背景颜色。
I have 1 listview, in every listview item I have 2 textviews. 我有1个列表视图,在每个列表视图项目中我都有2个文本视图。 All information I am showing in those listview items textviews is coming from server.
我在这些listview项目textviews中显示的所有信息都来自服务器。
One parameter I am getting is color code (eg. #138F6A). 我得到的一个参数是颜色代码(例如#138F6A)。 Now, I need to take this color code (I am inserting as a value in textview) and change this listview item specific textview background color according to color code value I have from server.
现在,我需要使用此颜色代码(我将其作为值插入到textview中),并根据我从服务器获得的颜色代码值来更改此listview项目特定的textview背景颜色。
How can I do that? 我怎样才能做到这一点? How can I set colorView textview background as color I get from server for this listview item?
如何将colorView textview背景设置为从服务器获取的该listview项目的颜色?
public void OnCreateActivity() {
...
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser objektinimi_parser = new XMLParser();
...
// looping through all item nodes <item>
for (int i = 0; i < ndlist.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element myelem = (Element) ndlist.item(i);
String bronID = objektinimi_parser.getValue(myelem, "BRON_ID");
String color= objektinimi_parser.getValue(myelem, "COLOR");
map.put(BRON_ID, bronID);
map.put(COLOR, color);
menuItems.add(map);
}
ListAdapter adapter = new SimpleAdapter (this, menuItems,
R.layout.kirjed,
new String[] { BRON_ID, COLOR },
new int[] { R.id.bronid, R.id.colorView });
setListAdapter(adapter);
ListView lv = getListView();
Thank you! 谢谢!
You can extend the adapter and set the background colour in the getView
method: 您可以扩展适配器并在
getView
方法中设置背景颜色:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
view.setBackgroundColor(theColour);
return view;
}
This will work fine if you don't need press states etc. 如果您不需要按状态等,这将很好地工作。
If you do, you'd need to use a ColorStateList
and setBackgroundResource
instead, see: http://developer.android.com/reference/android/content/res/ColorStateList.html 如果这样做,则需要使用
ColorStateList
和setBackgroundResource
代替,请参阅: http : //developer.android.com/reference/android/content/res/ColorStateList.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.