如何更改列表视图中每一行的背景颜色?

发布于 2024-12-09 00:54:04 字数 779 浏览 1 评论 0原文

我的应用程序包含 1 个列表视图,数据源是 1 个 sqlite 表,当我长按列表视图中的任何行时,它将显示 1 个菜单选项来更改该行的颜色,为此我在选择菜单时使用了 onContextItemSelected 函数选项它将调用 1 个函数change_color。我应该在change_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);
    }

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);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伴我心暖 2024-12-16 00:54:04

将您的方法调用为:

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);
}

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);
}
℉絮湮 2024-12-16 00:54:04

请参阅本教程 列表视图中的彩色行

refer to this tutorial Colored Row in List View

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文