簡體   English   中英

有條件地更改列表視圖行的背景顏色

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

我試圖基於參數和行View v = super.getView(position, convertView, parent)更改列表視圖的背景行顏色View v = super.getView(position, convertView, parent)其給我的錯誤是“無法直接調用抽象方法getView(int ,“視圖”,“視圖組”)。 這是所有代碼。

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

更改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;  
}

就像錯誤提示您無法執行此操作一樣

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

相反,你應該做

View v = convertView;

然后檢查v是否為空

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

也許我對您的問題不太了解,但與此相反

item.status == "green"

用這個

item.status.equals(“綠色”)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM