2014-09-25 1 views
1

내 응용 프로그램에서는 listview에 항목으로 yes 및 no 버튼을 유지합니다. setOnClickListener어댑터 클래스로 정의됩니다. 나는 이것에 문제가있다. 목록보기를 스크롤 한 후에 다른 행에 변경된 단추가 표시됩니다. 문제를 어떻게 해결할 수 있습니까?버튼을 클릭했을 때 listview의 버튼 배경색을 변경하는 방법

public class QuizAdapter extends BaseAdapter { 

    private static final String FONTH_PATH_1 = "fonts/Brandon_reg.otf"; 
    private static final String FONTH_PATH_2 = "fonts/Brandon_bld.otf"; 

    private ArrayList<Question> questionList = new ArrayList<Question>(); 
    private LayoutInflater inflater; 

    private Context context; 

    public QuizAdapter(Context context) { 
     super(); 
     this.inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     this.context = context; 
    } 

    public void addListItem(final Question item) { 
     questionList.add(item); 
     notifyDataSetChanged(); 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return questionList.size(); 
    } 

    @Override 
    public Question getItem(int position) { 
     // TODO Auto-generated method stub 
     return questionList.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 

     final ViewHolder holder; 


     Question currentQuestion = getItem(position); 

     Typeface font1 = Typeface.createFromAsset(context.getAssets(), 
       FONTH_PATH_1); 
     Typeface font2 = Typeface.createFromAsset(context.getAssets(), 
       FONTH_PATH_2); 

     if (convertView == null) { 
      holder = new ViewHolder(); 
      convertView = inflater.inflate(R.layout.quiz_list, null); 

      holder.questionCounterTextView = (TextView) convertView 
        .findViewById(R.id.questionCounterTextView); 
      holder.questionCounterTextView.setTypeface(font2); 

      holder.questionTextView = (TextView) convertView 
        .findViewById(R.id.questionTextView); 
      holder.questionTextView.setTypeface(font1); 

      holder.yesButton = (Button) convertView 
        .findViewById(R.id.yesButton); 
      holder.yesButton.setTypeface(font2); 

      holder.noButton = (Button) convertView.findViewById(R.id.noButton); 
      holder.noButton.setTypeface(font2); 

      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 


     } 

     holder.yesButton.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       holder.yesButton.setBackgroundColor(R.color.Gray); 
      } 
     }); 

     holder.questionCounterTextView.setText(Integer.toString(position + 1)); 

     holder.questionTextView.setText(currentQuestion.getQuestionMessage()); 

     return convertView; 
    } 

    public static class ViewHolder { 
     public TextView questionCounterTextView, questionTextView; 
     public Button yesButton, noButton; 
    } 

} 

XML 파일

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/White" 
    android:orientation="vertical" 
    tools:context="...." > 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" > 

     <ImageView 
      android:id="@+id/flagImageView" 
      android:layout_width="80dp" 
      android:layout_height="80dp" 
      android:layout_alignParentLeft="true" 
      android:src="@drawable/flag" /> 

     <TextView 
      android:id="@+id/questionCounterTextView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignBottom="@+id/flagImageView" 
      android:layout_alignLeft="@+id/flagImageView" 
      android:layout_alignRight="@+id/flagImageView" 
      android:layout_alignTop="@+id/flagImageView" 
      android:layout_marginLeft="10dp" 
      android:text="1" 
      android:gravity="center|left" 
      android:textColor="@color/White" 
      android:textSize="@dimen/question_counter_text_size"/> 

     <TextView 
      android:id="@+id/questionTextView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="5dp" 
      android:layout_toRightOf="@+id/flagImageView" 
      android:text="@string/loremIpsum2" 
      android:textColor="@color/Blue" 
      android:textSize="@dimen/text_size" /> 
    </RelativeLayout> 

    <LinearLayout 
     android:id="@+id/quizLinearLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="30dp" 
     android:layout_marginBottom="30dp" 
     android:orientation="horizontal" 
     android:weightSum="640" > 

     <Space 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="30" /> 

     <Button 
      android:id="@+id/yesButton" 
      style="@style/button_type" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="275" 
      android:background="@color/QuizGreen" 
      android:text="@string/yes" 
      android:textColor="@color/White" 
      android:textSize="@dimen/question_button_text_size" /> 

     <Space 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="30" /> 

     <Button 
      android:id="@+id/noButton" 
      style="@style/button_type" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="275" 
      android:background="@color/QuizOrange" 
      android:text="@string/no" 
      android:textColor="@color/White" 
      android:textSize="@dimen/question_button_text_size" /> 

     <Space 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="30" /> 
    </LinearLayout> 

</LinearLayout> 
+0

확실히 ViewHolder가 버그를 만듭니다. 배경색 변경을 위해 이전에 선택기를 사용해 보셨습니까? – Pedram

+0

@Pedram, 저는 2 개의 버튼이 있고 그 중 하나가 클릭 될 때 각각이 바뀔 수 있기 때문에 선택자를 사용하고 싶지 않습니다. 예를 들어, 예 버튼을 클릭 한 후에는 아무 버튼도 클릭 할 수 없습니다. 소유자와 동일한 문제가 발생할 수 있습니다. 답변 주셔서 감사합니다. – smihilor

답변

1

나는 사용자의 답변을 유지하는 Question 클래스의 새 String을 정의 할 때 내 문제를 해결했습니다. 예 또는 아니오를 제어하면 배경이 변경됩니다.

 holder.yesButton.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      if (currentQuestion.getUserAnswer() == null) { 
       questionList.get(position).setUserAnswer("TRUE"); 
       notifyDataSetChanged(); 
      } else if (currentQuestion.getUserAnswer().equals("FALSE")) { 
       questionList.get(position).setUserAnswer("TRUE"); 
       notifyDataSetChanged(); 
      } 
     } 
    }); 

    holder.noButton.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      if (currentQuestion.getUserAnswer() == null) { 
       questionList.get(position).setUserAnswer("FALSE"); 
       notifyDataSetChanged(); 
      } else if (currentQuestion.getUserAnswer().equals("TRUE")) { 
       questionList.get(position).setUserAnswer("FALSE"); 
       notifyDataSetChanged(); 
      } 
     } 
    }); 

    if (currentQuestion.getUserAnswer() == null) { 
     holder.yesButton.setBackgroundColor(context.getResources() 
       .getColor(R.color.QuizGreen)); 
     holder.noButton.setBackgroundColor(context.getResources().getColor(
       R.color.QuizOrange)); 
    } else if (currentQuestion.getUserAnswer().equals("TRUE")) { 
     holder.yesButton.setBackgroundColor(context.getResources() 
       .getColor(R.color.NewsFeedDividerGray)); 
     holder.noButton.setBackgroundColor(context.getResources().getColor(
       R.color.QuizOrange)); 
    } else { 
     holder.yesButton.setBackgroundColor(context.getResources() 
       .getColor(R.color.QuizGreen)); 
     holder.noButton.setBackgroundColor(context.getResources().getColor(
       R.color.NewsFeedDividerGray)); 
    } 
0

가지고 당신 홀더를 사용하여,이 같은 일을하지 시도?

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    RelativeLayout rl = (RelativeLayout)v.getParent(); 
    Button bttn = (Button)rl.findViewById(R.id.yesButton); 
    bttn.setBackgroundColor(R.color.Gray); 
} 
+0

아니요,이 방법은보기의 재사용 가능성을 줄입니다. ViewHolder를 사용하는 것이 더 좋습니다. 고맙습니다. – smihilor

+0

이전과 똑같은 방법으로 뷰 홀더를 사용합니다. 클릭 한 부모로부터 버튼을 가져 오려면 onClick 메서드를 변경하기 만하면됩니다. –

관련 문제