0

ListFragment의 항목을 나열하는 응용 프로그램이 있습니다. 각 항목은 항목 상태에 따라 다른 배경색을가집니다. 아이템은 SQL에 저장되기 때문에 어댑터는 SimpleCursorAdapter에서 서브 클래 싱됩니다. 이러한 항목의 편집은 다른 부분에서 이루어집니다. 상태가 변경되면 AsyncTask를 사용하여 어댑터의 notifyDataSetChanged()를 GUI 스레드에서 트리거합니다. 하지만 내 목록은 업데이트되지 않습니다.어댑터 데이터가 변경 될 때 ListView 항목의 배경 업데이트

확인하려면 notifyDataSetChanged 클래스를 오버로드했기 때문에 GUI 스레드에 있다는 것을 알고 있습니다. 같은 흔적도 내가 루틴에 도달하고 있다고 말해 준다. :

@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 

답변

1

당신은 adapter.changeCursor 때 데이터 변경을 사용하여 커서를 업데이트 할 수 있습니다

여기에 전체 어댑터입니다. 이 답변을 참조하십시오 : https://stackoverflow.com/a/1986071/198996

+0

고마워요, 제가 정확히 찾고 있었던 것이 었습니다. 이제 그것은 매력처럼 작동합니다. – Rben

관련 문제