Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.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 适配器数据更改时更新ListView项的背景_Android_Adapter_Android Listfragment_Ui Thread_Notifydatasetchanged - Fatal编程技术网

Android 适配器数据更改时更新ListView项的背景

Android 适配器数据更改时更新ListView项的背景,android,adapter,android-listfragment,ui-thread,notifydatasetchanged,Android,Adapter,Android Listfragment,Ui Thread,Notifydatasetchanged,我有一个在ListFragment中列出项目的应用程序。根据项目状态,每个项目都有不同的背景色。适配器是从SimpleCursorAdapter子类化的,因为这些项存储在SQL中。这些项目的编辑在不同的片段中进行。当状态更改时,我使用AsyncTask在GUI线程上的适配器上触发notifyDataSetChanged()。但我的名单没有更新 我知道我在GUI线程上,因为为了检查,我重写了notifyDataSetChanged类。同样的痕迹也告诉我,我已经达到了常规 @Override pub

我有一个在ListFragment中列出项目的应用程序。根据项目状态,每个项目都有不同的背景色。适配器是从SimpleCursorAdapter子类化的,因为这些项存储在SQL中。这些项目的编辑在不同的片段中进行。当状态更改时,我使用AsyncTask在GUI线程上的适配器上触发notifyDataSetChanged()。但我的名单没有更新

我知道我在GUI线程上,因为为了检查,我重写了notifyDataSetChanged类。同样的痕迹也告诉我,我已经达到了常规

@Override
public void notifyDataSetChanged() {
    String tag = TAG + ".notifyDataSetChanged()";
    Log.d(tag,"DATA SET CHANGE NOTIFICATION!");
    if (Looper.getMainLooper().getThread() == Thread.currentThread()) {
        Log.d(tag,"On GUI thread!");
    } else {
        Log.d(tag,"NOT on GUI thread!");
    }
    super.notifyDataSetChanged();
}
如果有任何建议,我将不胜感激。谢谢你的时间和兴趣

以下是整个适配器:

public class OrderListAdapter extends SimpleCursorAdapter {

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

    /**
     * Constructor
     * @param context - Context
     * @param layout - Layout
     * @param c - Cursor
     * @param from - String array with column names
     * @param to - Int array with destination field ids
     * @param flags - Integer with flags
     */
    public OrderListAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to, int flags) {
        super(context, layout, c, from, to, flags);
        _context = context;
    }

    /**
     * This override version changes the color of the background of the
     * row based on the order status.
     */
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        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) {
        Cursor cursor = getCursor();
        int col = cursor
                .getColumnIndex(DBContract.DeliveryOrderTable.ENROUTE_FLAG);
        String enroute_flag = cursor.getString(col);
        col = cursor
                .getColumnIndex(DBContract.DeliveryOrderTable.DELIVERED_DATETIME);
        String deliveredDateStr = cursor.getString(col);
        int bgColorId = 0;
        if (!deliveredDateStr.equals("")) {
            bgColorId = R.color.bg_status_delivered_color;
        } else if (enroute_flag.startsWith("t") || enroute_flag.startsWith("y")) {
            bgColorId = R.color.bg_status_enroute_color;
        } else {
            bgColorId = R.color.bg_status_assigned_color;
        }
        view.setBackgroundColor(_context.getResources().getColor(bgColorId));

    } // setRowColor()

    ///// DEBUG

    @Override
    public void notifyDataSetChanged() {
        String tag = TAG + ".notifyDataSetChanged()";
        Log.d(tag,"DATA SET CHANGE NOTIFICATION!");
        if (Looper.getMainLooper().getThread() == Thread.currentThread()) {
            Log.d(tag,"On GUI thread!");
        } else {
            Log.d(tag,"NOT on GUI thread!");
        }
        super.notifyDataSetChanged();
    }

} // class

数据更改时,必须使用适配器.changeCursor更新光标。看到这个答案:

谢谢,这正是我想要的。现在它就像一个符咒。