重写Android ArrayAdapter
我想做一件非常简单的事情。我的应用程序中有一个listview,可以动态地向其中添加文本。但是,在某一点之后,我想更改listview中文本的颜色。因此,我创建了一个定义自定义列表项的XML,并将ArrayAdapter子类化。但是,每当我在自定义ArrayAdapter上调用add()方法时,确实会将一个项添加到listview中,但文本不会放入其中 以下是我的XML:`重写Android ArrayAdapter,android,listview,android-arrayadapter,Android,Listview,Android Arrayadapter,我想做一件非常简单的事情。我的应用程序中有一个listview,可以动态地向其中添加文本。但是,在某一点之后,我想更改listview中文本的颜色。因此,我创建了一个定义自定义列表项的XML,并将ArrayAdapter子类化。但是,每当我在自定义ArrayAdapter上调用add()方法时,确实会将一个项添加到listview中,但文本不会放入其中 以下是我的XML:` <TextView xmlns:android="http://schemas.android.com/apk/re
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/list_content" android:textSize="8pt"
android:gravity="center" android:layout_margin="4dip"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#FF00FF00"/>
以及我的ArrayAdapter子类:
private class customAdapter extends ArrayAdapter<String> {
public View v;
public customAdapter(Context context){
super(context, R.layout.gamelistitem);
}
@Override
public View getView(int pos, View convertView, ViewGroup parent){
this.v = convertView;
if(v==null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=vi.inflate(R.layout.gamelistitem, null);
}
if(timeLeft!=0) {
TextView tv = (TextView)v.findViewById(R.id.list_content);
//tv.setText(str[pos]);
tv.setTextColor(Color.GREEN);
}
else {
TextView tv = (TextView)v.findViewById(R.id.list_content);
//tv.setText(str[pos]);
tv.setTextColor(Color.RED);
}
return v;
}
}
私有类customAdapter扩展了ArrayAdapter{
公众观点v;
公共customAdapter(上下文){
super(context,R.layout.gamelistitem);
}
@凌驾
公共视图getView(int pos、视图转换视图、视图组父视图){
这个.v=转换视图;
如果(v==null){
LayoutInflater vi=(LayoutInflater)getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
v=vi.充气(R.layout.gamelistitem,空);
}
if(timeLeft!=0){
TextView电视=(TextView)v.findViewById(R.id.list\u内容);
//tv.setText(str[pos]);
tv.setTextColor(Color.GREEN);
}
否则{
TextView电视=(TextView)v.findViewById(R.id.list\u内容);
//tv.setText(str[pos]);
tv.setTextColor(颜色:红色);
}
返回v;
}
}
我肯定我做错了什么,但我对安卓还是有点陌生
谢谢大家!!
`您需要在
getView()
中设置文本。获取要使用getItem(pos)
设置的值,然后进行设置
public View getView(int pos, View convertView, ViewGroup parent){
this.v = convertView;
if(v==null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=vi.inflate(R.layout.gamelistitem, null);
}
// Moved this outside the if blocks, because we need it regardless
// of the value of timeLeft.
TextView tv = (TextView)v.findViewById(R.id.list_content);
tv.setText(getItem(pos));
if(timeLeft!=0) {
//TextView tv = (TextView)v.findViewById(R.id.list_content);
//tv.setText(str[pos]);
tv.setTextColor(Color.GREEN);
}
else {
//TextView tv = (TextView)v.findViewById(R.id.list_content);
//tv.setText(str[pos]);
tv.setTextColor(Color.RED);
}
return v;
}
另外,您将
v
存储为成员变量,而不是仅存储在函数中,这有什么原因吗?我之所以存储v,是因为我认为可能需要重写add()方法,因为我的ListView开始时没有in,但随着时间的推移会获得对象。@Brian515:Ah。这是扩展ArrayAdapter这样的全功能类的一个好处。它已经处理了几乎所有您需要的东西(如add()
),您可以只担心一些零碎的东西,如自定义数据或自定义视图。我还没有机会过多地使用ListView(我才刚刚开始),但从我对您正在做的事情以及ListView如何工作的理解来看,我可以看到另一个潜在的问题。据我所知,ListView回收视图,因此如果在添加视图时检查timeLeft的值,则当用户上下滚动时,旧项目可能会从绿色变为红色。如果发生这种情况,可能会考虑在创建对象时使用一个包含字符串和当前TimelEFT的对象,而不是仅使用String。@ ErimHAMION,是的,在TimeleFt等于0时,所有对象都变红了。我如何克服这个问题?我可以让ListView不回收视图吗?@Brian:我可能会使用一个小类来保存字符串和当前timeLeft值。这将需要一个数组
并从ArrayAdapter
进行扩展,但它只会对getView()
进行微小的更改。如果无法在适配器内计算timeLeft,则可以添加一个add(String)
方法来创建一个新的MyClass对象(使用该字符串和当前timeLeft),并调用add(MyClass)
(仍然不会被覆盖)。这样,您就可以在添加数据时进行记录,而不是在添加视图时进行记录。