Bonjour,

Je développe actuellement un widget qui comporte une liste d'items. Aujorud'hui, le chargement de cette liste est faite à l'aide de données qui sont pour le moment stockées dans un tableau multidimensionnel. Dans ce tableau se trouve un champ (repertoire[i][5]) qui est une chaine égale à true ou false.

Je souhaiterai effectuer un traitement sur cette chaine et affecter un un textview présent dans chaque item de ma liste dans mon widget, la couleur verte si la chaine est égale à true et rouge sinon. Je ne sais pas ou positionner mon code pour opérer mon traitement.

Aujourd'hui, je l'ai fait dans la méthode onCreate() et getViewAt() mais ce traitement est opéré que pour le premier item de la liste. Comment pouvoir l'appliquer à l'ensemble des éléments ?

Voici le code de ma classe monWidgetService.java :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
 
import java.util.ArrayList;
import java.util.List;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;
 
public class StackWidgetService extends RemoteViewsService {
    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return new StackRemoteViewsFactory(this.getApplicationContext(), intent);
    }
}
 
class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
    private static int mCount;
    private List<WidgetItem> mWidgetItems = new ArrayList<WidgetItem>();
    private Context mContext;
    private int mAppWidgetId;
    public Boolean isFinished = false;
 
    public StackRemoteViewsFactory(Context context, Intent intent) {
        mContext = context;
        mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
    }
 
    public void onCreate() {
        // In onCreate() you setup any connections / cursors to your data source. Heavy lifting,
        // for example downloading or creating content etc, should be deferred to onDataSetChanged()
        // or getViewAt(). Taking more than 20 seconds in this call will result in an ANR.
 
    	String[][] repertoire = new String[][]{
                {"HP Touchpad", "descriptionHP", "http://google.frfrfr", "99€", "80€", "true"},
                {"test1", "desc1", "http://monsite1.fr", "10€", "80€", "false"},
                {"test2", "desc2", "http://monsite2.fr", "20€", "80€", "true"},
                {"test3", "desc3", "http://monsite3.fr", "20€", "80€", "true"},
                {"test4", "desc4", "http://monsite4.fr", "20€", "80€", "false"},
                {"test5", "desc5", "http://monsite5.fr", "20€", "80€", "true"},
                {"test5", "desc6", "http://monsite6.fr", "20€", "80€", "false"}};
 
    	mCount = repertoire.length;
 
        for (int i = 0; i < repertoire.length; i++) {
            mWidgetItems.add(new WidgetItem(repertoire[i][0], repertoire[i][1], repertoire[i][2], repertoire[i][3], repertoire[i][4], repertoire[i][5]));
            if (repertoire[i][5] == "true"){
            	isFinished = true;
            }
            else {
            	isFinished = false;
            }
        }
 
        // We sleep for 3 seconds here to show how the empty view appears in the interim.
        // The empty view is set in the StackWidgetProvider and should be a sibling of the
        // collection view.
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
 
    public void onDestroy() {
        // In onDestroy() you should tear down anything that was setup for your data source,
        // eg. cursors, connections, etc.
        mWidgetItems.clear();
    }
 
    public int getCount() {
        return mCount;
    }
 
    public RemoteViews getViewAt(int position) {
        // position will always range from 0 to getCount() - 1.
 
        // We construct a remote views item based on our widget item xml file, and set the
        // text based on the position.
        RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_item);
        rv.setTextViewText(R.id.titre, mWidgetItems.get(position).titre);
        rv.setTextViewText(R.id.description, mWidgetItems.get(position).description);
        rv.setTextViewText(R.id.url, mWidgetItems.get(position).site);
        rv.setTextViewText(R.id.ancienprix, mWidgetItems.get(position).ancienprix);
        rv.setTextViewText(R.id.nouveauprix, mWidgetItems.get(position).nouveauPrix);
 
        if (isFinished == false) {
        	rv.setInt(R.id.isFinished, "setBackgroundColor", android.graphics.Color.GREEN);
        } else {
        	rv.setInt(R.id.isFinished, "setBackgroundColor", android.graphics.Color.RED);
        }
 
        // Next, we set a fill-intent which will be used to fill-in the pending intent template
        // which is set on the collection view in StackWidgetProvider.
        Bundle extras = new Bundle();
        extras.putInt(StackWidgetProvider.EXTRA_ITEM, position);
        Intent fillInIntent = new Intent();
        fillInIntent.putExtras(extras);
        rv.setOnClickFillInIntent(R.id.titre, fillInIntent);
        rv.setOnClickFillInIntent(R.id.description, fillInIntent);
        rv.setOnClickFillInIntent(R.id.url, fillInIntent);
        rv.setOnClickFillInIntent(R.id.ancienprix, fillInIntent);
        rv.setOnClickFillInIntent(R.id.nouveauprix, fillInIntent);
 
        // You can do heaving lifting in here, synchronously. For example, if you need to
        // process an image, fetch something from the network, etc., it is ok to do it here,
        // synchronously. A loading view will show up in lieu of the actual contents in the
        // interim.
        try {
            System.out.println("Loading view " + position);
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
 
        // Return the remote views object.
        return rv;
    }
 
    public RemoteViews getLoadingView() {
        // You can create a custom loading view (for instance when getViewAt() is slow.) If you
        // return null here, you will get the default loading view.
        return null;
    }
 
    public int getViewTypeCount() {
        return 1;
    }
 
    public long getItemId(int position) {
        return position;
    }
 
    public boolean hasStableIds() {
        return true;
    }
 
    public void onDataSetChanged() {
        // This is triggered when you call AppWidgetManager notifyAppWidgetViewDataChanged
        // on the collection view corresponding to this factory. You can do heaving lifting in
        // here, synchronously. For example, if you need to process an image, fetch something
        // from the network, etc., it is ok to do it here, synchronously. The widget will remain
        // in its current state while work is being done here, so you don't need to worry about
        // locking up the widget.
    }
}