Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何根据每个项目中显示的数据更改列表项目的背景色?_Android_Listview_Background Color_Simplecursoradapter - Fatal编程技术网

Android 如何根据每个项目中显示的数据更改列表项目的背景色?

Android 如何根据每个项目中显示的数据更改列表项目的背景色?,android,listview,background-color,simplecursoradapter,Android,Listview,Background Color,Simplecursoradapter,我在SQLite中有一个状态不同的订单列表:已分配、已加载、已交付。我希望在列表中显示的每个订单都有不同颜色的背景。到目前为止,我还没有找到一个好的方法 关于如何根据位置更改列表项的背景色,我发现了很多讨论,但没有一个是基于数据内容的。我还发现了很多关于如何更改用于突出显示选中项目的颜色的讨论。这些对我没有帮助 我提出的解决问题的唯一方法包括在适配器创建整个列表之后,运行整个列表,并为每个项目设置背景。这既麻烦又浪费。我希望有一种更有效的方法,可以在从光标创建列表时在适配器中更改背景 我相信有更

我在SQLite中有一个状态不同的订单列表:已分配、已加载、已交付。我希望在列表中显示的每个订单都有不同颜色的背景。到目前为止,我还没有找到一个好的方法

关于如何根据位置更改列表项的背景色,我发现了很多讨论,但没有一个是基于数据内容的。我还发现了很多关于如何更改用于突出显示选中项目的颜色的讨论。这些对我没有帮助

我提出的解决问题的唯一方法包括在适配器创建整个列表之后,运行整个列表,并为每个项目设置背景。这既麻烦又浪费。我希望有一种更有效的方法,可以在从光标创建列表时在适配器中更改背景

我相信有更好的办法。我对安卓系统来说太陌生了,根本不了解它


我非常感谢到目前为止的答复。我正在尽我最大的努力把它们结合起来,但我仍然没有成功。这是我刚刚尝试的,基于我得到的答案和我所做的研究

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(),显然,字段中就没有数据了,因为我没有显式地进行传输,但我想我可以修改返回的视图

我的轨迹显示我正在读取数据,但背景颜色没有改变

我试图更改的视图似乎是线性布局,但更改背景色似乎不起作用


明白了!确保所有子视图的背景都是透明的。

我想说的是,尝试扩展CursorAdapter,以便将数据库与ListView绑定。然后可以重写ListView.dispatchDraw()以自定义绘制对象

或者,检查以下内容可能会有所帮助:


它根据天气状况使用不同的图像。移植到您的问题上,您可以使用9-patch或编程创建的可绘制图形作为背景,而不是在Paint中更改内容。

如果您正在使用listview的任何自定义适配器,那么您将拥有一个方法getView(),在返回之前只需调用一个方法,然后传递视图(正在返回)和数据,具体取决于要更改行的颜色

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

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

可能与我查看的数据类型重复。但它似乎只是根据视图所在的位置而不是行中的数据来更改颜色。它确实给了我一个我正在研究的想法,所以它可能会带来一个解决方案,但这并不是我所需要的。我还在研究另一个可能的解决方案。只需了解更多适配器的工作原理。重要的细节,请记住将所有子视图的背景设置为透明!这很有帮助。我似乎大部分时间都在那里,但由于某种原因,当我改变视图的背景色时,它不需要。这也很有帮助。非常感谢。