简体   繁体   English

有条件地更改列表视图行的背景颜色

[英]Changing the background color of a listview row conditionally

I'm trying to change the background row color of a list view based on a parameter and on the line View v = super.getView(position, convertView, parent) its giving me the error "Cannot directly invoke the abstract method getView(int, View, ViewGroup) for the type Adapter". 我试图基于参数和行View v = super.getView(position, convertView, parent)更改列表视图的背景行颜色View v = super.getView(position, convertView, parent)其给我的错误是“无法直接调用抽象方法getView(int ,“视图”,“视图组”)。 Here is the all of the code. 这是所有代码。

public class CustomListViewAdapter extends BaseAdapter
{  

LayoutInflater inflater;
List<ListViewItem> items;

public CustomListViewAdapter(Activity context, List<ListViewItem> items) {  
    super();

    this.items = items;
    this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override  
public int getCount() {  
    // TODO Auto-generated method stub  
    return items.size();  
}  

@Override  
public Object getItem(int position) {  
    // TODO Auto-generated method stub  
    return null;  
}  

@Override  
public long getItemId(int position) {  
    // TODO Auto-generated method stub  
    return 0;  
}

@Override  
public View getView(final int position, View convertView, ViewGroup parent) {  
    View v = super.getView(position, convertView, parent); 

    ListViewItem item = items.get(position);

    if(convertView==null)
        v = inflater.inflate(R.layout.my_hospitals, null);
        TextView status = (TextView) v.findViewById(R.id.status);
        TextView name = (TextView) v.findViewById(R.id.name);
        TextView table = (TextView) v.findViewById(R.id.id);
        TextView info = (TextView) v.findViewById(R.id.info);

        status.setText(item.status);
        name.setText(item.name);
        table.setText(item.table);
        info.setText(item.info);

        if (item.status == "green"){
            v.setBackgroundColor(Color.GREEN);
        }

    return v;  
}
}

Change getView() 更改getView()

@Override  
public View getView(final int position, View convertView, ViewGroup parent) {  
    View v=convertView;
    ListViewItem item = items.get(position);

    if(v==null){
        v = inflater.inflate(R.layout.my_hospitals, null);
    }
        TextView status = (TextView) v.findViewById(R.id.status);
        TextView name = (TextView) v.findViewById(R.id.name);
        TextView table = (TextView) v.findViewById(R.id.id);
        TextView info = (TextView) v.findViewById(R.id.info);

        status.setText(item.status);
        name.setText(item.name);
        table.setText(item.table);
        info.setText(item.info);     
        String state=tem.status;


        if (state.equals("green"){
            v.setBackgroundColor(Color.GREEN);
        }

    return v;  
}

well like the error says you cannot do this 就像错误提示您无法执行此操作一样

View v = super.getView(position, convertView, parent);

instead you should be doing 相反,你应该做

View v = convertView;

then check if v is null 然后检查v是否为空

if(v == null){
    //get the view
}

Maybe i didn't understand well your question but instead of this 也许我对您的问题不太了解,但与此相反

item.status == "green"

Use this 用这个

item.status.equals("green") item.status.equals(“绿色”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM