[英]Change color of an item in ListView with SimpleAdapter
I'm populating a ListView using a SimpleAdapter 我正在使用SimpleAdapter填充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);
XML of R.layout.list_cibo R.layout.list_cibo的XML
<?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; 我使用三个不同的SimpleAdapter创建三个不同的列表,基于三种不同的颜色; 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. 我尝试使用
.setColor
修改TextViews,但它不起作用。
There is a method inside SimpleAdapter
for that. SimpleAdapter
有一个方法。 It's called ViewBinder
. 它叫做
ViewBinder
。 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);
尝试在以下之后立即包含这行代码:
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.list_cibo, from, to);”
and before “setListAdapter(adapter);
. 。
SimpleAdapter.ViewBinder binder = new SimpleAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Object object, String value) {
System.out.println("view= "+view);
System.out.println("view.toString()= "+ view.toString());
System.out.println("view.getId()= "+ view.getId());
System.out.println("view.getVisibility()= "+ view.getVisibility());
System.out.println("view.equals((TextView) view.findViewById(R.id. ricettaTipo))= "+ view.equals((TextView) view.findViewById(R.id. ricettaTipo)));
if (view.equals((TextView) view.findViewById(R.id.ricettaTipo)))
{
TextView ricetta = (TextView) view.findViewById(R.id.ricettaTipo);
//Change color/answer/etc for ricettaTipo
}
//OR
if (view instanceof TextView) {
//Do stuff
return true;
}
return false;
}
};
adapter.setViewBinder(binder);
setListAdapter(adapter);
setViewValue
method will be called for each 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.
setViewValue
每个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.
调用setViewValue
方法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.
The setViewValue
method will be called each View/each time one of the above R.id's
is being drawn. 每次View /每次正在绘制上述
R.id's
之一时,都会调用The setViewValue
方法。
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. 为什么不尝试使用自定义适配器类(可能是配方适配器)扩展SimpleAdapter类,并覆盖getView(int position,View convertView,ViewGroup parent)方法。
public View getView(int position, View convertView, ViewGroup parent) {
return createViewFromResource(position, convertView, parent, mResource);
}
private View createViewFromResource(int position, View convertView,
ViewGroup parent, int resource) {
View v;
if (convertView == null) {
v = mInflater.inflate(resource, parent, false);
/* set a background color depending on the recipe shown in this table
* row you will have to include a recipe.getType() method
* to determine the necessary color
* v.setBackgroundColor(R.color.your_favourite_color)
*/
final int[] to = mTo;
final int count = to.length;
final View[] holder = new View[count];
for (int i = 0; i < count; i++) {
holder[i] = v.findViewById(to[i]);
}
v.setTag(holder);
} else {
v = convertView;
}
bindView(position, v);
return v;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.