问题描述
我的应用程序中包含1列表视图,数据源是1 SQLite表,当我拿着长按在列表视图的任何行会告诉我1菜单选项,以改变该行的颜色,为了这个,我已经使用onContextItemSelected功能,选择菜单选项,它会调用1功能change_color。我应该怎么写change_color功能,这样我可以改变行背景颜色。
公共无效onCreateContextMenu(文本菜单菜单,视图V,
ContextMenuInfo menuInfo){
super.onCreateContextMenu(菜单,V,menuInfo);
menu.add(0,PROCESSED_ID,0,R.string.menu_processed);
}
公共布尔onContextItemSelected(菜单项项){
开关(item.getItemId()){
案例PROCESSED_ID:
AdapterContextMenuInfo信息=(AdapterContextMenuInfo)项目
.getMenuInfo();
change_color();
返回true;
}
返回super.onContextItemSelected(项目);
}
打电话给你的方法:的
change_color(pass_your_list_view,pass_selected_position_of_list_view);
和定义change_color()为:
私人无效change_color(ListView控件的ListView,INT位置){
listView.getChildAt(位置).setBackgroundColor(Color.BLACK);
}
希望这会有所帮助。
编辑的
定义一个变量的位置
公共静态INT位置;
和更换你的code为
公共无效onCreateContextMenu(文本菜单菜单,视图V,
ContextMenuInfo menuInfo){
super.onCreateContextMenu(菜单,V,menuInfo);
menu.add(0,PROCESSED_ID,0,R.string.menu_processed);
//获取有关其项被选定的信息
AdapterContextMenuInfo信息=(AdapterContextMenuInfo)menuInfo;
//获得在您长pressed位置
位置= info.position;
}
公共布尔onContextItemSelected(菜单项项){
开关(item.getItemId()){
案例PROCESSED_ID:
AdapterContextMenuInfo信息=(AdapterContextMenuInfo)项目
.getMenuInfo();
change_color(getListView(),位置);
返回true;
}
返回super.onContextItemSelected(项目);
}
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);
And define change_color() as:
private void change_color(ListView listView, int position) {
listView.getChildAt(position).setBackgroundColor(Color.BLACK);
}
Hope this will help.
Edited
Define a variable a position
public static int position;
And replace your code as
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);
}
这篇关于如何改变在列表视图中的每一行的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!