2011-11-21 46 views
2

我使用的是SimpleAdapter R.layout.list_cibo的改變顏色的ListView與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填充一個ListView

<?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修改TextViews,但它不起作用。

+0

發佈您如何設置顏色的代碼。 –

+0

代碼在SimpleAdapter adapter = new SimpleAdapter聲明中設置 – Mangusto

回答

7

採取有內部SimpleAdapter該方法。它叫做ViewBinder。嘗試在此之後立即包含此行代碼: SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.list_cibo, from, to);」「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方法將被調用用於每個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方法將被調用每個View /每次正在繪製上述R.id’s之一。

+0

這就是答案!謝謝 ! – Jerry

+0

這應該是被接受的答案,因爲這個答案與問題更相關,它確實爲我節省了很多麻煩。 – Milanor

+0

謝謝!只需投票。 – Gene

1

爲什麼不嘗試使用自定義適配器類(可能是配方適配器)來擴展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; 
     } 

的代碼是從grepcode

+0

感謝您的詳細解答,請告知我是否正確:我創建了一個定義我的SimpleAdapter的新類,然後使用該類重寫了現有類的視圖我定義了? – Mangusto