如何在BaseAdapter中更改按钮上的文本颜色?

时间:2018-12-30 22:12:38

标签: android android-adapter baseadapter

如何更改按钮中名称的颜色,以使每个人对于BaseAdapter中特定字母的颜色都不相同,在视图中我在其中创建按钮?

此刻,在getView()方法中,我检查答案是否为空,如果是,则设置默认的蓝色并且一切都很好,但是我想区分特定索引的列表何时已满列表中的3,6,7设置了与当前示例不同的文本颜色,而不是绿色。

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;
}

}

我尝试通过以下方式进行操作:检查新列表是否包含答案中的字符,然后将文本的颜色更改为红色,但是不幸的是,我将所有字母更改为红色,并再次搜索列表时再次,所有文本颜色都会更改。而且我只想更改一种特定的一键式文本,而不是针对所有人。

我的尝试失败:

公共类GridViewAnswerAdapter扩展了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;
}

}

0 个答案:

没有答案