1

i want to change my listview item's Backgroundcolor but when I run it wont change and showing that index not found. here i also checked that last if(sts.equals("true")) condition is matched or not, but also it matched successfully just color is not changing, it invokes the catch part and showing "Error in fetching data: null"

public void viewTask()
{
    int i=0;
    try{
    Cursor c1 = myDB.rawQuery("SELECT * FROM "+TBName+" where Event_id= '" +Eid+ "'" , null);
    String Data[]  = new String[c1.getCount()+1];
    if (c1 != null) {
        c1.moveToFirst();
        do {
            Tname = c1.getString(c1.getColumnIndex("Task_name"));
            sts= c1.getString(c1.getColumnIndex("Status"));
            Data[i]=Tname;
            item.add(Data[i]);
            if(sts.equals("true"))
            {
                //Toast.makeText(getApplicationContext(),"Match",Toast.LENGTH_LONG).show();
                lvProlist.getChildAt(i).setBackgroundColor(Color.GREEN);
            }
            i++;
        }while(c1.moveToNext());
    }
    }
    catch (Exception e) {
        // TODO: handle exception
        if(i>0){
        Toast.makeText(getBaseContext(),
                "Error in Fetching data: " + e.getMessage(), Toast.LENGTH_LONG).show();
        }
        else {
            Toast.makeText(getBaseContext(),
                    "PLEASE ADD TASK", Toast.LENGTH_LONG).show();
        }
    }
}
Bhoomit_BB
  • 374
  • 4
  • 10
  • lvProlist.getChildAt(i) print this and check its value – Pavya Nov 27 '15 at 13:39
  • Can you post the whole class? – Nigam Patro Nov 27 '15 at 13:42
  • viewTask() is called from the onCreate() method, and i want to change the color at the runtime when activity starts, so it will check that task is completed or not using status, if status true then it could be changed to green background. – Bhoomit_BB Nov 27 '15 at 14:13

1 Answers1

0
public View row;

your_list.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> a, View v,
                    int position, long id) {

if (row != null) {
    row.setBackgroundResource(R.color.orange);
}
row = v;
v.setBackgroundResource(R.color.transparent_green);
)};

Here : listview item background color change

Community
  • 1
  • 1
johnrao07
  • 6,690
  • 4
  • 32
  • 55
  • Thaks for answering, but i've used arraylist with base adapter, so i want to just change the color at runtime, when activity starts, so it check for the status of that task and if task is completed then it'll show it in green background – Bhoomit_BB Nov 27 '15 at 14:10