使用 SimpleAdapter 更改 ListView 中项目的颜色
SimpleAdapter XML 填充 ListView
ArrayList<Recipe> ciboList = null;
ArrayList<HashMap<String,Object>> data = new ArrayList<HashMap<String,Object>>();
for(int i=0;i<ciboList.size();i++){
// Recipe is my own class defined in another java
Recipe r = (Recipe) ciboList.get(i);
HashMap<String,Object> ricettaMap = new HashMap<String, Object>();
// Informations loaded from Recipe.java
ricettaMap.put("tipo", r.getTipo());
ricettaMap.put("titolo", r.getTitolo());
ricettaMap.put("difficolta", r.getDifficolta());
ricettaMap.put("tempo", r.getTempo());
ricettaMap.put("persone", r.getPersone());
ricettaMap.put("ingredienti", r.getIngredienti());
ricettaMap.put("vino", r.getVino());
ricettaMap.put("consigli", r.getConsigli());
ricettaMap.put("preparazione", r.getPreparazione());
}
}
String[] from = {"tipo", "titolo", "difficolta", "tempo", "ingredienti", "vino", "consigli", "preparazione", "persone"};
int[] to = {R.id.ricettaTipo, R.id.ricettaTitolo, R.id.ricettaDifficolta, R.id.ricettaTempo, R.id.ricettaIngredienti, R.id.ricettaVino, R.id.ricettaConsigli, R.id.ricettaPrep, R.id.ricettaPersone};
SimpleAdapter adapter = new SimpleAdapter(
this,
data,
R.layout.list_cibo,
from,
to);
我使用R.layout.list_cibo 的
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:padding="5dip"
android:gravity="right">
<TextView
android:text="Titolo Ricetta"
android:singleLine="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="5dip"
android:textAppearance="?android:attr/textAppearanceMedium"
android:scrollHorizontally="false"
android:id="@+id/ricettaTitolo">
</TextView>
<TextView
android:text="Difficoltà"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#FF0000"
android:layout_marginLeft="5dip"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ricettaDifficolta"
android:singleLine="true"
android:scrollHorizontally="false">
</TextView>
<TextView
android:text="Tempo"
android:layout_toRightOf="@+id/ricettaDifficolta"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#FF3300"
android:layout_marginLeft="20dip"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/ricettaDifficolta"
android:gravity="right"
android:id="@+id/ricettaTempo"
android:layout_width="fill_parent">
</TextView>
<TextView
android:id="@+id/ricettaTipo"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:id="@+id/ricettaPersone"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/ricettaIngredienti">
</TextView>
<TextView
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/ricettaVino">
</TextView>
<TextView
android:id="@+id/ricettaConsigli"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:id="@+id/ricettaPrep"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</RelativeLayout>
我使用三个不同的 SimpleAdapter 基于三种不同的颜色创建三个不同的列表;我想做的是仅创建一个列表,根据参数用三种颜色之一为每个项目着色。
我尝试使用 .setColor
修改 TextView,但它不起作用。
I'm populating a ListView using a SimpleAdapter
ArrayList<Recipe> ciboList = null;
ArrayList<HashMap<String,Object>> data = new ArrayList<HashMap<String,Object>>();
for(int i=0;i<ciboList.size();i++){
// Recipe is my own class defined in another java
Recipe r = (Recipe) ciboList.get(i);
HashMap<String,Object> ricettaMap = new HashMap<String, Object>();
// Informations loaded from Recipe.java
ricettaMap.put("tipo", r.getTipo());
ricettaMap.put("titolo", r.getTitolo());
ricettaMap.put("difficolta", r.getDifficolta());
ricettaMap.put("tempo", r.getTempo());
ricettaMap.put("persone", r.getPersone());
ricettaMap.put("ingredienti", r.getIngredienti());
ricettaMap.put("vino", r.getVino());
ricettaMap.put("consigli", r.getConsigli());
ricettaMap.put("preparazione", r.getPreparazione());
}
}
String[] from = {"tipo", "titolo", "difficolta", "tempo", "ingredienti", "vino", "consigli", "preparazione", "persone"};
int[] to = {R.id.ricettaTipo, R.id.ricettaTitolo, R.id.ricettaDifficolta, R.id.ricettaTempo, R.id.ricettaIngredienti, R.id.ricettaVino, R.id.ricettaConsigli, R.id.ricettaPrep, R.id.ricettaPersone};
SimpleAdapter adapter = new SimpleAdapter(
this,
data,
R.layout.list_cibo,
from,
to);
XML of R.layout.list_cibo
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:padding="5dip"
android:gravity="right">
<TextView
android:text="Titolo Ricetta"
android:singleLine="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="5dip"
android:textAppearance="?android:attr/textAppearanceMedium"
android:scrollHorizontally="false"
android:id="@+id/ricettaTitolo">
</TextView>
<TextView
android:text="Difficoltà"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#FF0000"
android:layout_marginLeft="5dip"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ricettaDifficolta"
android:singleLine="true"
android:scrollHorizontally="false">
</TextView>
<TextView
android:text="Tempo"
android:layout_toRightOf="@+id/ricettaDifficolta"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#FF3300"
android:layout_marginLeft="20dip"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/ricettaDifficolta"
android:gravity="right"
android:id="@+id/ricettaTempo"
android:layout_width="fill_parent">
</TextView>
<TextView
android:id="@+id/ricettaTipo"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:id="@+id/ricettaPersone"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/ricettaIngredienti">
</TextView>
<TextView
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/ricettaVino">
</TextView>
<TextView
android:id="@+id/ricettaConsigli"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:id="@+id/ricettaPrep"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</RelativeLayout>
I use three different SimpleAdapter to create three different lists, based on three different colors; what I'd like to do is to create only one list that colors each item in one of the three colors, according to a parameter.
I've tried modifying the TextViews using .setColor
but it doesn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SimpleAdapter
中有一个方法可以实现这一点。它称为ViewBinder
。尝试在以下代码之后立即包含这行代码:SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.list_cibo, from, to);”
和之前的“setListAdapter(adapter);
。将为每个 R.id.ricettaTipo、R.id.ricettaTitolo、R.id.ricettaDifficolta、R.id.ricettaTempo、R.id.ricettaIngredienti、R.id 调用
setViewValue
方法.ricettaVino、R.id.ricettaConsigli、R.id.ricettaPrep、R.id.ricettaPersone。The setViewValue
方法将在每个视图/每次绘制上述 R.id 之一时被调用。There is a method inside
SimpleAdapter
for that. It's calledViewBinder
. Try to include this line of code immediately after:SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.list_cibo, from, to);”
and before“setListAdapter(adapter);
.setViewValue
method will be called for eachR.id.ricettaTipo, R.id.ricettaTitolo, R.id.ricettaDifficolta, R.id.ricettaTempo, R.id.ricettaIngredienti, R.id.ricettaVino, R.id.ricettaConsigli, R.id.ricettaPrep, R.id.ricettaPersone.
The setViewValue
method will be called each View/each time one of the aboveR.id’s
is being drawn.为什么不尝试使用自定义适配器类(也许是配方适配器)扩展 SimpleAdapter 类,并覆盖 getView(intposition,ViewconvertView,ViewGroupparent) 方法。
代码取自 grepcode
Why don't you try extending the SimpleAdapter class with a custom adapter class, maybe recipe adapter, and overwrite the getView(int position, View convertView, ViewGroup parent) Method.
the code is taken from grepcode