2013-08-05 5 views
0

J'essaie de définir la couleur RGB sur un ListView à partir d'une base de données. Comment pourrais-je le mettre à l'intérieur du ListAdapter?comment définir dynamiquement la couleur RGB sur la liste?

Il n'affiche que la dernière valeur de couleur de la base de données.

ListAdapter adapter = new SimpleAdapter(this, contactList, 
      R.layout.list_item, 
      new String[] { TAG_GRPNAME, TAG_QNT, TAG_BUDGET, TAG_STOCK, TAG_DIFF, TAG_DIFF_P },//TAG_DIFF_P 
      new int[] { 
        R.id.l2, R.id.l3, R.id.l4, R.id.l5, R.id.l6, R.id.l7}){ 
      @Override 
      public View getView(int position, View convertView, ViewGroup parent) { 
       View v = convertView; 
       if (v == null) { 
        LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        v = vi.inflate(R.layout.list_item, null); 
       } 
       TextView text1 = (TextView) v.findViewById(R.id.l7); 
       HashMap<String, String> map=contactList.get(position); 
       map.get(TAG_COLOR); 
       String[] ARGB = COLOR.split(" "); 
       String V1=ARGB[0]; 
       String V2=ARGB[1]; 
       String V3=ARGB[2]; 
       String V4=ARGB[3]; 
       a=Integer.parseInt(V1); 
       r=Integer.parseInt(V2); 
       g=Integer.parseInt(V3); 
       b=Integer.parseInt(V4); 
       text1.setBackgroundColor(Color.rgb(r, g, b)); 

       return super.getView(position, v, parent); 
      } 

Répondre

0
ListAdapter adapter = new SimpleAdapter(this, dataList, 
      R.layout.list_item, 
      new String[] { TAG_GRPNAME, TAG_QNT, TAG_BUDGET, TAG_STOCK, TAG_DIFF, TAG_DIFF_P, }, 
      new int[] { 
        R.id.l2, R.id.l3, R.id.l4, R.id.l5, R.id.l6, R.id.l7}){ 
     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View v = convertView; 
      if (v == null) { 
       LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       v = vi.inflate(R.layout.list_item, null); 
      } 
      text1 = (TextView) v.findViewById(R.id.l7); 


         String rgbColor= dataList.get(position).get(TAG_COLOR); 
         String[] ARGB = rgbColor.split(" ");  
         a=Integer.parseInt(ARGB[0]); 
         r=Integer.parseInt(ARGB[1]); 
         g=Integer.parseInt(ARGB[2]); 
         b=Integer.parseInt(ARGB[3]);       
         text1.setBackgroundColor(Color.rgb(r, g, b)); 
         return super.getView(position, v, parent); 
      } 

    }; 
0

Vous devez retourner la nouvelle vue gonflé getView.

changement return super.getView(position, v, parent);-return v;

Exemple de getView est ci-dessous:

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
    View rowView = convertView; 
    if (rowView == null) { 
     LayoutInflater inflater = context.getLayoutInflater(); 
     rowView = inflater.inflate(R.layout.list_item, null); 
     ViewHolder viewHolder = new ViewHolder(); 
     viewHolder.text1 = (TextView) rowView.findViewById(R.id.l7); 
     rowView.setTag(viewHolder); 
    } 

    ViewHolder holder = (ViewHolder) rowView.getTag(); 
    HashMap<String, String> map=contactList.get(position); 
       map.get(TAG_COLOR); 
       String[] ARGB = COLOR.split(" "); 
       String V1=ARGB[0]; 
       String V2=ARGB[1]; 
       String V3=ARGB[2]; 
       String V4=ARGB[3]; 
       a=Integer.parseInt(V1); 
       r=Integer.parseInt(V2); 
       g=Integer.parseInt(V3); 
       b=Integer.parseInt(V4); 
       holder.text1.setBackgroundColor(Color.rgb(r, g, b)); 

    return rowView; 
    } 


static class ViewHolder { 
    public TextView text1 ; 
    } 
+0

statique a été une erreur ... Si je supprime statique, il fonctionne, mais toujours une même sortie ... – ibu

+0

dans TAG_COLOR il ne contiendra que le dernier code couleur sur db ... comment obtenir un ensemble de valeur – ibu

Questions connexes