更改列表视图上一个文本视图的颜色而不更改其他文本视图
Change color of one textview on listview without change others
我需要更改列表视图背景色的颜色 属性。我能做到,我改变了颜色,但它改变了我所有的行,颜色相同。我的意思是,我需要一行红色,另一行绿色......我把代码放在下面来帮助:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, configureList(idRuta, rocodromo, dificultad))
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
TextView text = (TextView) view.findViewById(android.R.id.text1);
for(int i = 0; i < colores.size(); i++)
{
if(colores.get(i).equals("0"))
{
text.setBackgroundColor(Color.GREEN);
}
else if(colores.get(i).equals("1"))
{
text.setBackgroundColor(Color.RED);
}
else if(colores.get(i).equals("2"))
{
text.setBackgroundColor(Color.YELLOW);
}
else
{
text.setBackgroundColor(Color.WHITE);
}
}
return view;
}
};
第一次,我有"colores.get(i) = 1",所以颜色变成红色,但后来我有"colores.get(i) = 2",所以颜色变成黄色。但是我只需要将第二行更改为黄色,而不是第一行,第一行必须是红色。
在"colores"中,我有所有需要更改的颜色列表,按行排序,例如,当"i=0"时,我将第0行更改为该颜色,但是当"i=1" 我只想更改第 1 行,而不是所有行。
谁能帮帮我?谢谢!
试试这个:
if (position % 2 == 1) {
text.setBackgroundColor(Color.RED);
} else {
text.setBackgroundColor(Color.YELLOW);
}
此代码将背景颜色或奇数行设置为黄色,偶数设置为红色
我认为您应该考虑给函数 getView 的 "position" 参数。我假设您喜欢第一行:绿色,接下来是红色,接下来是黄色,其余的都是白色。为此:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, configureList(idRuta, rocodromo, dificultad))
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
TextView text = (TextView) view.findViewById(android.R.id.text1);
if(colores.get(position).equals("0"))
{
text.setBackgroundColor(Color.GREEN);
}
else if(colores.get(position).equals("1"))
{
text.setBackgroundColor(Color.RED);
}
else if(colores.get(position).equals("2"))
{
text.setBackgroundColor(Color.YELLOW);
}
else
{
text.setBackgroundColor(Color.WHITE);
}
return view;
}
};
在这种方法中,每一行(位置是索引)都将测试颜色。
这里不需要遍历颜色数组,因为 "position" 索引应该等于颜色数组中颜色的索引。
删除 for 循环并根据列表视图项目位置获取颜色。您只需要在每个行视图中设置颜色。
像下面这样改变,
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, configureList(idRuta, rocodromo, dificultad))
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
TextView text = (TextView) view.findViewById(android.R.id.text1);
if(colores.get(position).equals("0"))
{
text.setBackgroundColor(Color.GREEN);
}
else if(colores.get(position).equals("1"))
{
text.setBackgroundColor(Color.RED);
}
else if(colores.get(position).equals("2"))
{
text.setBackgroundColor(Color.YELLOW);
}
else
{
text.setBackgroundColor(Color.WHITE);
}
return view;
}
};
我需要更改列表视图背景色的颜色 属性。我能做到,我改变了颜色,但它改变了我所有的行,颜色相同。我的意思是,我需要一行红色,另一行绿色......我把代码放在下面来帮助:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, configureList(idRuta, rocodromo, dificultad))
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
TextView text = (TextView) view.findViewById(android.R.id.text1);
for(int i = 0; i < colores.size(); i++)
{
if(colores.get(i).equals("0"))
{
text.setBackgroundColor(Color.GREEN);
}
else if(colores.get(i).equals("1"))
{
text.setBackgroundColor(Color.RED);
}
else if(colores.get(i).equals("2"))
{
text.setBackgroundColor(Color.YELLOW);
}
else
{
text.setBackgroundColor(Color.WHITE);
}
}
return view;
}
};
第一次,我有"colores.get(i) = 1",所以颜色变成红色,但后来我有"colores.get(i) = 2",所以颜色变成黄色。但是我只需要将第二行更改为黄色,而不是第一行,第一行必须是红色。
在"colores"中,我有所有需要更改的颜色列表,按行排序,例如,当"i=0"时,我将第0行更改为该颜色,但是当"i=1" 我只想更改第 1 行,而不是所有行。
谁能帮帮我?谢谢!
试试这个:
if (position % 2 == 1) {
text.setBackgroundColor(Color.RED);
} else {
text.setBackgroundColor(Color.YELLOW);
}
此代码将背景颜色或奇数行设置为黄色,偶数设置为红色
我认为您应该考虑给函数 getView 的 "position" 参数。我假设您喜欢第一行:绿色,接下来是红色,接下来是黄色,其余的都是白色。为此:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, configureList(idRuta, rocodromo, dificultad))
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
TextView text = (TextView) view.findViewById(android.R.id.text1);
if(colores.get(position).equals("0"))
{
text.setBackgroundColor(Color.GREEN);
}
else if(colores.get(position).equals("1"))
{
text.setBackgroundColor(Color.RED);
}
else if(colores.get(position).equals("2"))
{
text.setBackgroundColor(Color.YELLOW);
}
else
{
text.setBackgroundColor(Color.WHITE);
}
return view;
}
};
在这种方法中,每一行(位置是索引)都将测试颜色。
这里不需要遍历颜色数组,因为 "position" 索引应该等于颜色数组中颜色的索引。
删除 for 循环并根据列表视图项目位置获取颜色。您只需要在每个行视图中设置颜色。 像下面这样改变,
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, configureList(idRuta, rocodromo, dificultad))
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
TextView text = (TextView) view.findViewById(android.R.id.text1);
if(colores.get(position).equals("0"))
{
text.setBackgroundColor(Color.GREEN);
}
else if(colores.get(position).equals("1"))
{
text.setBackgroundColor(Color.RED);
}
else if(colores.get(position).equals("2"))
{
text.setBackgroundColor(Color.YELLOW);
}
else
{
text.setBackgroundColor(Color.WHITE);
}
return view;
}
};