0
我有一個列出ListFragment中項目的應用程序。每個項目根據項目狀態具有不同的背景顏色。該適配器從SimpleCursorAdapter分類,因爲這些項目存儲在SQL中。這些項目的編輯發生在不同的片段中。當狀態發生變化時,我使用AsyncTask在GUI線程上觸發適配器上的notifyDataSetChanged()。但我的清單並未更新。更新適配器數據更改後ListView項目的背景
我知道我在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
謝謝你,那正是我一直在尋找的。現在它像一個魅力。 – Rben 2013-04-28 11:50:27