2011-12-12 4 views
2

Android 앱에서 작업을 나열하는 Listview가 있습니다. 작업 유형에 따라 행의 색을 지정하고 싶습니다. 다음은 Listview를 설정하는 데 사용하는 코드입니다.Listview에서 개별 행의 배경색을 개별적으로 설정합니다.

startManagingCursor(cursor); 
    adapter = new SimpleCursorAdapter(this, R.layout.menu_item, cursor, FROM, TO); 
    menuList.setAdapter(adapter); 

다음은 목록보기 항목의 Xml 레이아웃입니다.

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    android:padding="10sp"> 
    <TextView 
     android:id="@+id/rowid" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@color/exhibitor_header" /> 
    <TextView 
     android:id="@+id/rowidtwo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@color/listview_background" 
     android:layout_below="@+id/rowid"/> 
</RelativeLayout> 

각 작업 열과 관련된 색으로 각 행을 표시하도록 어떻게 설정합니까? 사용자 지정 어댑터를 사용하지 않고도이 작업을 수행 할 수 있습니까?

+3

사용자 정의 어댑터 없이는 할 수 없다고 생각합니다 ... 자신 만의 SimpleCursorAdapter를 작성해야합니다 ... – krackmoe

+0

해당 어댑터를 사용하십시오 .. – user370305

답변

1

여기 예가 있습니다 : Android – Applying Alternate Row Color in ListView with SimpleAdapter.

위의 예와 같은 사용자 지정 어댑터를 정의하면 getView() 메서드 내에서 작업 유형을 확인하기위한 조건을 입력해야하며이를 기반으로 다른 배경색을 적용 할 수 있습니다.

+0

예를 들어 주셔서 감사합니다. 다음 코드를 사용하여 사용자 지정 어댑터를 추가했습니다. public Lectures_Adapter (Context context, List > items, int resource, String [] from, int [] to) { super (context, items, resource , 에서부터); this.data = items; } 데이터를 사용하여 예제를 수정하려고하면 오류가 발생합니다. 내 데이터는 Sqlite 테이블에서 가져 와서 커서로로드됩니다. 오류로 인해 List > – user616076

+1

으로 캐스팅하여 커서를 변경하려고합니다. 내 어댑터가 SimpleCursorAdapter 대신 SimpleAdapter를 확장했지만 솔루션이 나에게 적합했습니다. 고맙습니다 – user616076

관련 문제