2014-09-30 129 views
1

每次我改變物品背景顏色當我滾動隨機物品時也改變背景顏色,我不知道爲什麼 另一件事情發生了是,如果該項目沒有在屏幕上顯示它並沒有改變背景顏色每當我滾動隨機物品時改變項目背景顏色也改變背景顏色

這裏是我的代碼:

public class MyActivity extends ListActivity { 

char[] items = { 'e', 'f', 'f', 'f', 'e', 'f', 'e', 'e', 'f', 'f', 'e', 'e', 'f', 'f', 'f', 'e', 'e', 'f', 'f', 'e', 'e', 'f', 'f', 'f', 'e', 'e', 'f', 'f', 'e', 'e', 'f', 'f', 'f', 'e', 'e', 'f', 'f', 'p', 'e', 'e', 'p', 'f', 'f', 'f', 'e', 'e', 'a', 'f', 'p', 'f', 'e', 'e', 'f', 'f', 'p', 'f', 'e', 'a', 'e', 'p', 'f', 'f', 'e', 'p', 'e', 'f', 'f', 'f', 'a', 'p', 'e', 'e', 'p', 'f', 'f', 'e', 'p', 'e', 'f', 'p', 'f', 'e', 'f', 'e', 'f' }; 
int[] horas = {00, 01, 03, 04, 04, 05, 05, 05, 06, 06, 06, 06, 07, 07, 07, 07, 8, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 11, 11, 11, 11, 11, 12, 12, 12, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 21, 21, 21, 22, 22, 23, 23,}; 
int[] minutos = {20, 00, 00, 00, 30, 10, 25, 40, 10, 20, 40, 50, 00, 10, 30, 50, 00, 20, 40, 00, 10, 20, 30, 50, 10, 20, 40, 00, 20, 30, 40, 50, 10, 30, 40, 10, 20, 30, 40, 50, 00, 00, 10, 30, 50, 00, 10, 20, 30, 40, 00, 10, 30, 50, 50, 00, 10, 20, 20, 30, 40, 00, 20, 30, 30, 50, 10, 20, 20, 30, 30, 40, 50, 00, 20, 40, 50, 50, 10, 15, 30, 00, 40, 10, 50}; 
String[] total = new String[horas.length]; 

// declare class variables 
private ArrayList<Item> m_parts = new ArrayList<Item>(); 
private Runnable viewParts; 
private ItemAdapter m_adapter; 

/** Called when the activity is first created. */ 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_my); 

    for (int i = 0; i < items.length; i++) { 
     total[i] = String.format("%02d",horas[i]) + ":" + String.format("%02d",minutos[i]); 
    } 

    int cuantos_f = cuantosF('f'); 

    // instantiate our ItemAdapter class 
    m_adapter = new ItemAdapter(this, R.layout.row, m_parts); 

    for (int i = 0; i < items.length; i++) { 
     m_parts.add(new Item(horas[i], minutos[i], items[i], total[i])); 
    } 

    m_adapter = new ItemAdapter(MyActivity.this, R.layout.row, m_parts); 

    // display the list. 
    setListAdapter(m_adapter); 

    final Button prox = (Button) findViewById(R.id.prox); 
    prox.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      mostrarSiguiente(); 
     } 
    }); 

    final Handler mHandler = new Handler(); 


    new Thread(new Runnable() { 
     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      while (true) { 
       try { 
        Thread.sleep(1000); 
        mHandler.post(new Runnable() { 

         @Override 
         public void run() { 
          // TODO Auto-generated method stub 
          // Write your code here to update the UI. 
          m_adapter.notifyDataSetChanged(); 
         } 
        }); 
       } catch (Exception e) { 
        // TODO: handle exception 
       } 
      } 
     } 
    }).start(); 

} 

private int cuantosF(char mi_char) { 
    int cant = 0; 
    for (int i = 0; i < horas.length; i++) { 
     if(items[i] == mi_char){ 
      cant++; 
     } 
    } 
    return cant; 
} 

private void mostrarSiguiente() { 
    Calendar rightNow = Calendar.getInstance(); 
    int hour = rightNow.get(Calendar.HOUR_OF_DAY); 
    int min = rightNow.get(Calendar.MINUTE); 

    int mejor_pos = 999, menor_resta = 1500; 

    for (int i = 0; i < horas.length; i++) { 
     if((horas[i]*60 + minutos[i] - (hour*60+min)) > 0){ 
      if ((horas[i]*60 + minutos[i] - (hour*60+min)) < menor_resta){ 
       mejor_pos = i; 
       menor_resta = (horas[i]*60 + minutos[i] - (hour*60+min)); 
      } 
     } 
    } 

    ChangeColors(mejor_pos); 

} 

void ChangeColors(final int numero){ 
    getListView().smoothScrollToPosition(numero); 

    ListView lv = getListView(); 
    lv.setCacheColorHint(0); 
    final View view = getViewByPosition(numero, lv); 

    changeColor(numero,view); 


} 
void changeColor (int numero, View v){ 
    v.setBackgroundColor(Color.parseColor("#3a536b")); 
} 
public View getViewByPosition(int position, ListView listView) { 
    final int firstListItemPosition = listView.getFirstVisiblePosition(); 
    final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1; 

    if (position < firstListItemPosition || position > lastListItemPosition) { 
     return listView.getAdapter().getView(position, null, listView); 
    } else { 
     final int childIndex = position - firstListItemPosition; 
     return listView.getChildAt(childIndex); 
    } 
} 
} 

在先進的感謝!

+1

ListViews將爲子對象重用視圖,這就是爲什麼你會看到「隨機」視圖上的顏色 – panini 2014-09-30 23:28:40

+0

@panini以及我如何改變項目的顏色? – user3739503 2014-09-30 23:47:20

+0

您需要存儲每個孩子應該按位置的顏色,然後在獲取查看功能中設置顏色。 – 2014-10-01 00:59:38

回答