如何更改列表视图中每一行的背景颜色? [英] How to change background color of each row in list view?
本文介绍了如何更改列表视图中每一行的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的应用程序包含 1 个列表视图,数据源是 1 个 sqlite 表,当我长按列表视图中的任何行时,它将显示 1 个菜单选项来更改该行的颜色,为此我使用了 onContextItemSelected 函数,在选择菜单选项时,它将调用 1 个函数 change_color.我应该在 change_color 函数中写什么,以便我可以更改行背景颜色.
My app contains 1 list view, data source is 1 sqlite table, when i hold long click on any row in listview it will show me 1 menu option to change the color of that row, for this i have used onContextItemSelected function, on selecting menu option it will call 1 function change_color. What should i write in change_color function so that i can change row bg color.
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, PROCESSED_ID, 0, R.string.menu_processed);
}
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case PROCESSED_ID:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
change_color();
return true;
}
return super.onContextItemSelected(item);
}
推荐答案
Call your method as :
change_color(pass_your_list_view, pass_selected_position_of_list_view);
并将 change_color() 定义为:
And define change_color() as:
private void change_color(ListView listView, int position) {
listView.getChildAt(position).setBackgroundColor(Color.BLACK);
}
希望这会有所帮助.
已编辑
定义一个变量一个位置
public static int position;
并将您的代码替换为
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, PROCESSED_ID, 0, R.string.menu_processed);
// Get the info on which item was selected
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
// Retrieve the position at where you long pressed
position = info.position;
}
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case PROCESSED_ID:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
change_color(getListView(), position);
return true;
}
return super.onContextItemSelected(item);
}
这篇关于如何更改列表视图中每一行的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文