如何更改基于所述数据列表中的项目的背景颜色被显示在各个项目?

问题描述:

我在SQLite的订单这状态而异的列表:分配,装载,交付。我想对每个那些命令,当在列表中显示的,具有不同颜色的背景。到目前为止,我还没有找到一个很好的办法做到这一点。

I have a list of orders in SQLite which vary in status: assigned, loaded, delivered. I'd like for each of those orders, when displayed in the list, to have a different colored background. So far, I haven't found a good way to do this.

我找到了很多关于如何更改的基于位置列表项的背景色的讨论,但没有基于数据的内容。我还发现很多关于如何更改用于突出所选项目颜色的讨论。这些不帮我。

I've found plenty of discussions on how to change the background color of list items based on the position, but none based on data content. I've also found lots of discussions on how to change the color that's used to highlight an item that is selected. These don't help me.

唯一的方法,我想出了解决我的问题涉及到整个列表上运行,它已经由适配器创建后,并设置每个项目的背景。它的缺憾和浪费。我希望有,将让背景在适配器被改变为被从光标创建的列表的更有效的方法。

The only methods I come up with for solving my problem involve running through the entire list, after it's been created by the adapter, and setting the background on each item. It's kludgy and wasteful. I'm hoping there's a more efficient method that would let the background be changed in the adapter as the list is being created from the cursor.

我敢肯定有一个更好的办法。我只是太新到Android知道这一点。

I'm sure there's a better way. I'm just too new to Android to know it.

我真的AP preciate答复迄今。我尽我所能将其纳入,但我仍然没有成功。下面是我刚试过的基础上,我已经得到了答案,我已经做了研究。

I really appreciate the responses so far. I'm doing my best to incorporate them, but I'm still not having success. Here's what I've just tried, based on the answers I've gotten and the research I've done.

public class OrderListAdapter extends SimpleCursorAdapter {

    private static final String TAG = "SimpleCursorAdapter";
    Context _context = null;
    int layoutResourceId = 0;

    public OrderListAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to, int flags) {
        super(context, layout, c, from, to, flags);
        _context = context;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        String tag = TAG + ".getView()";
        Log.d(tag,"in getView()");

        if (view == null) {
            LayoutInflater inflater = ((Activity)_context).getLayoutInflater();
            view = inflater.inflate(R.layout.fragment_order_list_row, null);
        }
        setRowColor(view);
        return view;
    }

    private void setRowColor(View view) {
        String tag = TAG + ".setRowColor()";
        Cursor cursor = getCursor();
        int col = cursor
                .getColumnIndex(DBContract.DeliveryOrderTable.ENROUTE_FLAG);
        String enroute_flag = cursor.getString(col);
        Log.d(tag, "enroute_flag = [" + enroute_flag + "]");
        col = cursor
                .getColumnIndex(DBContract.DeliveryOrderTable.DELIVERED_DATETIME);
        String deliveredDateStr = cursor.getString(col);
        Log.d(tag, "deliveredDateStr = [" + deliveredDateStr + "]");
        int bgColorId = 0;
        if (!deliveredDateStr.equals("")) {
            bgColorId = R.color.bg_status_delivered_color;
            Log.d(tag, "Setting to delivered color");
        } else if (enroute_flag.startsWith("T") || enroute_flag.startsWith("Y")) {
            Log.d(tag, "Setting to enroute color");
            bgColorId = R.color.bg_status_enroute_color;
        } else {
            Log.d(tag, "Setting to assigned color");
            bgColorId = R.color.bg_status_assigned_color;
        }
        view.setBackgroundColor(_context.getResources().getColor(bgColorId));
    }
}

不幸的是,这是行不通的。如果我没有拨打电话到super.getView(),我风与在地里没有数据,很明显,因为我并没有明确做出转让,但我想我可以只修改返回的视图。

Unfortunately, this doesn't work. If I don't make the call to super.getView(), I wind up with no data in the fields, obviously, since I don't explicitly make the transfers, but I figured I could just modify the returned view.

我的痕迹告诉我,我读的数据,但背景颜色是不会改变。

My traces show me that I am reading the data, but the background color is not changing.

看来,我试图改变视图是的LinearLayout,但改变背景颜色似乎不工作。

It appears that the view I'm trying to change is the LinearLayout, but changing the background color doesn't seem to work.

明白了!确保让所有的孩子的背景看待透明。

Got it! Make sure to make the backgrounds of all the child views transparent.

如果你正在使用的列表视图任何自定义适配器的话,你将有一个方法getView(),在刚刚返回前调用一个方法,并通过查看(这是返回)和数据取决于您要更改的行的颜色。

if you are using any custom adapter for listview then, you will have a method getView(), in that just call a method before returning, and pass the view(which is returning) and data depending on you want to change the color of the row.

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View view = convertView;
    if (view == null) {
        LayoutInflater vi = (LayoutInflater) _c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = vi.inflate(R.layout.list_item_var, null);
    }

    TextView varView = (TextView) view.findViewById(R.id.var);
    TextView valueView = (TextView) view.findViewById(R.id.value);

    VarDetails var = _data.get(position);
    setRowColor(view, var.getVar());
    varView.setText(var.var);
    valueView.setText("Value: " + var.value);

    return view;
}
private void setRowColor(View view, String var) {
    if("assigned".equalsIgnoreCase(var)){
        view.setBackgroundColor(Color.rgb(255,0,0));
    }else if("loaded".equalsIgnoreCase(var)){
        view.setBackgroundColor(Color.rgb(0,255,0));
    }else if("delivered".equalsIgnoreCase(var)){
        view.setBackgroundColor(Color.rgb(0,0,255));
    }
}

请根据您的数据类型更改方法。

please change in method depending on you data type.