问题
How can you change the color of the name in the button so that everyone is not in the same color for a specific letter in the BaseAdapter, where in the view I create buttons?
At the moment, in the getView () method, I check if the answer is empty, if so I set the default blue color and everything is fine, but I would like to distinguish when the list is full for a specific index list let's say 3,6,7 set a different text color than green as in the current example.
public class GridViewAnswerAdapter extends BaseAdapter {
private List<String> answer;
private Context context;
private MainActivity mainActivity;
public GridViewAnswerAdapter(List<String> answer, Context context, MainActivity mainActivity) {
this.answer = answer;
this.context = context;
this.mainActivity = mainActivity;
}
@Override
public int getCount() {
return answer.size();
}
@Override
public Object getItem(int position) {
return answer.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Button buttonAnswerAdapter = null;
if (convertView == null) {
if (answer.get(position).equals(" ")) {
buttonAnswerAdapter = new Button(context);
buttonAnswerAdapter.setLayoutParams(new GridView.LayoutParams(100, 100));
buttonAnswerAdapter.setPadding(8, 8, 8, 8);
buttonAnswerAdapter.setBackgroundColor(Color.BLUE);
} else if (mainActivity.listAnswer.get(position).equals(answer.get(position))) {
//Create new button
buttonAnswerAdapter = new Button(context);
buttonAnswerAdapter.setLayoutParams(new GridView.LayoutParams(100, 100));
buttonAnswerAdapter.setPadding(8, 8, 8, 8);
buttonAnswerAdapter.setBackgroundColor(Color.BLUE);
buttonAnswerAdapter.setTextColor(Color.GREEN);
buttonAnswerAdapter.setText(String.valueOf(answer.get(position)));
} else {
buttonAnswerAdapter = (Button) convertView;
}
}
return buttonAnswerAdapter;
}
}
I try to do this in such a way that I check if the new List contains the character from the answer and then change the color of the text to red, unfortunately I change all letters to red and when I again search the listAnswers again all text colors change. And I would like to change only one specific one button text not for everyone.
My failed attempts:
public class GridViewAnswerAdapter extends BaseAdapter {
private List<String> answer;
private Context context;
private MainActivity mainActivity;
public GridViewAnswerAdapter(List<String> answer, Context context, MainActivity mainActivity) {
this.answer = answer;
this.context = context;
this.mainActivity = mainActivity;
}
@Override
public int getCount() {
return answer.size();
}
@Override
public Object getItem(int position) {
return answer.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Button buttonAnswerAdapter = null;
if (convertView == null) {
if (answer.get(position).equals(" ")) {
buttonAnswerAdapter = new Button(context);
buttonAnswerAdapter.setLayoutParams(new GridView.LayoutParams(100, 100));
buttonAnswerAdapter.setPadding(8, 8, 8, 8);
buttonAnswerAdapter.setBackgroundColor(Color.BLUE);
} else if (mainActivity.listAnswer.get(position).equals(answer.get(position))) {
//Create new button
buttonAnswerAdapter = new Button(context);
buttonAnswerAdapter.setLayoutParams(new GridView.LayoutParams(100, 100));
buttonAnswerAdapter.setPadding(8, 8, 8, 8);
buttonAnswerAdapter.setBackgroundColor(Color.BLUE);
buttonAnswerAdapter.setTextColor(Color.GREEN);
buttonAnswerAdapter.setText(String.valueOf(answer.get(position)));
}else if(mainActivity.newList.get(position).equals(answer.get(position)))
{
//Create new button
buttonAnswerAdapter = new Button(context);
buttonAnswerAdapter.setLayoutParams(new GridView.LayoutParams(100, 100));
buttonAnswerAdapter.setPadding(8, 8, 8, 8);
buttonAnswerAdapter.setBackgroundColor(Color.BLUE);
buttonAnswerAdapter.setTextColor(Color.RED);
buttonAnswerAdapter.setText(String.valueOf(answer.get(position)));
} else {
buttonAnswerAdapter = (Button) convertView;
}
}
return buttonAnswerAdapter;
}
}
来源:https://stackoverflow.com/questions/53981855/how-to-change-text-color-on-button-in-baseadapter