Des*_*que 18 android listview adapter
我有一个自定义listView.主要布局xml是这样的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:layout_height="wrap_content"
android:id="@+id/lv_clientes"
android:layout_width="0dp">
</ListView>
<!-- From this part there are not problems -->
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
列表项XML就是这样
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlo_elemento"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/tv_nombre"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<!-- From this part there are not problems -->
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
现在适配器是这样的:
public class AdapterListaClientes extends BaseAdapter
{
private Cliente[] data;
Context context;
LayoutInflater layoutInflater;
int itemSelected = -1;
public void setSelected(int valor)
{
itemSelected = valor;
}
public AdapterListaClientes(Context context, ArrayList<Cliente> data)
{
this.data = data.toArray(new Cliente[0]);
this.context = context;
layoutInflater = LayoutInflater.from(context);
}
/*Mandatory things and so...*/
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
//All the things that we should put in this point.. I'm using the list14 example
//HERE IS THE POLEMIC CODE
if(position == itemSelected)
convertView.setBackgroundColor(R.color.rojo);
else
convertView.setBackgroundColor(R.color.blanco);
return convertView;
Run Code Online (Sandbox Code Playgroud)
}}
setBackgroundColor()方法不起作用.我知道这样做是因为当我使用此方法时,按下列表视图项时,按下的项目的背景颜色将更改为默认颜色的不透明版本.
这个问题只发生在背景颜色,我可以改变其他一切没有问题...
谢谢!
Yas*_*mar 58
使用
setBackgroundResource(R.color.rojo);
Run Code Online (Sandbox Code Playgroud)
R.color.rojo是一种资源,它不是颜色..
| 归档时间: |
|
| 查看次数: |
15975 次 |
| 最近记录: |