如何在android中单击按钮时更改列表视图按钮背景颜色

时间:2014-09-25 13:17:03

标签: android listview android-listview

在我的应用程序中,我在listview中保留yes和no按钮作为项目。 setOnClickListener 适配器类中定义。我有这个问题。滚动列表视图后,我在另一行中看到更改的按钮。我该如何解决我的问题?

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>

2 个答案:

答案 0 :(得分: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));
    }

答案 1 :(得分: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);  
}