我的应用程序包含1个列表视图,数据源是1个sqlite表,当我长按listview中的任意行时,它会显示1个菜单选项来更改该行的颜色,为此我使用了onContextItemSelected函数,在选择菜单选项时,它将调用1个函数change_color。我应该在change_color函数中写什么,以便我可以改变行bg颜色。
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);
}
答案 0 :(得分:3)
将您的方法称为:
change_color(pass_your_list_view, pass_selected_position_of_list_view);
将change_color()定义为:
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);
}
答案 1 :(得分:0)