我有一个问题,即在我的BaseAdapter中,我尝试仅针对在其中找到的特定字母来更改按钮的颜色,但是当我尝试这样做时,颜色会更改系统中的所有按钮,我该怎么办?干嘛?
更具体地说,在MainActivity类中,有两个列表: userAnswer和newList。在onCreate()中创建类时,我向userAnswer添加了某些字母A,B,C等。并且我想在选择buttonAddLetter之后向userAnswer添加下一个字母,只有我想使这些新字母将颜色更改为另一个字母,而不是与其他字母相同。一切正常,但是问题是所有字母都更改为不同的颜色,尽管在BaseAdapter中,我检查数据是否来自newList,如果是,则更改颜色。我究竟做错了什么?为什么所有按钮的颜色都会改变?
感谢您的帮助。
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();
}
}
我添加图片是因为我不知道如何完全解释我想要实现的目标:
在一张图片上,我有一个带有6个按钮的列表视图,在第二张图片上,我们只有一个不同的按钮列表(新的List,在索引上添加了0,1,2-A,B,C)颜色。问题是我想从第三张图片中获得效果,即当我按下袜子时,我希望新添加的字母具有完全不同的颜色,而那些字母已经具有原来的颜色。>
答案 0 :(得分:0)
更改您的Adapter
构造函数,并将List
“ newList”直接传递给Adapter
。传递Activity
并不是一个好主意,只是为了使您可以访问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;
}
在您的MainActivity
中使用它:
gridViewAnswerAdapter = new GridViewAnswerAdapter(userAnswer, newList, MainActivity.this);
将您的getView()
的{{1}}方法更改为此:
Adapter
在您的@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;
}
方法中,不需要这样做:
addMore()
因为已经设置了gridViewAnswerAdapter = new GridViewAnswerAdapter(userAnswer, getApplicationContext(), MainActivity.this);
gridViewAnswer.setAdapter(gridViewAnswerAdapter);
并且已经添加了“ newList”。
我通常将自己的自定义xml布局用于Adapter
中的View
。但是,如果仅创建inflate
对您有用。.为什么不呢?
答案 1 :(得分:-1)
我很难理解您想要的结果。如果要在初始化后更改按钮颜色,请存储对按钮适配器或按钮的引用。这样,您可以在运行时访问它并将其更改为所需的任何内容。