2013-02-10 3 views
1

저는 목록보기를 가지고 있으며 SQLite 데이터베이스에서 검색된 일부 조건에 따라보기 (@+id/panel) 색을 변경하려고합니다. 내가 그렇게 SimpleCursorAdapter.ViewBinder을 사용하고SimpleCursorAdapter.ViewBinder를 사용하여보기의 색을 변경하십시오.

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <LinearLayout 
     android:layout_width="10dp" 
     android:layout_height="fill_parent" 
     android:layout_weight="0.01" 
     android:orientation="vertical" > 

     <View 
     android:id="@+id/panel" 
     android:layout_width="10dip" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:background="#ffa500" 
     android:layout_marginRight="4dip" 
     /> 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="2.86" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/textViewB" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 

      android:text="" /> 
    </LinearLayout> 
</LinearLayout> 

는 :

SimpleCursorAdapter.ViewBinder binder = new SimpleCursorAdapter.ViewBinder() { 

@Override 
public boolean setViewValue(View view, Cursor cursor, int columnIndex){  

    if (view.getId() == R.id.panel) 
    {        
     String name = cursor.getString(1); 

     if (name.equals("Ravi")) { 
      view.setBackgroundColor(Color.RED); 
     } else { 
      view.setBackgroundColor(Color.BLUE); 
     } 
     return true; 
    } 
    return false; 
}    

listView.setAdapter(dataAdapter); 
dataAdapter.setViewBinder(binder); 

이 코드는 여전히 작동하지 않습니다. 코드는 절대로 조건을 통과하지 못합니다. TextView 객체 (@+id/textViewB)의 글꼴 색상을 변경할 수는 있지만보기 색상을 변경하는 데 성공하지 못했습니다.

답장을 보내 주셔서 감사합니다.

+0

나머지 색과 다른 색을 원한다면 하나의 행만 있습니까? 또는 여러 행? 한 행만 있으면 도움이 될 수 있습니다. –

+0

여러 행은 조건에 따라 다릅니다. 하지만 한 행에 대한 귀하의 솔루션도보고 싶습니다 – neonerdy

+0

왜 SimpleCursorAdapter를 확장하고 대신 getView를 재정의하지 마십시오. –

답변

0

SimpleCursorAdapter

TextViews 또는 XML 파일에 정의 ImageViews에 커서에서 열을지도하는 쉬운 어댑터입니다.

난 당신이 데이터베이스에서 값을 결합하려고 View 무슨 모르겠지만, 그것은 단지 TextViewView 유형 중 하나입니다 (@+id/textViewB) 때문에 TextView 개체에 대해 작동하는지 생각하고 당신이 할 수있는 이 인터페이스를 사용하십시오.

대신 SimpleAdapter.ViewBinder을 사용해보세요. 그렇지 않으면 getView()을 사용하면 좋은 결과를 얻을 수 있습니다.

관련 문제