2011-10-16 3 views
0

ListView에서 다른 색상을 사용하는 데 약간의 문제가 있습니다.다른 색상의 Android 목록보기

나는 몇 가지 있지만, 아무것도 시도가 작동하는 것 같다

개인 클래스 GameRowAdapter는 ArrayAdapter와 { 의 ArrayList 객체를 확장;

public GameRowAdapter(Context context, int textViewResourceId, ArrayList<RowModel> objects) { 
     super(context, textViewResourceId, objects); 
     this.objects = objects; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View row = convertView; 
     RowModelViews model; 
     if (row == null) { 
      LayoutInflater inflater = LayoutInflater.from(GamePlayerActivity.this); 
      row = inflater.inflate(R.layout.game_line_layout, null); 
      model = new RowModelViews(); 
      model.entry = (TextView) row.findViewById(R.id.gameLineEntry); 
      model.score = (TextView) row.findViewById(R.id.gameLineEntryScore); 
      row.setTag(model); 
     } else { 
      model = (RowModelViews) row.getTag(); 
     } 

     if (position == 6 || position == 7) { 
      row.setBackgroundColor(R.color.black); 
     } else { 
      row.setBackgroundColor(R.color.light_transparent); 
     } 

     model.entry.setText(objects.get(position).entry); 
     model.score.setText(objects.get(position).score); 

     return row; 
    } 
} 

static class RowModelViews { 
    TextView entry; 
    TextView score; 

} 

static class RowModel { 
    String entry; 
    String score; 

    public RowModel(String entry, String score) { 
     this.entry = entry; 
     this.score = score; 
    } 
} 

나에게 말할 수있는 사람은 누구입니까?

감사합니다.

UPDATE 여기

는 inflatet 행

<?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" 
       android:orientation="horizontal"> 
    <TextView android:id="@+id/gameLineEntry" 
       android:layout_height="fill_parent" 
       android:layout_width="fill_parent" 
       android:layout_marginLeft="20dp" 
       android:textSize="20dp" 
       android:gravity="center_vertical"/> 
    <TextView android:id="@+id/gameLineEntryScore" 
       android:layout_height="fill_parent" 
       android:layout_width="65dp" 
       android:layout_marginRight="20dp" 
       android:gravity="center" 
       style="@style/Button"     
       android:layout_gravity="right"/> 
</LinearLayout> 

답변

0

편집 : 작은 로컬 테스트를 실행 한 후에는 각 행에있는 TextView가 ListView 행보기 앞에 있다는 문제가있을 수 있습니다. model.entry 및 model.score 배경색을 설정해보십시오./편집

나는 그것이 convertViews에서 재사용되고 있다고 생각합니다. 이 이동해보십시오 :

if (position == 6 || position == 7) { 
    row.setBackgroundColor(R.color.black); 
} 

는 IF의 외부 (행 == NULL) 블록을이로 변경 :

if (position == 6 || position == 7) { 
    model.entry.setBackgroundColor(R.color.black); 
    model.score.setBackgroundColor(R.color.black); 

} else { 
    model.entry.setBackgroundColor(/*Whatever your default background color is*/); 
    model.score.setBackgroundColor(/*Whatever your default background color is*/); 
} 

을 그래서 전체의 getView 방법은 다음과 같습니다

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View row = convertView; 
     RowModelViews model; 
     if (row == null) { 
      LayoutInflater inflater = LayoutInflater.from(GamePlayerActivity.this); 
      row = inflater.inflate(R.layout.game_line_layout, null); 
      model = new RowModelViews(row); 
      model.entry = (TextView) row.findViewById(R.id.gameLineEntry); 
      model.score = (TextView) row.findViewById(R.id.gameLineEntryScore); 
      row.setTag(model); 
     } else { 
      model = (RowModelViews) row.getTag(); 
     } 

     if (position == 6 || position == 7) { 
      model.entry.setBackgroundColor(R.color.black); 
      model.score.setBackgroundColor(R.color.black); 
     } else { 
      model.entry.setBackgroundColor(/*Whatever your default background color is*/); 
      model.score.setBackgroundColor(/*Whatever your default background color is*/); 
     } 

     model.entry.setText(objects.get(position).entry); 
     model.score.setText(objects.get(position).score); 

     return row; 
    } 
+0

무슨 뜻인지 알 수 있습니다. 그러나이 코드를 사용하면 ListView의 모든 행이 검은 색으로 표시됩니다. – Bastaix

+0

배경색을 배경 위치로 설정 하시겠습니까? = {6,7} else 절에서? 빨간색이라고 말하면 6 & 7을 제외한 모든 배경이 빨간색으로 표시됩니다. –

+0

위치가! = {6,7} 인 경우 기본 색상을 설정합니다. 그러나 당신의 정정과 더불어 모든 선은 검게된다?? – Bastaix

2

에만 뷰의 생성에 컬러를 정의의 XML이다. 그러나 재생 된 뷰의 경우 색상을 다시 명시 적으로 설정해야합니다. 6과 7이 새로운 관점이된다는 보장은 없습니다. 그래서 getView를 호출 할 때마다 뷰의 색상을 항상 설정하십시오.

+0

각 getView 호출에 대한 색을 설정하려고했는데 (번스타인 대답 참조), 모든 행이 검은 색으로 변할 때 ?? – Bastaix

+0

시도한 새로운 방식을 게시 할 수 있습니까? 또한 xml을 부 풀릴 수 있습니까? –