android– 使用SimpleAdapter更改ListView中项目的颜色

android– 使用SimpleAdapter更改ListView中项目的颜色,第1张

概述我正在使用SimpleAdapter填充ListViewArrayList<Recipe>ciboList=null;ArrayList<HashMap<String,Object>>data=newArrayList<HashMap<String,Object>>();for(inti=0;i<ciboList.size();i++){//Recipeismyownc

我正在使用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);

R.layout.List_cibo的XML

<?xml version="1.0" enCoding="utf-8"?><relativeLayoutxmlns: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修改TextVIEws,但它不起作用.

解决方法:

SimpleAdapter中有一个方法.它叫做VIEwBinder.尝试在以下情况后立即包含以下代码:
 SimpleAdapter adapter = new SimpleAdapter(this,data,R.layout.List_cibo,from,to);“and”setlistadapter(adapter);.

SimpleAdapter.VIEwBinder binder = new SimpleAdapter.VIEwBinder() {@OverrIDepublic 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);    

将为每个R.ID.ricettaTipo,R.ID.ricettaTitolo,R.ID.ricettaDifficolta,R.ID.ricettaTempo,R.ID.ricettaIngredIEnti,R.ID.ricettaVino,R.ID.ricettaConsigli,R调用setVIEwValue方法.ID.ricettaPrep,R.ID.ricettaPersone.每次VIEw /每次正在绘制上述R.ID之一时,都会调用setVIEwValue方法.

总结

以上是内存溢出为你收集整理的android – 使用SimpleAdapter更改ListView中项目的颜色全部内容,希望文章能够帮你解决android – 使用SimpleAdapter更改ListView中项目的颜色所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1100927.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存