2

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);
    }
sagar
  • 1,900
  • 5
  • 30
  • 45

2 Answers2

3

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);
}
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
  • Thanks for ur answer,but it shows me nullPointerexception at line listView.getChildAt(position).setBackgroundColor(Color.BLACK); – sagar Oct 07 '11 at 10:03
  • Ok. How are you calling this method? Show me that line. And you are extending ListActivity or Activity? – Pankaj Kumar Oct 07 '11 at 10:06
  • extending ListActivity. i am calling function same as u shown,iam using getSelectetItemPosition & getListView function to get position & Listview , these functions r called in onContextItemSelected function befor switch case loop. data source for listview is 1 sqlite table – sagar Oct 07 '11 at 10:19
  • no, position value is returning as -1. is it possible that i can use id instead of position? – sagar Oct 07 '11 at 10:40
  • No, this is the position of row at you long pressed. I am updating my answer how to read long pressed row position. Wait... – Pankaj Kumar Oct 07 '11 at 10:59
  • sorry,I cant because it requires at least 15 reputation, mine is only 11 – sagar Oct 07 '11 at 18:50
0

refer to this tutorial Colored Row in List View

RoRo
  • 118
  • 9