How to change background color of each row in list view?

后端 未结 2 1707
滥情空心
滥情空心 2020-12-10 21:21

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

相关标签:
2条回答
  • 2020-12-10 21:58

    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);
    }
    
    0 讨论(0)
  • 2020-12-10 22:11

    refer to this tutorial Colored Row in List View

    0 讨论(0)
提交回复
热议问题