Android TextView.settextColor影响多个项目
我有一个安卓用户界面的问题,我似乎无法解决。 我的主要活动(aAndroid TextView.settextColor影响多个项目,android,baseadapter,Android,Baseadapter,我有一个安卓用户界面的问题,我似乎无法解决。 我的主要活动(aListActivity)显示项目列表。当用户单击一个时,它会通过AsyncTask更新基础数据(从web服务查询)。当请求成功终止时,我调用notifyDataSetChanged()以显示新信息。 我的理解是,将调用自定义BaseAdapter中的getView方法来执行渲染: 私有列表数据; @凌驾 公共视图getView(int位置、视图转换视图、视图组父视图) { if(convertView==null){ convert
ListActivity
)显示项目列表。当用户单击一个时,它会通过AsyncTask
更新基础数据(从web服务查询)。当请求成功终止时,我调用notifyDataSetChanged()以显示新信息。
我的理解是,将调用自定义BaseAdapter
中的getView
方法来执行渲染:
私有列表数据;
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图)
{
if(convertView==null){
convertView=LayoutFlater.from(ctx).充气(R.layout.list_项,父项,false);
}
InstalledApp app=data.get(位置);
字符串latest_version=app.getLatestVersion();
TextView版本=(TextView)convertView.findViewById(R.id.version);
if(最新版本!=null)
{
if(app.getVersion().equals(最新版本))
{
version.setText(app.getVersion());
version.setTextColor(Color.GREEN);
}
其他的
{
version.setText(app.getVersion()+“(当前:+最新版本+”);
version.setTextColor(Color.RED);
}
}
否则{
version.setText(app.getVersion());
}
返回视图;
}
问题是,当一个项目被更新并被调用时,两个项目的颜色发生了变化。但文本内容仍然正确。第二个通常不会立即显示,我必须向下滚动列表才能看到它。
您知道是什么导致了这种情况吗?由于convertView正在被回收,所以出现了这种问题。要解决您的问题,只需确保在IF语句中为convertView设置某些属性时,需要在ELSE语句中重置这些属性,以确保在重用视图且特定条件不再适用时,您的属性恢复为默认值 在您的特殊情况下:
- 对于列表中的第一项,这些条件可能为真:(最新版本!=null)和(app.getVersion().equals(最新版本)),所以您可以将textColor设置为绿色
- 然后向下滚动,此视图将被重新用于另一个列表项,因为不再可见(出于性能原因,listView就是这样设计的)。现在,对于这个新的列表项,不同的条件可能是正确的,特别是:(最新版本==null)->这导致此行被调用
version.setText(app.getVersion())代码>,因此您的新列表项具有正确的文本,但您没有为此场景设置textColor,因此textColor保持不变(在重用视图之前设置)
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null) {
convertView = LayoutInflater.from(ctx).inflate(R.layout.list_item, parent, false);
}
InstalledApp app = data.get(position);
String latest_version = app.getLatestVersion();
TextView version = (TextView) convertView.findViewById(R.id.version);
if (latest_version != null)
{
if (app.getVersion().equals(latest_version))
{
version.setText(app.getVersion());
version.setTextColor(Color.GREEN);
}
else
{
version.setText(app.getVersion() + " (Current: " + latest_version + ")");
version.setTextColor(Color.RED);
}
}
else {
version.setText(app.getVersion());
version.setTextColor(Color.GREEN); // YOU NEED TO ALSO SET CORRECT COLOR HERE
}
return convertView;
}
您正面临这个问题,因为listview回收视图
你可以尝试下面的方法来克服它
public class InstalledApp
{
...// rest of the code
...// setter an getter for version
int color;
public void setColor(int color)
{
this. color = color;
}
public int getColor()
{
return color;
}
}
填充列表数据时代码>。此列表可以在循环中填充。以下只是一个例子
InstalledApp app = new Installedapp();
if(version.equals(latest_version); // you condition to check latest version
{
app.setColor(Color.GREEN);
}
else
{
app.setColor(Color.RED);
}
...// setter for version here
data.add(app);
将填充的列表传递给适配器类的构造函数
然后在getView
InstalledApp app = data.get(position);
version.setText(app.getVersion() + " (Current: " + latest_version + ")");
version.setTextColor(app.getColor());
试试这个
private List<InstalledApp> data;
int[] colors=new int[data.size()];
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null) {
convertView = LayoutInflater.from(ctx).inflate(R.layout.list_item, parent, false);
}
InstalledApp app = data.get(position);
String latest_version = app.getLatestVersion();
TextView version = (TextView) convertView.findViewById(R.id.version);
if (latest_version != null)
{
if (app.getVersion().equals(latest_version))
{
version.setText(app.getVersion());
colors[position]=Color.GREEN;
version.setTextColor(colors[position]);
}
else
{
version.setText(app.getVersion() + " (Current: " + latest_version + ")");
colors[position]=Color.RED;
version.setTextColor(colors[position]);
}
}
else {
version.setText(app.getVersion());
}
return convertView;
私有列表数据;
int[]colors=newint[data.size()];
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图)
{
if(convertView==null){
convertView=LayoutFlater.from(ctx).充气(R.layout.list_项,父项,false);
}
InstalledApp app=data.get(位置);
字符串latest_version=app.getLatestVersion();
TextView版本=(TextView)convertView.findViewById(R.id.version);
if(最新版本!=null)
{
if(app.getVersion().equals(最新版本))
{
version.setText(app.getVersion());
颜色[位置]=Color.GREEN;
version.setTextColor(颜色[位置]);
}
其他的
{
version.setText(app.getVersion()+“(当前:+最新版本+”);
颜色[位置]=Color.RED;
version.setTextColor(颜色[位置]);
}
}
否则{
version.setText(app.getVersion());
}
返回视图;
}这种情况会发生,因为listview会回收视图。postInstalledApp
Class您能否提供更多关于示例数据的详细信息以及正在发生的事情……谢谢!我明白现在发生了什么。非常感谢你的回答和回收机制的链接。感谢你花时间回复。我更喜欢其他答案,因为跟踪所有颜色对我来说似乎没有必要。