I have a problem namely in my BaseAdapter I try to change the color of the button only for specific letters that I find in them, but when I try to do it, the color changes all buttons in the system, what can I do?
More specifically, in the MainActivity class I have two lists: userAnswer and newList. When creating a class in onCreate (), I add certain letters A, B, C etc. to the userAnswer. and I would like to add next letters to userAnswer after choosing buttonAddLetter, only I want to make these new letters change color to another one and not to the same as the other ones. Everything works, however, the problem is that all letters change to a different color, although in BaseAdapter I check if the data comes from newList, if so, change the color. What am I doing wrong? Why is the color changing for all buttons?
Thank you for your help.
GridViewAnswerAdapter.class
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);
}
//I want to change color button only where newList add some new letter, //everything is ok, but it's change every button colors, why?
else if(mainActivity.newList.size() != 0){
if (mainActivity.newList.get(position).equals(answer.get(position))) {
buttonAnswerAdapter = new Button(context);
buttonAnswerAdapter.setLayoutParams(new GridView.LayoutParams(100, 100));
buttonAnswerAdapter.setPadding(8, 8, 8, 8);
buttonAnswerAdapter.setBackgroundColor(Color.BLACK);
buttonAnswerAdapter.setTextColor(Color.RED);
buttonAnswerAdapter.setText(String.valueOf(mainActivity.newList.get(position)));
}
}
else if(mainActivity.userAnswer.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
{
Toast.makeText(context, "Something is wrong!", Toast.LENGTH_SHORT).show();
}
}
else {
buttonAnswerAdapter = (Button) convertView;
}
return buttonAnswerAdapter;
}
}
MainActivity.class
public class MainActivity extends AppCompatActivity {
List<String> userAnswer = new ArrayList<>();
List<String> newList = new ArrayList<>();
GridViewAnswerAdapter gridViewAnswerAdapter;
GridView gridViewAnswer;
Button buttonAddLetter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridViewAnswer = findViewById(R.id.gridViewAnswer);
buttonAddLetter = findViewById(R.id.buttonWskazówka);
buttonAddLetter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addMore();
}
});
userAnswer.add(0, "A");
userAnswer.add(1, "B");
userAnswer.add(2, "C");
userAnswer.add(3, "D");
userAnswer.add(4, "E");
userAnswer.add(5, "F");
userAnswer.add(6, "G");
gridViewAnswerAdapter = new GridViewAnswerAdapter(userAnswer, getApplicationContext(), MainActivity.this);
gridViewAnswer.setAdapter(gridViewAnswerAdapter);
gridViewAnswerAdapter.notifyDataSetChanged();
}
private void addMore()
{
newList.clear();
newList.addAll(userAnswer);
newList.set(0, "A");
newList.set(1, "B");
newList.set(2, "C");
gridViewAnswerAdapter = new GridViewAnswerAdapter(userAnswer, getApplicationContext(), MainActivity.this);
gridViewAnswer.setAdapter(gridViewAnswerAdapter);
gridViewAnswerAdapter.notifyDataSetChanged();
}
}
I add pictures because I do not know how to fully explain what I would like to achieve:
On 1 picture I have a list view with 6 buttons, on the second picture we have a list (new List, which adds 0,1,2 - A, B, C on the index) of buttons in only a different color. The problem is that I would like to achieve the result from the 3rd picture, i.e. when I press the sock I want the newly added letters to have a completely different color and the ones that have already been in the color that it was.
Change your Adapter
constructor and pass the List
"newList" directly to the Adapter
. It isn't a good idea to pass the Activity
just so you can access some class variable that is in your Activity
.
private List<String> answer;
private List<String> newList;
private Context context;
public GridViewAnswerAdapter(List<String> answer, List<String> newList, Context context) {
this.answer = answer;
this.newList = newList;
this.context = context;
}
Use this from your MainActivity
:
gridViewAnswerAdapter = new GridViewAnswerAdapter(userAnswer, newList, MainActivity.this);
Change the getView()
method of your Adapter
to this:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Button buttonAnswerAdapter = null;
if (convertView == null) {
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(answer.get(position));
}else{
buttonAnswerAdapter = (Button) convertView;
}
if(newList.size() != 0){
if (newList.get(position).equals(answer.get(position))) {
buttonAnswerAdapter.setBackgroundColor(Color.BLACK);
buttonAnswerAdapter.setTextColor(Color.RED);
}
}
return buttonAnswerAdapter;
}
In your addMore()
method there is no need to have this:
gridViewAnswerAdapter = new GridViewAnswerAdapter(userAnswer, getApplicationContext(), MainActivity.this);
gridViewAnswer.setAdapter(gridViewAnswerAdapter);
because the Adapter
is already set and the "newList" has already been added.
I would usually use my own custom xml layout for the View
that I would inflate
in my ListView
. But, if just creating a Button
works for you..why not?