我想根据更改的值更改网格视图中按钮的颜色。我的代码在这里。但它没有用。所有按钮的颜色始终相同
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
System.out.println("Inside GETVIEW()");
if (convertView == null) {
System.out.println("Inside convertVIEW()");
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.list_toggle_button, null);
holder.button = (Button) convertView.findViewById(R.id.each_device);
convertView.setTag(holder);
View_device.add(convertView);
if (Constants.Score!=null) {
Log.i("SCORE:ARAY LIST",""+Constants.Score);
for(int m=0;m<Constants.Score.size();m++)
{
if(Constants.Score.get(m)==0){
holder.button.setBackgroundResource(R.drawable.red_btn);//FAILED
Log.i("SCORE::ZERO","RED BACKGROUND");
}
else {
holder.button.setBackgroundResource(R.drawable.green_btn);//PASS
Log.i("SCORE::ONE","GREEN BACKGROUND");
}
}
}
}}
答案 0 :(得分:0)
删除 for 循环,如下所示,它将起作用。
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
System.out.println("Inside GETVIEW()");
if (convertView == null) {
System.out.println("Inside convertVIEW()");
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.list_toggle_button, null);
holder.button = (Button) convertView.findViewById(R.id.each_device);
convertView.setTag(holder);
View_device.add(convertView);
if (Constants.Score!=null) {
Log.i("SCORE:ARAY LIST",""+Constants.Score);
if(Constants.Score.get(position)==0){
holder.button.setBackgroundResource(R.drawable.green_btn);//FAILED
Log.i("SCORE::ZERO","RED BACKGROUND");
}
else {
holder.button.setBackgroundResource(R.drawable.red_btn);//PASS
Log.i("SCORE::ONE","GREEN BACKGROUND");
}
}
}}
答案 1 :(得分:0)
试试这个,
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
System.out.println("Inside GETVIEW()");
if (convertView == null) {
System.out.println("Inside convertVIEW()");
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.list_toggle_button, null);
holder.button = (Button) convertView.findViewById(R.id.each_device);
convertView.setTag(holder);
View_device.add(convertView);
}else{
holder=(ViewHolder)convertView.getTag();
}
if (Constants.Score!=null) {
Log.i("SCORE:ARAY LIST",""+Constants.Score);
for(int m=0;m<Constants.Score.size();m++)
{
if(Constants.Score.get(m)==0){
holder.button.setBackgroundResource(R.drawable.red_btn);//FAILED
Log.i("SCORE::ZERO","RED BACKGROUND");
}
else {
holder.button.setBackgroundResource(R.drawable.green_btn);//PASS
Log.i("SCORE::ONE","GREEN BACKGROUND");
}
}
}else{
holder.button.setBackgroundResource(R.drawable.red_btn);//FAILED
Log.i("SCORE::ZERO","RED BACKGROUND");
}
}