2016-07-08 3 views
0

영역 객체가있는 GridView가 있습니다. GridView를 올바르게 채울 수 있습니다. 영역 객체 변수에 따라 항목의 배경색을 변경하는 방법은 잘 못됩니다. realm object stockEntry.VERIFIED가 1이면 배경 색상이 녹색이어야합니다.android gridview 항목 동적 배경색

나는 녹색을 보이게 만들었지 만, view.setBackgroundColor를 변경함으로써 녹색으로 보이지만, 더 많은 항목을 스크롤하면 자동으로 녹색 배경이됩니다!

public class StockEntryAdapter extends BaseAdapter { 
private LayoutInflater inflater; 

private List<StockEntry> stockEntries = null; 

public StockEntryAdapter(Context context) { 
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 

public void setData(List<StockEntry> details) { 
    this.stockEntries = details; 
} 

@Override 
public int getCount() { 
    if (stockEntries == null) { 
     return 0; 
    } 
    return stockEntries.size(); 
} 

@Override 
public Object getItem(int position) { 
    if (stockEntries == null || stockEntries.get(position) == null) { 
     return null; 
    } 
    return stockEntries.get(position); 
} 

@Override 
public long getItemId(int i) { 
    return i; 
} 

@Override 
public View getView(int position, View currentView, ViewGroup parent) { 
    if (currentView == null) { 
     currentView = inflater.inflate(R.layout.stock_entry_listitem, parent, false); 
    } 

    StockEntry stockEntry = stockEntries.get(position); 

    if (stockEntry != null) { 
     TextView textView = (TextView) currentView.findViewById(R.id.itemnmbr); 
     textView.setText(stockEntry.getItemFullName()); 

     if (stockEntry.getVerified() == 1) { 
      // here I need to set the items background colour to green 
     } 
    } 

    return currentView; 
} 

답변

0

모두 @의 comeback4you 및 @EsatIBIS이

currentView.setBackgroundColor(ContextCompat.getColor(context.R.color.yellow)); 
+0

또한, 당신은 너무보기의 배경을 기본 코드로'else' 문을 추가 그래서 재활용되기 때문에 더 많은 녹색 얻고있는 다른 항목을 제어 할 필요가 원래. –

+0

그는 뷰 홀더 클래스를 사용하지 않으므로 else 파트가 필요 없다고 생각합니다. – comeback4you

+0

comeback4you - 내 원래 코드에 매우 가깝습니다. currentView.setBackgroundColor (Color.parseColor ("# A5D6A7")); @EsatIBIS에 의해 문제가 강조되었습니다 - 기본 배경에 다른 것을 가지고 있다고 생각하지 않았습니다. 둘 다 덕분에 효과가있었습니다! – user260582

0

감사를 시도, 이것은 나를 위해 일한 것입니다; 당신은 당신이 원하는 색상을 변경 한 후

public View getView(int position, View currentView, ViewGroup parent) { 
    if (currentView == null) { 
     currentView = inflater.inflate(R.layout.stock_entry_listitem, parent, false); 
    } 

    StockEntry stockEntry = stockEntries.get(position); 

    if (stockEntry != null) { 
     TextView textView = (TextView) currentView.findViewById(R.id.itemnmbr); 
     textView.setText(stockEntry.getItemFullName()); 
     if (stockEntry.getVERIFIED() == 1) { 
      currentView.setBackgroundColor(Color.parseColor("#A5D6A7")); 
      if (stockEntry.getVARIANCECOST() > 100 || stockEntry.getVARIANCECOST() < -100) { 
       currentView.setBackgroundColor(Color.parseColor("#EF9A9A")); 
      } 
     } else { 
      currentView.setBackgroundColor(Color.WHITE); 
     } 
    } 

    return currentView; 
}