我想为自定义的 ArrayAdapter
实现一个选择器可绘制资源。但是我不断地收到一个 android.content.res.Resources$NotFoundException: File res/drawable/list_selector.xml from drawable resource ID #0x7f020
的错误信息。
有人能提供一些建议吗?
我有两个 xml 文件:
res/drawable/list_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:color="@android:color/white" />
<item android:state_checked="false" android:color="@android:color/black" />
</selector>
res/layout/list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/casualty_text_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/list_selector"/>
</LinearLayout>
最后,我的代码按如下方式加载列表项:
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TCCC cur_object = getItem(position);
View cur_view = convertView;
if(cur_view == null)
{
System.out.println("Inflating!");
cur_view = View.inflate(getContext(), R.layout.list_item, null);
String text_value = (cur_object.m_name.length() == 0) ? "New Casualty" : cur_object.m_name;
TextView name_box = (TextView)cur_view.findViewById(R.id.casualty_text_view);
if(name_box != null)
name_box.setText(text_value);
}
return cur_view;
}
setBackgroundColor()
编程方式,这很容易并且有效。选择器(出乎意料地)也可以工作,但不正确。它没有设置正确的颜色。你有什么想法可以告诉我在哪里可以阅读有关视图膨胀的信息,换句话说,选择器只能在膨胀时静态使用还是可以持续使用?或者默认的getView()
调用是否利用它们? - ConstantinsetItemChecked()
,你是在 TextView 上调用它吗? - Vikram