[英]How to change listview's button background color when button clicked in android
In my application, I keep yes and no button in listview as an item. 在我的应用程序中,我在列表视图中保留“是”和“否”按钮作为一项。 setOnClickListener is defined in Adapter class. setOnClickListener在Adapter类中定义。 I have a problem with this. 我对此有疑问。 After I scroll the listview, I see the changed button in another row. 滚动列表视图后,在另一行中看到更改的按钮。 How can I resolve my issue ? 我该如何解决我的问题?
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 file 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>
I solved my problem with defining a new String in Question class which is kept user answer. 我通过在Question类中定义一个新的String来解决我的问题,该类保留用户答案。 Controlling yes or no than change the background. 控制是或否,而不是更改背景。
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));
}
Have you tried not using the holder, and doing something like this? 您是否尝试过不使用支架,而是这样做?
@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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.