更改listview项的背景颜色,从循环中获取索引

时间:2015-11-27 13:35:58

标签: android listview android-listview background-color

我想改变我的listview项目的Backgroundcolor,但是当我运行它时不会改变并显示找不到索引。 在这里我还检查了最后一个if(sts.equals(“true”))条件是否匹配,但它也匹配成功只是颜色没有改变,它调用catch部分并显示“获取数据时出错:null”< / p>

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

1 个答案:

答案 0 :(得分: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);
)};

此处:listview item background color change